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.
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.
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 -rTo display files sorted by size with the largest files first, use:
find . -type f -exec ls -lh {} + | sort -k 5 -h -rExplanation:
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.
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.
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 ffor files or-type dfor directories. - List files larger than a specific size: Use
-size +100Mto list files larger than 100MB, for example.
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 -rfind /path/to/directory -type f -exec ls -lh {} + | sort -k 5 -h -rfind . -type f -size +1G -exec ls -lh {} + | sort -k 5 -h -rfind . -type d -exec du -sh {} + | sort -h -rThis 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!