Linux ls command

Table of Contents

Introduction

The Linux ls command is one of the basic commands and it is used to list information about files and sub-directories of the working directory (by default).
While this command is pretty simple to use, this post tells you how to best use all of its parameters.

Linux ls command

The ls command allows you to list files and directories.
It provides some parameters and can be used specifing (or not) a file or a directory.

Let’s see now the structure of ls command:

ls [PARAMETERS] [FILES/DIRECTORY]

As already mentioned before, you can avoid specifying parameters or directories.
If you type and run this command in your terminal, you can see that it returns the information for the working directory.

ls

As you can see, files and directories are listed in alphabetical order across multiple columns.

What if I want to see the information of a specific directory and not just the working one?
No problem, just add the directory name after the ls command

ls /var/

The ls command works with absolute and relative path.
In this example we have used an absolute path, but you can use a relative one as well.

If you don’t know the difference between absolute and relative path, check this post!

Last but not least, you can use more than one directory at the same time, look at this example:

ls /var/ /etc/

In this case, the output will be divided in two sections, one for var and one for etc.

Linux ls command – Show more details

By default the ls command does not return much information about files and directories, just the name.
With this command instead you will have access to different information for each file or directory present.

ls -l

Using this command, the following information will be displayed for each element:

  • The file type
  • The file permissions
  • Number of hard links to the file
  • File owner
  • File group
  • File size
  • Date and Time
  • File name
Output of Linux ls -l command
Output of Linux ls -l command

To fully understand how to interpret this output take a look at this article!

Linux ls command – Show hidden files

The ls command will not show you by default hidden files.
If you want to see hidden files as well, just use the -a parameter which is used not to hide the items starting with the . (hidden files in Linux).

ls -a

My suggestion is to use the -a parameter with the -l to obtain a complete view of all files and directories.

ls -la

Linux ls command – Sort output results

As we said, the ls command by default sort the results in alphabetical order.
But what if we want to order in another way? the Linux command ls provides several parameters to sort the results, let’s see some of them:

Parameter in extended formParameter in short form
–sort=size-S
–sort=time-t
–sort=version-v
–sort=extension-X
Sort options for ls command

Let’s do an example to understand better!

ls --sort=size

This command will show you all the elements present in your working directory sorted by size.
You will obtain the same output using this command:

ls -S

If you want to sort the results backwards, you can use the -r parameter:

ls -rS

Linux ls command – Recursive

The -R parameter of the linux ls command allows you to automatically repeat the command for all subfolders as well.
Keep in mind that if there are several nested subfolders the output will be really long and hard to read.

ls -R

Linux ls command – Possible errors

Usually the ls command does not raise errors but if the connected user does not have the permissions to access a certain directory, yes.

The typical error is the following:

ls: cannot open directory '<selected_dir>': Permission denied

Conclusion

If you have come this far, it means that you have now learned how to use the ls command with and without parameters.

As always, I recommend that you continue with the next article to learn everything there is to know about the Linux world.
Feel free to leave a comment and ask me for help if you are in trouble.

Leave a Comment

Your email address will not be published. Required fields are marked *