Discover more from LetUsDevOps’s Newsletter
Navigating the Linux File System
In this post, we will look at important Linux commands that can help in navigating the file system.
Linux File System Tree
Linux organizes its files in a hierarchical directory structure. This means they are organized in a tree-like pattern of directories (sometimes called folders in other systems), which may contain files and other directories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories, and so on.
The current working directory
At any given point of time, we will be inside a specific directory in the linux file system tree. This directory is known as the current working directory.
We will now open an online terminal and try to find out the current working directory. We will be using the JSLinux online terminal to run all the commands. If you already have Linux installed on your system, you can open a new terminal and run all the commands.
Open jslinux terminal in a new tab. It may take some time to load the terminal.
Congrats!! The terminal is live and it is prompting you to type some commands. You would also have noticed the line “Welcome to Fedora 33” at the top. Fedora is one of the popular Linux distributions.
How do we find the name of the current working directory or which command should we use to give us the name of the directory in which we are there now ?
We can use pwd(print working directory) to find the absolute path of the current working directory. Let’s now type pwd on the terminal and press enter.
We get “/root” as the output after running the pwd command. You can try to visualise in the file system tree where exactly we are now.
Changing the current working directory
Let’s say you now want to navigate to a different directory in the tree. You can do this using cd command. Type cd command in your terminal followed by a pathname.
In the above example, we have used the cd command followed by the pathname “/usr/bin”. We can verify using the pwd command that our current working directory has changed.
We can specify pathnames in one of two different ways: as absolute pathnames or as relative pathnames.
An absolute pathname begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed. The pathname(/usr/bin) we have used in the above example is an absolute pathname.
A relative pathname starts from the current working directory. There are some special notations that are used to represent relative pathnames.
.(dot) notation refers to the current working directory
..(dot dot) notation refers to the parent directory of the current working directory.
Let’s try to understand these with some examples.
Scenario 1: Our current working directory is “/usr” and we want to change our working directory to “/usr/bin”. We can do this in two different ways.
Provide the absolute path(/usr/bin) to the cd command.
We know that “usr” is a directory present in the current working directory(/usr). We can use .(dot) notation to navigate here.
In most cases, we can omit the “./” and just provide the path and that will also imply the same. There is an exception to this about which we will discuss in a separate post.
Scenario 2: Our current working directory is “/usr/bin” and we want to change our working directory to “/usr”. We can do this in two different ways.
Provide the absolute path(/usr) to the cd command.
Do note that ‘“/usr” is the parent directory of “/usr/bin”. We can use ..(dot dot) notation to navigate to the parent directory.
In case, we wanted to navigate to “/” from “/usr/bin”, the below command can do the trick.
There are some more shortcuts that can be used with cd command to navigate the file system easily.
cd ~ : This will change the current directory to your home directory.
cd - : This will change the current directory to the previous working directory. In the below example, we navigated to “/usr/bin” from “/”. Now when we run cd -, we again go back to the previous directory(/).
Listing the content of a directory
To list the files and directories in the current working directory, we use the ls command.
We can also provide the pathname to the ls command and it will list the files and directories present in the path.
In case we want to see more detailed information about each file, we can use ls -l command. This gives the output in the long format.
In the above example, we have first changed the current directory to home directory and then ran ls -l command. We have two files in the home directory named “bench.py” and “hello.c”.
Let’s now look at what each field means in the above output of ls -l command for the “bench.py” file.
It is quite easy to navigate the file system on a graphical user interface with the help of mouse and keyboard. For navigating the file system on a command line interface, you will need to use commands like pwd, cd and ls. In this post, we looked at what these commands do and how to use them. I would highly recommend you to try out these commands on your system or like what I have done in this post(using online terminal).
In the next post, we will look at how to view files in Linux.
Thanks for reading LetUsDevOps’s Newsletter! Subscribe for free to receive new posts and support my work.