BACK TO BLOG
Product Updates

How to display files sorted by size using a GNU/Linux script

Discover how to show files sorted by size in GNU/Linux using a simple one-liner script you can reuse whenever needed. This command helps identify the largest files in a filesystem that's running out of space, so you can purge or relocate unnecessary files to free up space and prevent disk full…

July 12, 2023
·
Manuel Pérez Gómez-Miranda
Table of contents
Example heading content
Example heading content

Discover how to show files sorted by size in GNU/Linux using a simple one-liner script you can reuse whenever needed. This command helps identify the largest files in a filesystem that's running out of space, so you can purge or relocate unnecessary files to free up space and prevent disk full conditions that could crash your system. Since we're in the GNU/Linux world, we'll use native commands available on any system—no scripting languages like Python or Perl required.

How to show files sorted by size using GNU/Linux commands

The one-liner command is: # du -a | sort -n -r | less This combines du and sort commands with pipes (pipelines) to process the output step by step. du (Disk Usage) is a standard UNIX/Linux command for checking disk space usage. Its basic structure is: # du [options] [file/system]du recursively scans the file structure, summing up space used by each file. The -a flag includes all files, not just directories. NOTE:For more on du, see the manual page: du(1) - Linux manual page The du -a output pipes to sort -n -r:

  • -n: Sorts numerically by value
  • -r: Reverse order (largest first). Remove -r for smallest first.

NOTE:For more on sort, see: sort(1) - Linux manual page Finally, it pipes to less for paged viewing (use more if preferred): # du -a | sort -n -r | more Or save to a file for later review: # du -a | sort -n -r >> filelist.txt

Step 1. Executing file list sorted by size (largest first) using Linux script
Step 1. Executing file list sorted by size (largest first)
Step 1. First window of command output using Linux script
Step 1. First window of command execution output

One-liner: Show files sorted by size in GNU/Linux

To list filesystem files sorted largest to smallest: # du -a | sort -n -r | less That's it—your complete one-liner script.

Conclusions

As shown in this how to show files sorted by size in GNU/Linux tutorial, a few well-chained commands provide critical disk usage insights to prevent filesystem full conditions. This is a quick troubleshooting workaround—implement disk quotas and filesystem monitoring for production environments. The command works on all GNU/Linux systems. If you encounter issues, contact us—we're happy to help. Find more Linux tutorials on the Jotelulu blog. Thanks for reading!

Read more

Product Updates
Manuel Pérez Gómez-Miranda
·
6.25.26

How to configure and troubleshoot time issues in Windows Server?

Configure and troubleshoot time synchronization issues in Windows Server using NTP, CMD and PowerShell to avoid domain and security problems.
Read article
Product Updates
Manuel Pérez Gómez-Miranda
·
6.22.26

Extended support hours at weekends

Partner Success now available weekends too! Mon-Fri 08:00-20:00, Sat-Sun 09:00-18:00 [GMT+1]. Ideal for migrations & maintenance without disrupting SME operations.
Read article
Product Updates
Manuel Pérez Gómez-Miranda
·
6.22.26

Assisted Migrations, the new feature for migrating cloud environments with support from Partner Success.

Migrate cloud and on‑premise environments to Jotelulu faster and with less risk thanks to Assisted Migrations and extended Partner Success support hours.
Read article
Back to blog