القائمة الرئيسية

الصفحات

List directory content in Linux

List directory content in Linux


We will start learning the most used and basic linux commands


When you open a terminal in Linux as a regular user, it will start in your home directory, which is the directory associated with your user account and it is often denoted by ~/ or /home/username where username is your actual username


and you can verify that running the command pwd


pwd stands for print working directory, and it is used to print the full path of the directory you are currently in


So whenever you don't know your location in the filesystem you can use the pwd command which will display the full path of the current working directory


Print working directory



Ls command


Now Let's say that we want to know what we have in the current directory. so, we want to display the content of this directory.


So what command should we use here?


For that we use the ls command which stands for list. it used to list the files and directories in a directory


Ls is one the most commonly used commands and it has various options and arguments to customize its behavior


When we run this command without any arguments, it will display the files and directories of the current directory. but it doesn't show all the content


By default, the ls command will not display hidden files which are the files and directories that start with a dot. To list all the files including hidden files we have to use the -a or --all option 


If you want to see the content of a specific directory, you can specify either the absolute path or the relative path to that directory


List directory content



Absolute path - relative path


So let's see now, what is an absolute path and and what is a relative path? and what are the differences between the two?


Absolute path


Absolute paths and relative paths are two ways to specify the location of a file or a directory within a file system


But, what is the difference between an absolute path and a relative path??


An absolute path specifies the location of a file or a directory from the root directory of the file system


For example here, the absolute path to the home directory of mary  is /home/marry


And the absolute path to python directory is /home/bob/python


So, here we start from the root directory


Absolute path



Relative path


Let's move now, to the relative path


A relative path specifies the location of a file or a directory relative to the current working directory


It is a shorter and more convenient way to describe the location of a file or a directory within a directory tree. So, in relative path, we don't need to specify the entire path from the root directory


A relative path can start either with a directory name, which a sub-directory in the current directory, a . which represent the current directory or double dots .. used to navigate up to the parent directory


For example here, the relative path of the home directory of marry is ../marry


And the relative path of the python directory is either python or ./python so the . here represent the current directory


Relative path



Ls with the -l option


Now, let's go back to the ls command


With the ls command you can use the -l option to display a long format listing, which includes additional information, such as file permissions, the owner, the group, file modification date and more


List directory content in long format



If you want to list the files and the directories sorted by modification time in descending order, you can the -t option and to reverse the sorting you can add the -r option to the options that you use


List directory content in long format in reverse order


Comments