Learn Interesting Uses of ‘ls’ command in Linux

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.

ls command result for / directory in Linux.

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 --allCode language: Bash (bash)

Let us see the output of the command above

ls -a command output
example: ls -a command

You can notice the hidden directories named . and .. before the 1.

Also Read : Myths and Misconceptions About Security and Hacking

TECHENUM

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 # --colorCode language: PHP (php)

The –color option will add the color for directories. Let us look at the output below.

ls -f --color command output

Show contents as list ( -l )

We can make the output a little bit much more readable with the option -l.

Use the command below to get a pretty output.

ls -l

Which will produce an output as below

ls -l command output

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

ls --full-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.

Also Read : Tycoon : A New Java ransomware targets Linux and Windows

TECHENUM

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 # -lCode language: PHP (php)

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 -l like

ls -lhCode language: Bash (bash)

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.

ls -R 

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 Ubuntu

TECHENUM

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

ls -lF

Which will produce output as below

ls -F command output

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 --sort.

The command usage is as

ls --sort=size

I have sorted the contents with size let us see the output.

ls -sort command output

See the table below for more options

ValueWhat will it do ?
nonedisables the sorting; shows value without sorting
sizeaccording to the size of file
timemodified time is taken into account
versionsorted with the version of the file
extensionaccording 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

ls -1

The command will produce the output below.

ls -1 command output

Those were some of the handy tricks with ls command in Linux. Comment below if you have problem with any of them below.

Also Read : Learning about encrypting files & folders on Ubuntu

TECHENUM

Related Posts