How to Sort Files in Ubuntu (GUI and Shell)

When we view the contents of any directory in Ubuntu, it is displayed based on file and folders' names. However, sometimes we may need to sort the files in a specific order to get a better overview and locate files easier. For instance, it may be helpful if we want to view which files were accessed last time.

In this article, I will explain how to sort files in a Ubuntu system using the Nautilus File Manager (the GUI) and the ls command (the command line).

We have used Ubuntu 20.04 for running the commands and procedures mentioned in this article.

Sorting Files in Nautilus Files Manager

In the Nautilus File Manager, there is a feature available that allows you to sort files based on various user preferences. To open the Nautilus file manager, hit the super key on your keyboard, then from the left sidebar, select the Files icon. When the Nautilus file manager opens, click on the menu button on the right side of its window displayed as three horizontal lines.

From the menu that appears, click on the Preferences option.

Nautilus preferences

You will see the following Preferences window in the Views tab.

Show sidebar in nautilus file manager

Move to the List Columns tab by clicking on it. Here you will see the following options through which you can sort files and folders in the file manager. Use the Up and Down arrow buttons to select the order in which you want the selected items to appear. If you want to roll back all changes and revert to the default settings, click the Reset to Default button.

Configure columns

You can also access some of the basic sort options by clicking on the drop-down arrow as shown in the following screenshot.

Sort drop-down menu in Nautilus file manager

Sorting Files with ls command

Ls command is used to view the contents of a specific directory in a command-line Terminal. It also provides some sorting options that make the information more helpful. If we combine the ls command with some flags, it can be used to sort the output by name, size, modification date, last accessed time, date of creation, and by extension. You can also reverse the sorting order using the -r flag.

To open the command line Terminal, go to the Activities tab on the top left corner of your desktop. Then search for the keyword terminal using the search bar. When the Terminal icon appears, click on it to open.

Sort by name

In order to sort the output of ls command by the file or folder names, combine ls with the –l flag as follows:

$ ls -l

It will return the list in alphabetical order.

Sort by size

In order to sort the output by the file size, combine the ls with the -S flag as follows:

$ ls -S

Files with the largest size will be at the top of the list.

Sort by modification date

In order to sort the output using the modification date, combine ls with the -t flag as follows:

$ ls -t

The most recently modified files will be at the top of the list.

Sort by last access time

In order to sort the output using the last accessed time, combine ls with the -u and -t flag as follows:

$ ls -ut

The most recently accessed files will be at the top of the list.

Sort by date of creation

In order to sort the output using the date of creation, combine ls with the -U and -t flag as follows:

$ ls -Ut

The most recently created files will be at the top of the list.

Sort by extension

In order to sort the output using the file extension, combine ls with the -X flag as follows:

$ ls -X

It will return the list in alphabetical order based on the file extension.

How to reverse sort any order

In order to reverse the sort order of any of the above sorts, you can use the -r or --reverse flag. For instance, to list the output of ls command in reverse order of size, use the following command:

$ ls -Sr

That is all there is to it! In this article, we have discussed some ways both the GUI and the command line through which we can sort files in a directory based on our requirements.