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?
In simple terms, Bash is a textual way to interact with our computer, particularly the computer’s file system.
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:
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
An important concept for us to be aware of is that Bash marks a particular directory in our system as 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:
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:
This will list out all of the files and directories in the current directory for us, neatly formatted.
Moving around our file system isn’t very useful if we aren’t able to create anything. Let’s start with learning how to create directories.
We’ll make a folder called “hello-bash” using the following command:
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.
Arguments and Paths
Before we move on, there are two important things we need to cover here.
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
But as we see with
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:
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
We can create files using the
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:
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
We’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:
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
You 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!
pwdPrints out the current working directory that bash is in.
lsLists 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
-fflag for directories!).
man [command]Shows the bash manual for the specified command.