Skip to content

Instantly share code, notes, and snippets.

@mzaman
Created August 5, 2025 02:40
Show Gist options
  • Select an option

  • Save mzaman/d8c96f876e572b10d2a5495f1d4848fd to your computer and use it in GitHub Desktop.

Select an option

Save mzaman/d8c96f876e572b10d2a5495f1d4848fd to your computer and use it in GitHub Desktop.
File Size Sorter (macOS Terminal)

File Size Sorter (macOS Terminal)

This script allows you to list files and folders recursively from the current directory, sort them by size in descending order, and display the file sizes in a human-readable format (KB, MB, GB, etc.) in the macOS Terminal.

Table of Contents

Introduction

This tool uses the find, ls, and sort commands in the macOS Terminal to list all files and folders in the current directory and subdirectories, sorted by size. The sizes are displayed in a human-readable format (KB, MB, GB, etc.), and it can be customized for different use cases.

Usage

Basic Command

To list all files recursively from the current directory and sort them by size, use this command:

find . -type f -exec ls -lh {} + | sort -k 5 -h -r

Sorting by Size (Largest First)

To display files sorted by size with the largest files first, use:

find . -type f -exec ls -lh {} + | sort -k 5 -h -r

Explanation:

  • find . -type f: Searches for files recursively starting from the current directory.
  • -exec ls -lh: Lists the file details with sizes in human-readable format (KB, MB, GB, etc.).
  • sort -k 5 -h -r: Sorts the files by size (5th column), in human-readable format (-h), in reverse order (-r) to show the largest files first.

Displaying Human-Readable Sizes

The -h flag in ls -lh ensures that the file sizes are displayed in a human-readable format, making it easy to see file sizes in KB, MB, GB, etc.

Customizing the Command

You can customize the command as needed:

  • Change the starting directory: Replace the . (current directory) with a path, e.g., /path/to/directory.
  • Filter by file type: Use -type f for files or -type d for directories.
  • List files larger than a specific size: Use -size +100M to list files larger than 100MB, for example.

Examples

Example 1: List all files in the current directory and subdirectories, sorted by size (largest first)

find . -type f -exec ls -lh {} + | sort -k 5 -h -r

Example 2: List files in a specific directory and sort by size

find /path/to/directory -type f -exec ls -lh {} + | sort -k 5 -h -r

Example 3: List files larger than 1GB in size

find . -type f -size +1G -exec ls -lh {} + | sort -k 5 -h -r

Example 4: List directories sorted by their total size

find . -type d -exec du -sh {} + | sort -h -r

Conclusion

This tool is helpful for managing large datasets or identifying the largest files and directories within your project or file system. It can assist with tasks like cleaning up disk space, organizing files, or analyzing file usage patterns.

Feel free to modify and use this script to suit your specific needs!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment