The most used command for the CLIphile users in Linux. You might be a heavy user of the ‘ls‘ command. Or you might be just starting with the Linux. Either way here we are going to learn interesting things to do with ls command in Linux.
Let us dive right in and understand in brief about the ls command.
What is ‘ls’ command in Linux ?
It is a simple utility program taken from *nix systems. It’s main task is to list all the files of specified directory. The directory can be your current directory or any other which you can pass as an argument.
A simple output of the command ‘ls’ is shown below in the image. But don’t get light hearted as it can be used for much more things.
Now what more can we do apart from simply looking at the list of files ? Let us find out
Show hidden files ( -a )
You can use the -a modifier short for –all to show all the hidden files with ‘ls’ command.
You need to type in:
ls -a # or ls --all
Let us see the output of the command above
You can notice the hidden directories named
.. before the 1.
List contents unsorted ( -f )
If you notice the command outputs a pretty version of the directory structure. What if you want to see the unsorted version ?
Well it’s possible with the command below
ls -f # --color
The –color option will add the color for directories. Let us look at the output below.
Show contents as list ( -l )
We can make the output a little bit much more readable with the option
Use the command below to get a pretty output.
Which will produce an output as below
It looks quite good to the eyes, doesn’t it ?
Show full date time ( –full-time )
Up until now we have been seeing date as mention We can see the full date and time instead of just Month and Day.
Execute the command below to see the full date and time
Let us see what is the output of the command
You can notice in the image above the full date and time including timezone is listed for each content.
List directories first (–group-directories-first)
It’s really hard to scan through the list with multiple files and directories. Sometimes you might want to group the directories first and everything else after that.
The command below will help you list directories first
ls --group-directories-first # -l
Let us see the output of the command below
I have combined –group-directories with
-l option to make output look pretty.
Show readable file size ( -lh )
If you have noticed the file size is always listed in bytes. That is really hard to make sense of if we have a large file.
So for that reason there is an option
-h which will automatically format the file sizes for you. You have to combine it with
Let us see the output below
Notice the 10M file and other 4.0K unlike 4096 in above screenshots ?
Show sub directories ( -R )
You can not only list the contents in specified path. But you can also recursively list all the contents in directories of the path.
With the following command you can list out all the files inside the subdirectories.
Let us see an example output of the command above
Great! we have learn one more use of ‘ls’ command in Linux.
Also Read : 5 Productivity Tips and Tricks for UbuntuTECHENUM
Append / to directories ( -F )
We might sometimes confuse files with directories and vice versa. Let us see a quick way of appending
/ to the end of each directory.
The command to identify directory is
-F let us see how it is used
Which will produce output as below
I have 2 commands executed one with -l and one without. See the difference yourself.
Sort output multiple ways ( –sort )
You can sort the output with multiple options available to you. Le me quickly show you the command with
The command usage is as
I have sorted the contents with size let us see the output.
See the table below for more options
|Value||What will it do ?|
|none||disables the sorting; shows value without sorting|
|size||according to the size of file|
|time||modified time is taken into account|
|version||sorted with the version of the file|
|extension||according to the file extension|
List each file in new line ( -1 )
Sometimes you might want to obtain the name of file in plain text format. One line after the other.
For such cases you can use the
-1 option like below
The command will produce the output below.
Those were some of the handy tricks with ls command in Linux. Comment below if you have problem with any of them below.