I remember that the first thing I got stuck with when I started to code was how to use the command line. I was just so used to GUIs that seeing the prompt on the terminal was shocking. The aim of this blog post is explain some basics of the command line on Bash for working with files (directories and regular files). It’s not an extensive list of commands but some of the commands I find more useful.
The motivation for writing this post came as an attempt to contribute to the John Hopkins Data Science Specialization. Recently, while I was doing the Data Scientist’s Toolbox course, they opened a GitHub repository for gathering resources people find useful. I’ve learnt a lot by the posts, and courses that people wrote and shared on internet. I hope this could be one of my first contributions to pay back something to the community.
Enough introduction, let’s start.
Working with the Linux file system
The first thing you should know to be confortable while using a terminal is understanding how linux machines are structured. We could summarize telling that linux has a tree directory structure. There’s a root directory and inside it, there are directory
s that can contain other directories as well and so on. To understand this better let’s learn our first bash command.
pwd: For discovering the current directory
Open a terminal and type:
Note: the dollar sign ‘$’ just indicates that I’m in the terminal. You don’t have to type it.
“pwd” stands for Print Working Directory. In plain English this means that it tells you what directory you are in at the moment. After executing it, you will get something like this.
Accordingly to that, I’m in the directory “Rumil/” which is inside the directory “Users/” which is inside the main directory “/”. The tree structure is like the following (the directory
s of my path are in bold).
The directory shown when you first open a terminal is called your home directory. As I’m working on a Mac, mine is /Users/Your_Username. However, if you’re using other unix variant it could be different.
ls: Show the directory content
We know already where we are, but we don’t know which files and directories we have in our current directory. This will be retrieved by the next command.
By typing ‘ls‘ on our terminal we’ll get a list of all the visible files and directories.
A better way to use this command is with the arguments “l” and “a”:
$ ls -la
The argument -l gives us extra information from each file such as file mode, owner name, group name and so on. The -a argument shows the files that starts with a dot “.” which otherwise will be hidden. (For more information check the ls manual)
cd: Changing your current directory
‘cd‘ command that stands for Change Directory lets us to move through the directories tree (that means changing our working directory). This command works typing:
$ cd <path>
Where we have to change <path> with the directory we want to change to. For example, knowing that I’m at my home directory if I want to move to my Downloads directory (which is in my home directory) I’d type:
$ cd Downloads
Quite easy, isn’t it? So let’s add some difficulty talking about the two kinds of path.
- An absolute path is the full extension of a directory from the main directory ‘/‘. Do you remember the path we got by typing ‘pwd‘? That was the absolute path of that directory
. The good thing about the absolute paths is that they let you to refer to a directory wherever directory
you were. So, by typing
cd /Users/Rumil, You always will move to that directory.
- However, when I introduced ‘cd‘ I just typed ‘Downloads’ whereas I should have typed
/Users/Rumil/Downloadsshouldn’t I? This is known as relative path. Since I’m already in
/Users/Rumil/I don’t have to type that again, I can type just the directory
inside my current working directory and the computer will complete the path by itself.
So if I’m at
/Users/Rumil I can go to
Downloads typing either:
$ cd /Users/Rumil/Downloads(Absolute path)
$ cd Downloads(Relative path)
However if we were, for instance, at
/Users/Rumil/Documents and we wanted to move to
/Users/Rumil/Downloads we couldn’t just type
cd Downloads since Downloads isn’t at our working directory. We should use an absolute path or refer to the parent directory
/Users/Rumil. Thus, instead of typing the absolute path We can type
$ cd /Users/Rumil/Downloads(Absolute path)
$ cd ../Downloads(Relative path)
‘..’ is a reference to the parent directory of our working directory (
/Users/Rumil in the example).And we could refer back as many as we wanted
.. (the parent directory),
../.. (the parent directory of the parent directory) and so on.
Note: The reference (
..) works for any command, so
$ ls ..in our terminal will display the content of the parent directory of our working directory
Some extra hints:
$ cd -gets you back to your previous working directory
- The “~” symbol is a direct reference to your home directory. So you can type
$ cd ~and go directly instead of typing any path
- Typing just
cdgets you to your home directory as well
mkdir: Creating directories
After knowing how to move through directories we could be interested in creating our owns. The command is mkdir and works as follows
‘mkdir‘ which stands for MaKe DIRrectory works with the path types we’ve seen with
$ mkdir my_directory(Creates a directory on our current working directory)
$ mkdir ../my_directory(Creates a directory on the parent directory
of our working directory)
$ mkdir /Users/Rumil/Documents/my_directory(Creates a directory inside the
Files management commands
A file is either a directory (directory) or any file whatever its extension is (.txt, .doc …). The following commands will work on both.
cp : Copying files
If you want to copy a file into another location type:
$ cp origin_path destination_path
This command will duplicate your origin_file into the destination directory. If the destination directory doesn’t exists it will create it.
For exemple, if I have a directory in my
test and I want to copy it in
/Users/Rumil/Images I’d type:
$ cp /Users/Rumil/Downloads/test /Users/Rumil/Images
mv : Moving files
Works the same as ‘cp’ but deletes the original file once the new copy is made.
$ mv origin_path destination_path
An additional feature is that it works for renaming a file. For instance if in my working directory I have a file called example.txt I want to rename to example2.txt:
$ mv example.txt example2.txt
rm: Removing files
The command that will let us to remove a directory and all its content is
We haven’t seen the use of arguments before but -rf are 2 arguments for the rm command which allows to rm to delete the file (and it’s content if it’s a directory) automatically.
-r tells to rm that we want to delete the given directory and all of its contents.
-f tells to rm that delete the content without prompting for confirmation.
Note: Noticed that as I’ve used 2 arguments I’ve typed ‘–‘ just once and the 2 arguments together
However be careful by using this command because it can’t be undone.
A final tip: Commands documentation
I’ve explained just the basic use of some commands, but there are a lot of arguments and options for each one. Explaining each option would be tedious. However, there’s a way to check what a command does and which are its options by typing
$ man ls
ls for whatever command you want) you will acces to its manual.
When you are in the manual, press the arrow keys to read it, and ‘q’ to exit.
If you have any suggestion
There are a lot more of commands but I think I’ve covered the most relevant here. However, if you read the post and find some command is missing and should be here, tell me in the comments and I’ll gladly update the post.
Thank you for reading.
I want to thank Andrew Esler, from Code Newbie, who took the time to review this post.
And thank you to Gregor Samsa and Brieana Polk-Perez for leaving additional tips for the ls and cd commands which I’ve already included in the post.