ls [list] – the ls command is used to list the directory contents in the Linux system. By default, the ls command displays the content of the current directory. The ls command is also available in EFI [Extensible Firmware Interface] shell.
Syntax
The general syntax of the ls command is as follow −
$ ls [OPTION]... [FILE]...
Brief description of option available in the ls command.
Sr.No.Option & Description1-a, --allDisplay all files including hidden files
2--author
Display author of each file with -l option
3-B, --ignore-backups
Do not display backup file
4-d, --directory
List only directories, not their contents
5-l
Display long listing format
6-N, --literal
Display entry names without quoting
7-R, --recursive
Display subdirectories recursively
8-x
Display list by lines instead of by columns
9-Z, --context
Print any security context of each file
10--help
Displays a help message and then exits.
11--version
Output version information and exit
To display the current directory content in the Linux/Unix system, we use the ls command as shown in below.
$ ls
Here, we will display the list contents of a directory in long listing format as well as the author of each file, date, and owner permission using -l option with the ls command in the Linux system.
vikash@tutorialspoint: ~ $ ls -l shadow total 16 -rw-rw-r—1 vikash Vikash 34 Dec 26 19:28 file.txt -rw-rw-r—1 vikash Vikash 34 Dec 26 19:28 file.txt -rw-rw-r—1 vikash Vikash 34 Dec 26 19:38 file.txt drwxrwxr-x2vikash vikash 4096 Dec 26 19:47 snow
dir [directory] – the dir command is used to list contents of the directory but the output of the dir command is not colored like ls command. In the Windows operating system the dir command is also used to list the directory contents. By default, the dir command lists the contents of a directory in a column and shorted vertically. The command is available in CLI [command line interface].
Syntax
The general syntax of the dir command
$ dir [OPTION]... [FILE]...
Brief description of options available in the dir command.
Sr.No.Option & Description1-a, --allDisplay all files including hidden files
2--author
Display author of each file with -l option
3-B, --ignore-backups
Do not display backup file
4-d, --directory
List only directories, not their contents
5-l
Display long listing format
6-N, --literal
Display entry names without quoting
7-R, --recursive
Display subdirectories recursively
8-x
Display list by lines instead of by columns
9-Z, --context
Print any security context of each file
10--help
Displays a help message and then exits.
11--version
Output version information and exit.
To display current directory contents, also we use the dir command. The output of the dir command is not colored whereas the output of the ls command is colored.
The
ls
3 command lists files and directories within the file system, and shows detailed information about them. It is a part of the GNU core utilities package which is installed on all Linux distributions.This article will show you how to use the ls command through practical examples and detailed explanations of the most common ls options.
How to Use the ls
3 Command #
ls
The syntax for the
ls
3 command is as follows:ls [OPTIONS] [FILES]
When used with no options and arguments,
ls
3 displays a list of the names of all files in the current working directory :ls
The files are listed in alphabetical order in as many columns as can fit across your terminal:
cache db empty games lib local lock log mail opt run spool tmp
To list files in a specific directory, pass the directory path as an argument to the
ls
3 command. For example, to list the contents of the cache db empty games lib local lock log mail opt run spool tmp
0 directory, you would type:ls /etc
You can also pass multiple directories and files separated by space:
ls /etc /var /etc/passwd
If the user you are logged in with doesn’t have read permissions to the directory, you will get a message saying that
ls
3 can’t open the directory:ls /root
ls: cannot open directory '/root': Permission denied
The
ls
3 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
3 command shows only the names of the files and directories, which is not very informative.The
cache db empty games lib local lock log mail opt run spool tmp
4 [ lowercase L] option tells ls
3 to print files in a long listing format.When the long listing format is used, you can see 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.
Here is an 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 this example, the first character is
cache db empty games lib local lock log mail opt run spool tmp
6, which indicates a regular file. Values for other file types are as follows:
6 - Regular file.cache db empty games lib local lock log mail opt run spool tmp
8 - Block special file.cache db empty games lib local lock log mail opt run spool tmp
9 - Character special file.cache db empty games lib local lock log mail opt run spool tmp
0 - Directory.ls /etc
1 - Symbolic link.ls /etc
2 - Network file.ls /etc
3 - FIFO.ls /etc
4 - Socket.ls /etc
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
ls /etc
5 command. The permission character can take the following value:
6 - Permission to read the file.ls /etc
7 - Permission to write to the file.ls /etc
8 - Permission to execute the file.ls /etc
4 -ls /etc
0 bit.ls /etc /var /etc/passwd
1 -ls /etc /var /etc/passwd
2 bit.ls /etc /var /etc/passwd
In our example,
ls /etc /var /etc/passwd
3 means that the user can read and write the file, and the group and others can only read the file. The number ls /etc /var /etc/passwd
4 after the permission characters is the number of hard links to this file.The next two fields
ls /etc /var /etc/passwd
5 are showing the file owner and the group, followed by the size of the file [ls /etc /var /etc/passwd
6], shown in bytes. Use the ls /etc /var /etc/passwd
7 option if you want to print sizes in a human-readable format. You can change the file owner using the ls /etc /var /etc/passwd
8 command.ls /etc /var /etc/passwd
9 is the last file modification date and time.The last column is the name of the file.
Show Hidden Files #
By default, the
ls
3 command will not show hidden files. In Linux, a hidden file is any file that begins with a dot [ls /root
1].To display all files including the hidden files use the
ls /root
2 option:ls -la ~/
ls
0Sorting the Output #
As we already mentioned, by default, the
ls
3 command is listing the files in alphabetical order.The
ls /root
4 option allows you to sort the output by extension, size, time and version:
5 [orls /root
6 ] - sort alphabetically by extension.ls /root
7 [orls /root
8] - sort by file size.ls /root
9 [ orls /root
0] - sort by modification time.ls: cannot open directory '/root': Permission denied
1 [orls: cannot open directory '/root': Permission denied
2] - Natural sort of version numbers.ls: cannot open directory '/root': Permission denied
If you want to get the results in the reverse sort order, use the
ls: cannot open directory '/root': Permission denied
3 option.For example, to sort the files in the
ls: cannot open directory '/root': Permission denied
4 directory by modification time in the reverse sort order you would use:ls
1It’s worth mentioning that the
ls
3 command does not show the total space occupied by the directory contents. To get the size of a directory , use the ls: cannot open directory '/root': Permission denied
6 command.List Subdirectories Recursively #
The
ls: cannot open directory '/root': Permission denied
7 option tells the ls
3 command to display the contents of the subdirectories recursively: