How to Sort Files in Linux (GUI and Shell)

When we view the contents of a Directory in Ubuntu, they are listed on the basis of the file and folder names. Sometimes, we need to sort the contents of the directory to make the list more informative for us. For example, in order to view which file/folder is taking more space on your system, you can list them on the basis of size.

In this article, we will explain how to view sorted files and folders in the Ubuntu Nautilus file manager and also learn how to print the output of the ls command in the command line according to our sorting preferences.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Sorting Files in Nautilus Files Manager

The Nautilus File Manager provides a built-in feature to sort files based on multiple available user preferences. Simply open the Nautilus File Manager and click on the Files menu in the top bar.

Nautilus Filemanager Preferences

Then select the Preferences option from the File menu; this will open the Preferences window in the “Views” view. Switch to the List Columns view by clicking the relevant tab in the Preferences window. You will be able to view the following options on the basis of which, you will be able to sort files and folders in the file manager:

List Columns

Select the sort order through this view and your file and folder names will now be sorted in this order. You can also use the Reset to Default option that sets the sort order to the system default, i.e, sorting on the basis of Name, Size, and Last Modified.

You can also do some basic sorting by clicking the options button located in the top-right corner of the file manager view.

Use options Button for sorting

Sorting Files through the ls command

We mostly use the ls command to list files and folders of a directory in the Linux command line.

Here, we will list some ways through which we can sort the output of this command to make the information more useful for us. All we need to do is use the relevant flag with this command in order to sort the output according to a sorting order.

In order to open the Ubuntu command line, the Terminal, either access it through the system Dash or use the Ctrl+Alt+T shortcut.

Sort by name

The ls command displays the contents of a directory by the file/folder names by default. You can view a vertical list of the directory contents, sorted name wise, explicitly through the following command:

$ ls -1

Sort by size

In order to view the contents of a directory, sorted on the basis of size, use the following command:

$ ls -S

Sort by modification date

In order to view the contents of a directory, sorted on the basis of the date of modification, use the following command:

$ ls -t

Sort by last access time

In order to view the contents of a directory, sorted on the basis of the last time of access, use the following command:

$ ls -ut

Sort by date of creation

In order to view the contents of a directory, sorted on the basis of the date of creation, use the following command:

$ ls -Ut

Sort by extension

In order to view the contents of a directory, sorted on the basis of file extension, use the following command:

$ ls -X

How to reverse sort any order

In order to reverse the sort order you specified through a flag in the sort command, simply add the ‘r’ flag with the already specified flag.

For example, the following command will print the output of the ls command in reserve order of size:

$ ls -Sr

By following the ways defined in this article, you can now sort the contents of a directory based on your preferences, both in the UI and the command line.