Linux ls command to list files

The ls command is one of the basic commands that Linux users should know

3 min read
By myfreax
Linux ls command to list files
Linux ls command to list files

The ls command is one of the basic commands that Linux users should know. It is used to list information about files and directories in the file system.

The ls command is part of the GNU core commands and is installed on all Linux distributions.

In this tutorial, we'll show you how to use the ls command with practical examples and details of the most common ls options.

Includes list file permissions, modifying time, size, sorting, showing hidden files, sorting ls output, and RECURsively listing subdirectories.

Syntax of the ls command ls [OPTIONS] [FILES]. OPTIONS is the ls option, FILES can be one or more files.

When no parameters are used, the ls command lists all files in the current working directory. The documents are listed in alphabetical order:

To list the files in the specified directory, pass the path to the directory to the ls command. For example, to list the contents of the /etc directory, run the command ls /etc.

You can also pass multiple directories and files separated by Spaces to the ls command.

If the user you are currently logged in to does not have access to the directory.

You will receive a permission denial error message indicating that the directory cannot be opened by the ls command.

ls: cannot open directory '/root': Permission denied.


ls /etc

ls /etc /var /etc/passwd

ls /root

Commonly used options

The default output of the ls command only displays the name of the file, which is not very informative. The -l option enables ls to print files in long list format.

When using the long list format, the ls command displays the file type, permissions, number of hard links, owner, group, file size, date and time, and file name.

ls -l /etc/hosts
-rw-r--r-- 1 root root 337 Oct  4 11:31 /etc/hosts

Let's explain some of the important columns in the output. The first character shows the file type. In our example, the first character is - regular file. Values for other file types include.

- Regular files, b block device files, c role files, d directory, l symbolic links, n network files, p FIFO, s socket.

The next nine characters show file permissions. The first three characters are for users, the last three are for groups, and the last three are for other users.

You can change file permissions using the chmod command. The value of the permission character can be.

r read permission, w write permission, x execute permission, s setgid bit, t stickiness.

In our example, rw-r--r-- means that users can read and write files, while groups and others can only read files. The number 1 indicates the number of hard links to the file.

The next two fields root root show the owner and group of the file, followed by the size of the file 337 in bytes.

To print the size in a readable format, use the -h option. You can change the file owner using the chmod command.

Oct 4 11:31 is the date and time of the last file modification. The last column is the name of the file.

Show hidden files

By default, the ls command does not show hidden files. In Linux, hiding a file is a dot. All the files at the beginning. To display all files, including hidden files, use the -a option.

We also use the -loption to display the details of all files.

ls -la ~/
drwxr-x--- 10 myfreax myfreax 4096 Feb 12 16:28 . drwxr-xr-x 18 myfreax myfreax 4096 Dec 26 09:21 .. -rw------- 1 myfreax myfreax 1630 Nov 18 2017 .bash_history drwxr-xr-x 2 myfreax myfreax 4096 Jul 20 2018 bin drwxr-xr-x  2 myfreax myfreax 4096 Jul 20 2018 Desktop drwxr-xr-x 4 myfreax myfreax 4096 Dec 12 2017 .npm drwx------ 2 myfreax myfreax 4096 Mar 4 2018 .ssh

Sort ls output

As we already mentioned, the ls command lists files alphabetically by default.

--sort=extension or -X sort alphabetically by file extension. --sort=size or -S Sort by file size.

--sort=time or -t sort by when the file was modified. --sort=version or -v natural type number in text

If you want to get the results in reverse sort order, use the -r option. For example, sort the files in the /var directory in reverse order by modification time.

It is worth mentioning that the ls command does not show the total space occupied by the contents of the directory.

ls -ltr /var

List subdirectories recursively

Use the -R parameter to tell the ls command to recursively display the contents of the subdirectory.

ls -R


At this point, you should have a good understanding of how to use the Linux ls command. For more information about other ls options, visit the GNU Coreutils page.

If you have any questions or feedback, please feel free to comment.