ls command is one of the basic commands that any Linux user should know. It is used to list information about files and directories within the file system. The
ls utility is a part of the GNU core utilities package which is installed on all Linux distributions.
In this tutorial, we will show you how to use the
ls command through practical examples and detailed explanations of the most common
How to Use the
The syntax for the
ls command is as follows:
ls [OPTIONS] [FILES]
When used with no options and arguments,
ls displays a list of the names of all files in the current working directory:
The files are listed in alphabetical order:
cache db empty games lib local lock log mail opt run spool tmp
To list files in a specific directory, pass the path to the directory as an argument to the ls command. For example, to list the contents of the
/etc directory you would type type:
You can also pass multiple directories and files to the
ls command separated by space:
ls /etc /var /etc/passwd
If the user you are logged in doesn’t have read permissions to the directory, you will get a message saying that
ls can’t open the directory:
ls: cannot open directory '/root': Permission denied
ls command has a number of options. In the sections below, we will explore the most commonly used options.
Long Listing Format
The default output of the
ls command shows only the names of the files and directories, which is not very informative.
-l ( lowercase L) option causes
ls to print files in a long listing format.
When the long listing format is used, the
ls command will display the following file information:
- The file type
- The file permissions
- Number of hard links to the file
- File owner
- File group
- File size
- Date and Time
- File name
Consider the following example:
ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct 4 11:31 /etc/hosts
Let’s explain the most important columns of the output.
The first character shows the file type. In our example, the first character is
- which indicates a regular file. Values for other file types are as follows:
-– Regular file
b– Block special file
c– Character special file
l– Symbolic link
n– Network file
The next nine characters are showing the file permissions. The first three characters are for the user, the next three are for the group, and the last three are for others. You can change the file permissions with the
chmod command. The permission character can take the following value:
r– Permission to read the file
w– Permission to write to the file
x– Permission to execute the file
s– setgid bit
t– sticky bit
In our example,
rw-r--r-- means that the user can read and write the file, and the group and others can only read the file. The number
1 after the permission characters is the number of hard links to this file.
The next two fields
root root are showing the file owner and the group, followed by the size of the file (
337), shown in bytes. Use the
-h option if you want to print sizes in a human-readable format. You can change the file owner using the
Oct 4 11:31 is the last file modification date and time.
The last column is the name of the file.
Show Hidden Files
By default, the
ls command will not show hidden files. In Linux, a hidden file is any file that begins with a dot (
To display all files including the hidden files use the
ls -la ~/
drwxr-x--- 10 linuxize linuxize 4096 Feb 12 16:28 . drwxr-xr-x 18 linuxize linuxize 4096 Dec 26 09:21 .. -rw------- 1 linuxize linuxize 1630 Nov 18 2017 .bash_history drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 bin drwxr-xr-x 2 linuxize linuxize 4096 Jul 20 2018 Desktop drwxr-xr-x 4 linuxize linuxize 4096 Dec 12 2017 .npm drwx------ 2 linuxize linuxize 4096 Mar 4 2018 .ssh
Sorting the Output
As already mentioned, by default the
ls command is listing the files in alphabetical order.
--sort option allows you to sort the output by extension, size, time and version:
-X) – sort alphabetically by extension.
-S) – sort by file size.
-t) – sort by modification time.
-v) – Natural sort of version numbers.
If you want to get the results in the reverse sort order use the
For example, to sort the files in the
/var directory by modification time in the reverse sort order you would use:
ls -ltr /var
It’s worth mentioning that the
ls command is not showing the total space occupied by the contents of the directory. To get the size of a directory use the
List Subdirectories Recursively
-R option tells the
ls command to display the contents of the subdirectories recursively: