This post was originally published on my blog. You can also follow me on Twitter @AnnaJMcDougall.
There is no debate: using a command-line interface (CLI) is more efficient than the graphic user interface (GUI) with which weโre all brought up. The problem is, itโs not as intuitive and itโs not what we all grew up with. Here Iโll cover the absolute basics for finding your way around your computer using the command line using Bash, which is the standard shell used by Linux. If you donโt understand what all these terms mean, donโt sweat it. The important thing is that youโve booted up Linux, opened the Terminal (shortcut Ctrl-Alt-T) and are ready to find and make files.
However, before we begin it is crucial that you understand that you see a $
in the code examples below, that means that you enter it into the command line.
By the end of this post, youโll not only be able to use the following commands, but youโll know what they mean (gasp!) and the variations you can use to help you save some time:
$pwd
$cd
$ls
Where am I? Locating your current folder using the command line using $pwd
When you first open the Terminal, by default you will see some sort of identifying information, followed by a tilde (~) and then a dollar sign ($).
The dollar sign is an indicator that you are in the command line, and is commonly used in code snippets (like on this post) to show you that the code is to be entered into the Terminal.
The tilde is also important, as it indicates you are in your Home Directory. A quick editorโs note here: Iโm purposefully going to use capital letters when referring to the Home Directory because of a fun quirk: the Home Directory is not the same as the folder/directory named โhomeโ. You will see why this becomes important later, so for now please always make this association:
Home Directory = ~$
~$
= Home Directory
So where is your home directory? Try typing this:
$pwd
Remember, you donโt need to type the dollar sign ($) because it is only ever used as an indicator that you are in the Bash terminal. So just type โpwdโ and enter.
โฆCongratulations! You just executed your first Terminal command, and you can now see exactly where in your computer you are working from. Most likely, you see something like one of these:
/Users/yourUserName
-
/home/yourUserName
/user/yourUserName
The second example in the results above shows you why Iโll always use capitals to describe the Home Directory: because โhomeโ is not always Home: in this case, your Home Directory is actually a folder within the โhomeโ directory! Got a headache yet? Great! Youโre on your way to becoming a developer!
The command $pwd
stands for โpresent working directoryโ, and it tells you where you are within your folders at any given time. If youโre used to using the Windows GUI, youโre actually already used to seeing something like this in the Explorer bar:
Root Directory vs Home Directory
You may have noticed that when you typed pwd
above, the location of your Home Directory doesnโt begin with a word but with a slash. That slash represents your Root Directory: if you picture your entire computer and all its files as one big tree, the root is the singular location from which it all stems. There is only one root location, and it is always denoted with a /
.
Just as ~$
always indicates your Home Directory, /$
always indicates your Root Directory.
Now itโs time to learn another command, $cd
, which allows us to move from one folder/directory into another. Try thisโฆ
$ cd /
Donโt forget the space, and remember not to type the $. Now, try going back to our trusty olโ pal, $pwd
, so we can find out where we are!
Hang on, itโs only showing a /
? How can that be? Well, as I clarified above, the /
indicates that we are in our root directory, and our root directory is as far back/down the tree as we can go. When we typed $cd /
we were combining two ideas: $cd
is the command, essentially saying โGo toโฆโ and then the /
is our Root Directory. So what we have told the computer is โGo to the Root Directory, pleaseโ (side note: as far as Iโm aware, there is no analogous term to โpleaseโ in the Bash Terminal, but Iโm doing a polite translation).
So now weโre in our Root Directory, and thereโs nowhere deeper for us to explore in our computer. Thatโs good to know, but really we want to be doing the majority of our work in our Home Directory. So letโs get back there! Here you have two options:
$cd ~
This is probably what youโd expect, right? The ~
represents your Home Directory, so if we use it with cd
then we can navigate home. AND IT WORKS! Hoorah!
Now, not to burst your bubble, but since the Home Directory is kinda a big deal, the developers of Bash have made it even easier to get back there. All you need to type is:
$cd
Yep, thatโs it. Just the command, running without an argument. $cd
will always take you home, no matter where you are or how far youโve roamed. If you need a way to remember it, just remember that if youโre at a party and someone tells you to โLeave!โ, you will probably just go home. If someone says โLeave with Andrew and his friends so you can make it to the next party!โ then youโll go there instead. So the $cd
command with arguments takes you somewhere in your files and folders, and the $cd
command without arguments just takes you to your Home Directory.
Does that mean $cd
can take me to any of my folders, anywhere on my computer?
Why yes, dear reader, yes it does! There are two ways of navigating to another folder: an โabsolute pathโ or a โrelative pathโ. Letโs look at both using the following folder structure as an example:
Root Directory
|__user
|__Anna (~)
|__Music
|__Downloads
|__Documents
|__CameraUploads
|__ImportantForWork
|__InternetFiles
As you can see, my Root Directory is the beginning of all my folders, and from there branches off the user directory, then the folder with my name (which is my Home Directory), then all the files I use day-to-day. Now letโs say I really want to find an HTML file which I know is important for work. As you may have guessed, itโs in my โImportantForWorkโ folder.
Absolute Paths: navigating to a folder based on its relationship to the Root Directory
An โAbsolute Pathโ describes where a folder or file is, relative to the Root directory.
We want to get to โImportantForWorkโ which is in Documents, which is in Anna (my Home Directory), which is in user, which is in my Root Directory. In Bash, this looks like the following:
/user/Anna/Documents/ImportantForWork
Please note that these commands are all case-sensitive.
Relative Paths: navigating to a folder based on where you are now
The fun thing about developers is that theyโve learned by now that laziness can often mean efficiency: similarly, although the above will always work (assuming the folder exists and you have the location correct), it is a bit of a bother to type that every time you want to move from one place to another, especially when the folders are quite close by. For example, if youโve just opened the Terminal, youโre already half way to the โImportantForWorkโ folder, because youโre in Anna folder (which is our Home Directory or ~
). So why would you bother instructing the computer to go back to where you already are? Ainโt nobody got time for that!
Instead, we can tell the computer to take us to a location nestled within our current one.
~$cd Documents
As you can see, the tilde tells us weโre in our Home Directory (Anna), and from there we can simply jump into the Documents folder. Now we just need to go one step further, into our final destination folder, and then weโll use $pwd
to confirm that we made it.
$ cd ImportantForWork
$ pwd
The Terminal should now show us /user/Anna/Documents/ImportantForWork
. Huzzah! We made it!
โHold on one second, Anna,โ I hear you say indignantly. โThat wasnโt particularly efficient!โ
Why shucks, dear reader, no it wasnโt. Letโs make it better. First weโll use $cd
alone, to bring us back to our Home Directory (Anna).
$ cd Documents/ImportantForWork
BOOM! Easy. Now weโre where we needed to be.
Want to make it even easier? Use your โtabโ button to act as an auto-complete tool.
As soon as you have enough characters typed to indicate a unique directory name, hit <tab>
and youโll see it pop up. In this case, something like
$cd Doc <tab> / Im <tab>
Navigating this way, weโve managed to get to our folder using just 11 keystrokes rather than the 40(!) we used when navigating using the absolute path.
Simply put: if you know where youโre going relative to your current location, youโre better off using the relative path in combination with the tab key.
Show me more ways to get around!
OK since you insist. Letโs have a look at our file structure again:
Root Directory
|__user
|__Anna (~)
|__Music
|__Downloads
|__Documents
|__CameraUploads
|__ImportantForWork
|__InternetFiles
As you may have noticed, you can use $cd
with a folder name to access what we call a โchild directoryโ, that is, a directory which stems directly off the one youโre in now. In the above example, CameraUploads, ImportantForWork, and InternetFiles are all child directories of Documents. Logically it therefore follows that we can refer to Documents as their โparent directoryโ.
Moving to a Parent Directory
Any time youโre in a folder, you can move to the parent directory using:
$cd ..
The .. tells the computer to move up a step. These can also be stacked to access a grandparent directory:
$cd ../..
Moving to a directory within the parent directory
Letโs say we are already in the ImportantForWork folder, and we now want to check out a photo of our nephew in the CameraUploads folder. How do we get there? If you logic it out, itโs quite simple: first we move to the parent directory, then we $cd
into the CameraUploads folder.
$cd ..
$cd CameraUploads
This will work, but as usual we can save time by stacking the two commands:
$cd ../CameraUploads
Tada! Youโve navigated up a level and down a level in one command.
Last but not leastโฆ Seeing whatโs inside using $ls
!
The last command is pretty straightforward, but next to $cd
it is one of the commands you will type the most. Navigate to your Home Directory using$cd
or $cd ~
and then type the following:
$ ls
The command stands for โlistโ and when used without an argument (as above) it shows you what files and folder are within the present working directory (i.e. where you are).
If your system is relatively new, you might not have any hidden files, but later on โ especially if you plan on using Git โ you will definitely have some! `$ls wonโt show those to you automatically, so to see them, type:
$ls -a
This -a
is called a โflagโ and it tells the computer to something special with the command itโs given. In this case, it stands for โallโ, so weโre telling the computer โList the filesโฆ yes, all of them please, even the hidden ones.โ
As you may have guessed from my wording above, you can also use $ls
with an argument, meaning you tell it which folder to list without actually having to go into it.
For example, if we want to see the files in the Documents folder, we can either do this:
~$cd Documents
$ls
or we can simply type:
~$ls Documents
This will show us everything in the Documents folder, without actually taking us outside of our Home Directory.
Thatโs it! Youโre now a genius!
OK maybe not, but at least you can now move between folders using $cd
, find out where you are using $pwd
, and see whatโs inside a folder using $ls
, plus you know how to cut some corners by using relative paths, parent directories, and the <tab>
button, saving you some time in an industry where every keystroke can be valuable.
Top comments (6)
cd
andls
are two of the most core command-line tools in our cli tool belt. They are so core to what we do yet we forget to introduce them to beginners. I can't count the number of times I've had someone tell me they don't have write permissions or the cli cant find the file it needs because the opened up cmd and never left the defaultC:/windows/system32
directory. ๐คฆโโ๏ธKeep spreading the genius โจ
Yes it is so important and yet often people done find the command line until further in, OR (like me) they start by always googling command line stuff and being super confused because it seems more complex than necessary. Honestly just getting this stuff down with half an hour of practice can save a lot of pain in future.
Great post.
One that has helped me is
ls -lart
(it will list and show any hidden files like .gitignore)Another tip for the cd command: you can type cd - (that's cd followed by a hyphen) - to go back to the previous directory!
Love this one and it's one I sometimes forget as well! Thanks for adding it Joe ๐
Cat is one of my favourites. You use it to to read text files, without leaving the terminal.