Basic navigation
In this section, you will learn the basics of moving around the system. Many tasks rely on being able to get to or reference the correct location. As such, this stuff forms the foundation of being able to work effectively in Linux.
Finding working directory
The first command you are going to learn is pwd, which stands for Print Working Directory. It tells you what your current working directory is.
user@bash: pwd
/home/ryan
A lot of Linux commands are named as an abbreviation of a word or words describing them.
Listing content
It is one thing to know where you are. Next, you need to know what is there. The command for this task is ls, which is short for list.
user@bash: ls
bin Documents public_html
Whereas pwd is run with no arguments, ls provides for more versatile usage. In the example above, ls has no arguments, in which case it does a plain listing of the current location. If you use the command with the square brackets ( [ ] ) as shown below, this means the items in the brackets are optional, and you can run the command with or without them.
ls [options] [location]
Usage example
In the example below, ls is run in a few different ways to demonstrate its various usages.
user@bash: ls
bin Documents public_html
user@bash: ls -l
total 3
drwxr-xr-x 2 ryan users 4096 Mar 23 13:34 bin
drwxr-xr-x 18 ryan users 4096 Feb 17 09:12 Documents
drwxr-xr-x 2 ryan users 4096 May 05 17:25 public_html
user@bash: ls /etc
a2ps.cfg aliases alsa.d cups fonts my.conf systemd
...
user@bash: ls -l /etc
total 3
-rwxr-xr-x 2 root root 123 Mar 23 13:34 a2ps.cfg
-rwxr-xr-x 18 root root 78 Feb 17 09:12 aliases
drwxr-xr-x 2 ryan users 4096 May 05 17:25 alsa.d
...
Let's break the example down:
You run
lsin its most basic form. It listed the contents of the current directory.You run
lswith a single command-line option (-l), which indicates you are going to do a long listing. A long listing has the following:- The first character indicates whether it is a normal file (
-) or directory (d). - Next nine characters are permissions for the file or directory.
- The next field is the number of blocks.
- The next field is the owner of the file or directory (
ryanin this case). - The next field is the group the file or directory belongs to (
usersin this case). - Following this is the file size.
- Next up is the file modification time.
- Finally, you have the actual name of the file or directory.
- The first character indicates whether it is a normal file (
You run
lswith the/etccommand-line argument. It tellslsnot to list the current directory but instead to list the directory content.You run
lswith both a command-line option and argument. As such, it did a long listing of the/etcdirectory.
Exploring paths
Whenever you refer to either a file or directory on the command line, you are referring to a path. A path is a means to get to a particular file or directory in a system.
Absolute and relative paths
There are two types of paths you can use—absolute and relative. Whenever you refer to a file or directory, you are using one of these paths.
To begin with, you have to understand that the file system under Linux is a hierarchical structure. At the very top of the structure is what's called the root directory. It is denoted by a single slash (/). It has subdirectories, the latter have subdirectories, and so on. Files can reside in any of these directories.
Absolute paths specify a location (file or directory) in relation to the root directory. You can identify them easily as they always begin with a forward slash (/).
Relative paths specify a location (file or directory) in relation to where you currently are in the system. They do not begin with a slash.
Here's an example to illustrate:
user@bash: pwd
/home/ryan
user@bash: ls Documents
file1.txt file2.txt file3.txt
...
user@bash: ls /home/ryan/Documents
file1.txt file2.txt file3.txt
...
In the example above:
You run
pwdto verify where you currently are.You run
ls, providing it with a relative path.Documentsis a directory in your current location.The command can produce different results depending on where you are. If you had another user in the system (Bob) and you ran the command when in their home directory, you would then list the contents of their
Documentsdirectory instead.You run
ls, providing it with an absolute path. This command provides the same output regardless of the location at the moment when you run it.
More details on paths
You'll find that a lot of stuff in Linux, including paths, can be achieved in several different ways. Here are some more building blocks you can use to help build your paths:
~(tilde)—a shortcut for your home directory. For instance, if your home directory is/home/ryan, you could refer to theDocumentsdirectory with the/home/ryan/Documentspath or~/Documents..(dot)—a reference to your current directory. For instance, in the example above, you referred toDocumentson line 4 with a relative path. It could also be written as./Documents...(dotdot)—a reference to the parent directory. You can use this several times in a path to go up the hierarchy. For instance, if you were in the/home/ryanpath, you could run thels ../../, command, and this would do a listing of the root directory.
The best approach is whichever is the most convenient for you. Here are some examples:
user@bash: pwd
/home/ryan
user@bash: ls ~/Documents
file1.txt file2.txt file3.txt
...
user@bash: ls ./Documents
file1.txt file2.txt file3.txt
...
user@bash: ls /home/ryan/Documents
file1.txt file2.txt file3.txt
...
user@bash: ls ../../
bin boot dev etc home lib var
...
user@bash: ls /
bin boot dev etc home lib var
...
Moving across directories
In order to move around in the system, use the cd command, which stands for change directory. It works as follows:
cd [location]
If you run the cd command without any arguments, it takes you back to your home directory.
You can run the cd without a location, as shown in the shortcut above. However, typically, you run it with a single command-line argument—the location you would like to switch to. You can specify the location as an absolute or relative path using any path-building blocks mentioned above. Here are some examples:
user@bash: pwd
/home/ryan
user@bash: cd Documents
user@bash: ls
file1.txt file2.txt file3.txt
...
user@bash: cd /
user@bash: pwd
/
user@bash: ls
bin boot dev etc home lib var
...
user@bash: cd ~/Documents
user@bash: pwd
/home/ryan/Documents
user@bash: cd ../../
user@bash: pwd
/home
user@bash: cd
user@bash: pwd
/home/ryan
Tab completion
Typing out the paths can become tedious. The command line has a nice little mechanism to help you in this respect. It's called Tab completion.
When you start typing a path, press the Tab key on your keyboard to invoke an auto-complete action. If nothing happens, it means there are several possibilities. If you hit Tab again, it shows you those possibilities. You can then continue typing and hit Tab again, and it will again try to autocomplete for you.
It's quite hard to demonstrate here, so it's probably best if you try it yourself. If you start typing cd /hTab/<beginning of your
username> > Tab, you will get a feel for how it works.
Activities for practicing
Right, now let's put the stuff into practice. Have a go at the following:
Start by getting familiar with moving around. Use the
cdandlscommands to explore what directories are on your system and what's in them. Make sure you use a variety of relative and absolute paths.Some interesting places to look at are:
/etcstoring config files for the system/var/logstoring log files for various system programs. You may not have permission to look at everything in the directory./bincontaining several commonly used programs./usr/bin—another location for programs in the system.
Now, go to your home directory using four different methods.
Make sure you are using Tab Completion when typing out your paths.