The ls
is a Linux shell command, used to list information about files and directories within the file system. By default ls
utility is installed on all Linux distributions. This tutorial explains how to use the ls command with examples.
How to Use the ls Command
Following is the basic syntax for the ls
command:
ls [OPTIONS] [FILES]
If no options or arguments used with ls
command, it will show a list of the names of all files in the current working directory:
ls
By default, the files will be listed in alphabetical order.
If you want to list the content of a specific directory, pass the path to the directory as an argument with ls
command. For example, to list the content of the /etc
directory you would type type:
ls /etc
You also can pass the multiple directories path in a single command separated by space:
ls /etc /var /etc/passwd
Make sure the user in which you are logged in should have read permission otherwise it will show the error message, ls can’t open the directory.
ls /root
ls: cannot open directory '/root': Permission denied
Long Listing Format
By default the ls
command shows only the names of the files and directories. You can use -l
option with ls to show files in long listing format. Following details will be displayed when long listing format used.
- The file type
- The file permissions
- Number of hard links to the file
- File owner
- Group of File
- File size
- Date and Time
- File name
For example, to view details of /etc/hosts
:
ls -l /etc/hosts
-rw-r--r-- 1 root root 597 Sep 14 2019 /etc/hosts
In above 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 fileb
– Block special filec
– Character special filed
– Directoryl
– Symbolic linkn
– Network filep
– FIFOs
– Socket
After that, nine characters are displaying the permission of the file. Out of nine, first three characters are for user, next three for the group and last three for others. As per your requirement you can change the file permissions using chmod command. Below characters are used for permissions:
r
– File read permissionw
– Permission to write to the filex
– Permission to execute the files
– setgid bitt
– sticky bit
In our last 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 shows 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 (597
bytes). To show the size of file in human readable format use -h
option. You can change the file owner using the chown command.
After that Sep 14 2019
is the last file modification date and time. In last column shows name of 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 (.
).
Use -a
option to display all files including hidden files.
ls -la ~/
drwxr-xr-x 8 tecnstuff tecnstuff 4096 Jun 2 02:14 .
drwxr-xr-x 3 tecnstuff tecnstuff 4096 Sep 14 2019 ..
-rw------- 1 tecnstuff tecnstuff 10740 Jun 19 11:45 .bash_history
-rw-r--r-- 1 tecnstuff tecnstuff 220 Sep 14 2019 .bash_logout
-rw-r--r-- 1 tecnstuff tecnstuff 3771 Sep 14 2019 .bashrc
drwx------ 3 tecnstuff tecnstuff 4096 Sep 18 2019 .cache
drwxrwxr-x 6 tecnstuff tecnstuff 4096 Sep 18 2019 Desktop
Sorting the Output
As we seen before, by default ls command shows output in alphabetical order. The --sort
option allows you to sort the output by extension, size, time and version.
--sort=extension
(or -X ) – sort alphabetically by extension.--sort=size
(or -S) – sort by file size.--sort=time
( or -t) – sort by modification time.--sort=version
(or -v) – Natural sort of version numbers.
To sort result in reverse order use the -r
option.
For example, to sort files of /var
directory by modification time in reverse sort order you would use:
ls -ltr /var
List Subdirectories Recursively
The -R
option tells the ls
command to display the contents of the subdirectories recursively:
ls -R
Conclusion
The ls
command lists information about files and directories.
To know more about ls
command visit the GNU Coreutils page or type man ls
in your terminal.
If you have any questions or feedback, feel free to leave a comment.