Bash: How to List All Files in Folder by Size


Often you may want to list all files in a folder ordered by size in Bash.

You can use the following methods to do so:

Method 1: List All Files by Size (Largest to Smallest)

ls -lS

Method 2: List All Files by Size (Largest to Smallest in More Readable Format)

ls -lSh

Method 3: List All Files by Size (Smallest to Largest)

ls -lShr

Here is what the various commands do:

  • ls: List all contents in folder
  • -l: Use long listing format
  • -S: Order by size
  • -h: Use human-readable format when possible

The following examples show how to use each of these methods in practice with the following directory on our computer:

  • c:/users/bobbi/current_data

Let’s jump in!

Example 1: List All Files by Size (Largest to Smallest)

We can use the following syntax to list all files in the current folder by size from largest to smallest:

ls -lS

The following screenshot shows how to use this syntax in practice:

Bash list files by size from largest to smallest

Notice that the files in the current folder are listed by size from largest to smallest.

Example 2: List All Files by Size (Largest to Smallest in More Readable Format)

We can use the following syntax to list all files in the current folder by size from largest to smallest in a more human-readable format:

ls -lSh

The following screenshot shows how to use this syntax in practice:

Once again, the files in the current folder are listed by size from largest to smallest.

By adding the -h option this time we are able to make the numbers more readable that represent the size of the files.

Example 3: List All Files by Size (Smallest to Largest)

We can use the following syntax to list all files in the current folder by size from smallest to largest:

ls -lShr

The following screenshot shows how to use this syntax in practice:

Bash list files by size from smallest to largest

Notice that the files in the current folder are listed by size from smallest to largest.

Related Tutorials

The following tutorials explain how to perform other common tasks in Bash:

Bash: How to Delete All Files in Directory
Bash: How to Delete All Empty Lines in File
Bash: How to Replace Multiple Characters in String

Leave a Reply