Bash for Beginners: Becoming a Terminal Ninja

We can do that using this command:cd .

Bash recognizes the .

as wanting to go up to the parent directory of the current directory we're in.

This is how we build relative file paths; if we want to go into a directory, we simply use the name of that directory, and if we want to go out of a directory, we use .

If you’ve been running all the commands so far, you should currently be in your home directory (if not, remember you can cd ~ to get to it).

How to Create, Move, Copy, and Delete FilesWe can create files using the touch command:touch hi.

txtThis will create a completely new, empty file that we can put whatever we want into it.

If we want to move the file we just created to somewhere else, we need to specify what we’re moving, and where we’re moving to in the file tree:mv hi.

txt hello-bashThe move command also has another use that might not make sense at first, in that it can also be used to rename files.

If we specify a file and then use the same directory it's in, but give it a different name, it will rename it:mv hello-bash/hi.

txt hello-bash/hello.

txtNotice here how we specified the directory twice, once for each argument.

If we wanted to, we could’ve also cd hello-bash and then run mv hi.

txt hello.

txt which would've accomplished the exact same thing.

Not exactly intuitive, but that’s how Bash does it.

Let’s say we want to copy a file or directory, we can use the following command:cp hello-bash/hi.

txt .

This command is copying the hi.

txt file from the hello-bash directory to the current working directory.

That’s what the .

is for, it is a file path reference to the current directory, similar to how .

is a reference to the parent directory.

We’ve gone over how to create, move, and copy both files and directories, but we’re missing one thing, and that’s how to delete a file:rm hello-bash/hi.

txtThis will remove the specified file, permanently deleting it from the file system (this command will not move a file to the trash directory, it directly deletes it, completely and permanently!).

It’s important to note that by default, this command will not delete directories.

To do that, we have to pass a special argument called a flag:rm -f hello-bashYou can easily tell which arguments are flags; they are always prefixed with a dash.

The other key point about flags is that generally they need to come before the argument they are flagging, in this case, the -f flag denotes that the following argument is a directory and not a file.

How to ask for HelpWe’ve learned a lot, but there’s one final command that every bash user needs to know, and that’s how to look up help on any command:man cdBash will then switch modes and display a Manual on the given command.

The Manual will (usually) give us a very descriptive explanation of the command, how to use it, what arguments it expects, and a list of flags we can use when running the command.

The fact that Bash enters a different mode can really get to new users, as it’s very unclear on how you exit.

To actually exit the manual, press Ctrl + z on your keyboard, and bash will return back to the command prompt.

*NOTE for Windows users: If you’re using Git Bash, the command is help.

ConclusionYou now have a solid foundation in understanding how to navigate around your computers file system, as well as how to create, move, copy, and delete files and directories.

A lot of terminology was covered too, from the home directory, absolute and relative paths, to the working directory context, and arguments and flags.

So what next?.From here, you can start to learn more useful commands to add to your workflow, which I highly recommend.

If you’re feeling adventurous, and notice that you’re running the same commands again and again, you might want to delve into the world of bash scripting.

Good luck, and happy bashing!Quick referencepwd Prints out the current working directory that bash is in.

ls Lists out the files and directories in the current working directory.

cd [path] Changes the working directory to the specified path.

mkdir [directory name] Creates a new directory with the given name.

touch [file name] Creates a new file with the given name.

mv [target file/directory] [destination directory] Moves a file or directory to the specified destination path.

cp [target file/directory] [destination directory] Copies a file or directory to the specified destination path.

rm (-f) [target file/directory] Deletes the specified file (don't forget the -f flag for directories!).

man [command] Shows the bash manual for the specified command.

.

. More details

Leave a Reply