Using Shell to Navigate the File System in Linux

Before you start

Objectives: Learn how to move trough the file system using shell and basic commands in Linux.

Prerequisites: you have to know what is path in Linux.

Key terms: command, ls, directory, files, file, cd, move, working, system, current, linux, path, home


Basic Commands

In this tutorial we will be working with basic file management commands that we need to know in order to maneuver through the file system. The first thing we have to know is how to display information about the current position in the file system. In Windows/DOS world we use the “dir” command to do that, and in the Linux world we use the “ls” command (list). If we only type the “ls” command, we will only get the names of the files in different colors. The color is based on the file type and this is set up in our user profile. This list of files output is similar to the “dir -w” command in Windows.

 ls Command

ls Command

In our case the blue color represents directories, and white represents normal files. We can also use the “ls path” command, in which path is the directory for which we want to list files. If we want to get more information about our files we can use the “ls -l” command (long listing). This command will show us the file types, the permissions, the owners, the access times, and file size.

 ls -l Command

ls -l Command

If we have many files in our directory, we can list them one screen at the time. To do that we can use the “ls -l|more” command. To move to the next screen we can press the “space” on our keyboard, or if we want to move one line at the time, we can press the “enter” key on our keyboard.

Both “ls” and “ls -l” commands only show us non-hidden files. If we want to see hidden files we have to use the “ls -a” command. Notice that now see more files in our home directory. If we compare this to the normal “ls” command, notice that with “ls -a” command we see files that start with the “.” (dot).

 ls -a Command

ls -a Command

In Linux we create hidden files by putting the “.” at the beginning of the file name or the directory name. So, anything that starts with a “.” is a hidden file. In comparison, in Windows world we hide files by using the “hidden” attribute of the file or folder. If we want to show the list of files one screen at the time we can use the “ls -a|more” command. We can also see a listing of files recursively in all sub directories, and to do that we can use the “ls -aR” command. We can also see all files in long listing mode, and to do that we combine the “ls -a” and “ls -l” together by using the single command “ls -la” command or “ls -al” command. Notice that we have combined two flags (options) together.

 ls -la Command

ls -la Command

There are many other options that we can use with the “ls” command. To see other options enter the “ls –help” command.

We also have to know how to move trough the file system. For that we use the “cd” (change directory) command. This is similar to the “cd” command in Windows/DOS world. With the “cd” command we also have to provide the path that we want to go to. We can use relative paths or absolute paths. Absolute paths always start with the “/”, meaning they are relative to the root of the file system. When we use relative paths, we move down or up the directory structure from the current working directory. For example, in our case we are currently in our home directory, so we will enter the “cd  ~/Desktop” command to move to the Desktop directory. Notice that we can always see in which directory we are currently in, our working directory (marked with red on the picture).

 cd Command

cd Command

If we want to go back to our home directory we can just type the “cd” command itself. If we use the “cd” command without any options, the shell will take us to our home directory. We can also use relative or absolute path, or use a special symbol called the tilda (~). The “~” is actually the shortcut to the home directory. To view our current position in the file system we can use the “pwd” command (print working directory). This command will show us the the absolute path of where we are in the file system.  In our example we have checked the current position by using “pwd”, and then change the working directory to “Desktop/work”.

 pwd Command

pwd Command

To go back one level in the directory structure we can use the “cd ..” command. To move up two levels we can use the “cd ../..” command, and so on. As you should know, double dot is the shortcut to the directory “above” our current working directory. To move to the root directory we can use the “cd /” command. To run the file from the current working directory we have to precede the filename with the “./”, for example “./myfile”.