When navigating a system using the command-line, there are essentially three basic commands that any user should have as second nature;
pwd
– print working directory.
cd
– change directory.
ls
– list directory contents.
Table of Contents
pwd
pwd
or will print the working directory, i.e. will tell you the path to the directory you are currently working in. Most systems, are configured to include this information on the command-line itself but in some instances, this may not be the case so simply inputting pwd
will get you back on track, if you are ever lost when navigating through a file structure.
cd
There is not much to say about cd
as it does exactly what it says on the tin, simply input cd
followed by the path to the directory you wish to navigate to. There are, however, two shortcuts that are incredibly useful to be aware of, as follows:
..
– the relative parent directory. For example, if you are in /home/user/directory/sub-directory/
and enter cd ..
, you would then be moved to /home/user/directory/
. Likewise, if you were in /home/user/directory/
you could use the same cd ..
command to navigate to /home/user/
.
~
– user’s home directory. Inputting the command cd ~
will always return you to the home directory of the current user you are connected as. For example, if you were the connected as the user example_user
and were currently in the /home/example_user/directory/sub-directory/this/is/a/really/long/path/lol/
directory, then rather than having to use cd /home/example_user/
, you could simply input cd ~
and you’d land in the same spot.
ls
ls
is the command used to list the contents of the working directory (the one we’re currently “in” so-to-speak). You can also include the path to a directory you’d like to see the contents of without having to navigate there. For example, if I want to see the contents of the /home/user/directory/sub-directory/
but my present working directory is actually /home/user02/some_other_directory/
, I don’t need to cd
my way there. I can just input ls /home/user/directory/sub-directory/
and I’ll see the contents without changing the directory, I’m actually in. (This is particularly useful when copying files etc. as it’s impractical to constantly be changing directories unnecessarily)
The ls
command is also an excellent example of using operators (sometimes described as options). Most commands in linux have operators, which allow you to modify how the command is executed. By default, the ls
command prints the directory contents in a not-so-great format to my eye so I use three operators to make things more readable for myself which are -lah
. The best way to illustrate this is with an image so check out the contents of a Windows 10 desktop printed with just ls
and then again with ls -lah
;
Read more:
Shortcuts and Wildcards in Linux.