Home Linux How to List Directory Contents on Linux

How to List Directory Contents on Linux

by admin

When working with the Linux file system, its important to know some of the different ways you can list directory contents on Linux.

This article will look at some of the commands you can use to list directory contents, which will work on whichever version of Linux you are using. These commands will also work to list directory contents on VMware ESXi.

It will cover how to do a basic directory listing, how to list specific information such as file size and permissions, and how to sort and filter the directory list output.

Let’s start by looking at the basic usage of the ls command, before moving onto some more advanced examples of how you can use ls to list directories and their contents.

Using the LS Command to List Directory Contents on Linux

If you have been using Linux then you are likely already aware of the ls command, as it is one of the first commands you will learn about using. It is basically the equivalent of the Windows dir command, though is more flexible, as we will see later.

On the Linux command line, just running ls by itself will result in listing the files contained in the current directory:

$ ls
config2.txt  file3.txt  list1.txt  list2.txt  list3.txt

You can also list the files in another directory by specifying the directory path to use with the ls command. For example:

$ ls ./myfiles/
config2.txt  file3.txt  list1.txt  list2.txt

Now, whilst we have a list of the files in the directory, that’s all we have. To display additional information about the files, use ls -l. For example:

$ ls -l
total 36
-rw-rw-r-- 1 cloud_user cloud_user 12 Dec 15 11:48 config2.txt
-rw-rw-r-- 1 cloud_user cloud_user 18 Dec 15 11:56 file3.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list1.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list2.txt

This output gives a lot more information about the files in the directory listing. It shows the file permissions, link count, owner and group information, file size and last modification date and time. By default, the files are listed in name order, but if you wish to reverse the order of the output, you can do so by using the -r option with the command. For example:

$ ls -lr

The ls -l command shows a lot more information than the default output – but be aware that this will not show any hidden files.

List Hidden Files on Linux using LS

By default, hidden files are not displayed in the default ls output. Linux hidden files and directories are prefixed with a . – for example, .hiddenfile.txt. To include hidden files in the directory listing output, use the -a or --all option with the ls command:

$ ls -all
total 44
drwxrwxr-x  2 cloud_user cloud_user 4096 Jan 18 10:36 .
drwxr-xr-x 23 cloud_user cloud_user 4096 Jan 18 09:14 ..
-rw-rw-r--  1 cloud_user cloud_user    0 Jan 18 10:36 .hiddenfile.txt
-rw-rw-r--  1 cloud_user cloud_user   12 Dec 15 11:48 config2.txt
-rw-rw-r--  1 cloud_user cloud_user   18 Dec 15 11:56 file3.txt

This time we can see a hidden file which wasn’t listed in the previous examples.

How to List ONLY Hidden Files

To only list hidden files, you can use ls -ld .?* – for example :

$ ls -ld .?*
drwxr-xr-x 23 cloud_user cloud_user 4096 Jan 18 09:14 ..
-rw-rw-r--  1 cloud_user cloud_user    0 Jan 18 10:36 .hiddenfile.txt

How to Display File Sizes when Listing Directory Contents on Linux

File sizes can be included in the ls output by using the -s or --size option. Note that the size information outputted from this command is displayed in blocks. Luckily there is an option available to present this in a human readable format, which is to use -h. For example, if we run ls -lah:

list directory contents linux

The highlighted values in the image above show the file size of the files in the directory listing.

Sort Directory List Based on File Size

To sort the directory list based on file size, the -S option can be used. Run ls -lahS to list the directory contents in descending size order:

$ ls -lahS
total 44K
drwxrwxr-x  2 cloud_user cloud_user 4.0K Jan 18 10:36 .
drwxr-xr-x 23 cloud_user cloud_user 4.0K Jan 18 09:14 ..
-rw-rw-r--  1 cloud_user cloud_user   91 Dec 16 14:45 results.txt
-rw-rw-r--  1 cloud_user cloud_user   18 Dec 15 11:56 file3.txt
-rw-rw-r--  1 cloud_user cloud_user   12 Dec 15 11:48 config2.txt

Sort Directory List by Last Modified Time

As well as sorting the directory list by file size, the directory list output can also be ordered by last modified timestamp using the -t option. For example:

$ ls -lt
total 36
-rw-rw-r-- 1 cloud_user cloud_user 91 Dec 16 14:45 results.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list1.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list2.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list3.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list4.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 list5.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 test1.txt
-rw-rw-r-- 1 cloud_user cloud_user 18 Dec 15 11:56 file3.txt
-rw-rw-r-- 1 cloud_user cloud_user 12 Dec 15 11:48 config2.txt

Using this option lists the files in the order they were last modified, with the most recently changed at the top of the list. If you want to list the directory contents with the oldest file first, you can add the -r option to the command to output the directory contents in reverse order:

$ ls -ltr
total 36
-rw-rw-r-- 1 cloud_user cloud_user 12 Dec 15 11:48 config2.txt
-rw-rw-r-- 1 cloud_user cloud_user 18 Dec 15 11:56 file3.txt
-rw-rw-r-- 6 cloud_user cloud_user 12 Dec 15 15:09 test1.txt

As you can see, the oldest file is now listed first.

Sorting and Listing Directory List Contents by File Extension on Linux

It’s useful to be able to filter and sort the files in a directory by the file extension. This lets us group together similar files such as images or text files. First of all, using the -X option with ls will allow us to sort the files within a direction using the file extension:

ls -lX
total 36
-rw-rw-r-- 1 cloud_user cloud_user  0 Jan 18 14:37 1.jpg
-rw-rw-r-- 1 cloud_user cloud_user  0 Jan 18 14:37 x.jpg
-rw-rw-r-- 1 cloud_user cloud_user 12 Dec 15 11:48 config2.txt
-rw-rw-r-- 1 cloud_user cloud_user 18 Dec 15 11:56 file3.txt

The output shows the files grouped by their extension, .jpg and .txt. If you want to list files with a particular extension, a pattern can be used. For example, to list only the .jpg files:

$ ls *.jpg
1.jpg  x.jpg

Alternatively, to hide only the .jpg files and display everything else, the following can be used:

$ ls --hide=*.jpg
config2.txt  file3.txt  list1.txt  list2.txt

Very useful for doing some housekeeping! For example, you could use the list to pipe to the mv command or the rm command to move or delete particular file types.

Sort the Directory List by other Attributes using the Sort Command

So far we have seen how to sort the directory using the file size and the last modified time stamp. We can go further however by combining ls with the sort command. The sort command has has an option to sort by any of the columns presented in the ls output by using the -k option. This is used by using -k along with the column number we wish to sort by. For example, to sort by the file Owner, which is detailed in the 3rd column, the following can be used:

$ ls -la | sort -k3
total 44
-rw-rw-r--  1 cloud_user cloud_user    0 Jan 18 10:36 .hiddenfile.txt
-rw-rw-r--  1 cloud_user cloud_user    0 Jan 18 14:37 1.jpg
-rw-rw-r--  1 cloud_user cloud_user    0 Jan 18 14:37 x.jpg
-rw-rw-r--  1 cloud_user cloud_user   12 Dec 15 11:48 config2.txt

Likewise we could use this to sort by the group:

$ ls -la | sort -k4

More Examples of How to List Directory Contents on Linux

So far I have covered many of the common use cases, with examples of how to use the ls command effectively. In this section I will cover some other examples that you may find useful. In the interests of keeping this article a little shorter, the examples in this section will be presented in a table:

CommandPurpose
ls -ltu List files by access date (most recently accessed first)
ls –formatChange the output format of the directory list. The options available are:
# ls –-format=across
# ls –format=comma
# ls –format=horizontal
# ls –format=long
# ls –format=single-column
# ls –format=verbose
# ls –format=vertical
ls -p Append indicators like (/=@|) in output to the contents of the directory.
ls –sort Other ways to sort the directory list output. Options include:
# ls –sort=extension
# ls –sort=size
# ls –sort=time
# ls –sort=version
# ls –sort=none
ls -nPrint numeric UID and GID for all the items in a directory
ls -d The ls command will show both files and directories by default. Use this if you want to list only directories
ls -i Display Inode number of File or Directory
ls -R Recursively list Sub-Directories. Same as using –recursive
ls -l > out.txtRedirect Directory List output to a file

Conclusion

In this article you have learnt how to use the ls command to list directory contents on Linux. We first looked at the output from the default ls command, then looked at how we can use options to list more detail on the files in a directory.

We then looked at how to sort and filter the directory list output, and how to list hidden files on Linux. Finally we had a look at some of the other advanced ls command options, including how to export the directory file list to a text file.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More