Table of Contents
- Linux ls command
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.
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
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.
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
To fully understand how to interpret this output take a look at this article!
Linux ls command -1 parameter
The ls command provides also a numerical parameter that is -1 and it is used to list one file per line.
Basically it is very similar to the previous one but differs in the number of files per line.
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).
My suggestion is to use the -a parameter with the -l to obtain a complete view of all files and directories.
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 form||Parameter in short form|
Let’s do an example to understand better!
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:
If you want to sort the results backwards, you can use the -r parameter:
Linux ls – Recursive option
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.
Linux ls command – Reverse
It may happen that we get the data in an order that is not right for us. The Linux ls command allows you to reverse the sorting of data using a simple parameter i.e. -r or –reverse.
Here is an example of usage:
Linux ls command -f parameter
The linux ls command provides a special parameter that is the
-f which does a combination of actions.
This command will print out the result not sorted, without colors, not in list and without the size.
It is like a shortcut to use the
ls command with several parameters all together.
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
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.