Bash for Beginners: Becoming a Terminal Ninja

Brandon Burrus
May 28 · 7 min read
Photo by Markus Spiske on Unsplash

Using the Terminal for the first time can be really intimidating.

We’re creatures of habit, used to beautiful and sophisticated interfaces that help us accomplish whatever we’re looking to get done.

Compare that to the blank slate and blinking cursor of the command line, and suddenly all the safety wheels of modern computing systems come right off.

In this article, we’ll explore how to get up and running with Bash, including:

  • What the heck even is Bash?
  • How to navigate our computers file system.
  • How to create, move, copy, and delete files and folders.
  • Key concepts in bash, including the home directory, arguments, and paths.
  • How to ask bash for help.

*Note for Windows users: The Command Prompt is not the same as Bash, and a majority of the commands simply will not work the same. If you want something similar to Bash, on a Windows System, I highly recommend Git Bash, it’s free, and brings most of the modern features of Bash to the Windows OS.


What is Bash?

We do this by entering specific commands that tell Bash exactly what to do.

The first thing we need to know is that Bash will execute commands from a particular context, that being a specific directory on our computer (a directory is Bash’s word for folder).

Bash calls this context the “working directory”, and we can run a command to tell us the working directory:

pwd

The name of the command is short for Print Working Directory, which tells us where we currently are in our file system, as far as Bash is concerned.

Congratulations, you’ve just run your first command!


The Home Directory

It denotes this specific directory using the ~ symbol, and we can move our working directory context to this folder by running the command:

cd ~

This command is short for Change Directory, and if we now run pwd we'll see what our home directory is.

Most likely, nothing has changed, usually because Bash will start us in the home directory, letting us work from there.

If we wanted to know all the folders and files in the current directory we’re in, we can run the following command:

ls

This will list out all of the files and directories in the current directory for us, neatly formatted.


Creating Directories

We’ll make a folder called “hello-bash” using the following command:

mkdir hello-bash

This will make a directory in the current working directory with the given name.

We can run ls again to see our new folder.

If we wanted to move our working context into this folder, we can use cdagain, but this time using the name of our created folder as the name of the directory we want to "change" into.

cd hello-bash

Arguments and Paths

By now, you’ve probably noticed that some commands are composed of multiple words.

The first part of a command will always be the name of the command. The ones we know so far are pwd, cd, ls, and mkdir.

But as we see with cd and mkdir, they require a second word, which we call an Argument.

An argument is what we give to the command when we call it and is generally what the command will act upon (for most Bash commands, this is a file or directory).

An important thing to note is that some commands may require more than one argument. In fact, commands can take a near-infinite amount of arguments.

The other important concept is Paths.

Every file system has what is called the “root” directory, which from there has directories, which themselves can have directories (and files, of course).

We can think of this as a tree, in that the “root directory” is literally the root of the tree, with directories branching out from there.

A path is simply where we are in the tree, and this is exactly what pwd does; it tells us where we currently are at in the file tree.

There are specifically two kinds of paths: Absolute and Relative.

An absolute path is where one directory is in relation to the root directory (this is what pwd gives us).

A relative path is where one directory is, in relation to another directory.

This is what cd works from, it allows us to move from one directory to another, using a relative path (though it can also use an absolute path too if needed).

We know that if we want to move into a directory contained within the current directory, all we have to do is cd [name] where name is the name of the directory.

But what if we want to move out of the current directory, and up into the parent directory? 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 Files

touch hi.txt

This 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-bash

The 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.txt

Notice 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.txt

This 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-bash

You 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 Help

man cd

Bash 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.


Conclusion

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 reference

  • 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.

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Brandon Burrus

Written by

Software Engineer @ Prism Studios · Phoenix, AZ

The Startup

Medium's largest active publication, followed by +536K people. Follow to join our community.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade