Good Evening Guys! So, Today we will learn the most popular and mostly used command in Linux which is ls command. It is mostly used to list the files and folders.
With the help of ls command, we can check the permission, file type, username group, and many more. We will discuss this in detail in the same post.
So, Let’s start with our first command with the Practice link
$ ls output: exam file linux practice taste the
As you can see the above the files and folder are listed, but how can we say whether the exam is the folder or a file.
-l option is for a long listing
$ ls -l output: drwxrwxr-x 2 tla tla 4096 Apr 19 19:28 exam -rw-rw-r-- 1 tla tla 0 Apr 19 19:29 file drwxrwxr-x 2 tla tla 4096 Apr 19 19:28 linux drwxrwxr-x 2 tla tla 4096 Apr 19 19:28 practice drwxrwxr-x 2 tla tla 4096 Apr 19 19:28 taste drwxrwxr-x 2 tla tla 4096 Apr 19 19:28 the
Now you can see the highlighted part in red color that is d and –, this are the file types, -d is denoted as directory and – is the regular file.
a option will see all the file including hidden file.
$ ls -a output: . .. hacked linux paper quiz
So the .(dot) in the blue colour is the hidden directory in the Linux.
-R option will list the subdirectories in recursively format.
So, as you can see exam that is a folder in that current directory, So in the exam folder there was hacked, linux and paper and also contain a file named as quiz.
But now the -R option was also searched in the hacked, linux and paper directory.
-r option to reverse order while sorting use with option -l
$ ls -lr output: -rw-rw-r-- 1 tla tla 0 Apr 19 19:56 quiz drwxrwxr-x 2 tla tla 4096 Apr 19 19:56 paper drwxrwxr-x 2 tla tla 4096 Apr 19 19:56 linux drwxrwxr-x 2 tla tla 4096 Apr 19 19:56 hacked
So, the above example has sorted by the alphabetical order from Z to A. Now we will sort this by time, Let’s see that with
t option to sort the file by modification time
$ ls -lrt output: drwxrwxr-x 2 tla tla 4096 Apr 19 18:56 paper drwxrwxr-x 2 tla tla 4096 Apr 19 19:46 linux drwxrwxr-x 2 tla tla 4096 Apr 19 20:59 hacked -rw-rw-r-- 1 tla tla 0 Apr 19 21:50 quiz drwxrwxr-x 2 tla tla 4096 Apr 19 22:56 comp -rw-rw-r-- 1 tla tla 0 Apr 19 23:01 learn_new
Just look at the red highlighted part in the above example, you can see the timings, the last file learn_new is the latest one, but that file is at the last file because we have used -r option to reverse the things.
So, Guys, we can say that to see the latest file you can use the command “ls -lrt“
s option to print the size of the files
$ ls -s output: total 130732 4 exam 130724 p4v.tgz 4 test
So, the above output shows the data in bytes, let’s use -h option so, that we can read the output easily.
h option for the human-readable
$ ls -hs output: total 128M 4.0K exam 128M p4v.tgz 4.0K test
The human readable means the size of the file in kb, mb and gb.
As, we can clearly see the size of the file in the above output.
S option to sort by file size
$ ls -lS output: total 130732 -rw-r--r-- 1 root root 133855793 Apr 21 22:06 p4v.tgz drwxrwxr-x 6 tla tla 4096 Apr 19 23:01 exam drwxrwxr-x 3 tla tla 4096 Apr 20 15:05 test
Above example can justify that -S option sorted by file size
$ man ls
This is known as the MANUAL page of ls command, where you can get more options for this command.
So, Lets Play the QUIZ for the ls command
ls command cheat sheet
Check how to create directory in Linux with there different options.
Thank you Guys to read this post! Support us Guys
ls cheat sheet, ls command in Linux