温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.ms8.com/how-to-sort-files-by-size-in-linux
点击访问原文链接

How to Sort Files by Size in Multilevel Directories in Linux / macOS - Making Sense of the Infinite

How to Sort Files by Size in Multilevel Directories in Linux / macOS - Making Sense of the Infinite

Making Sense of the Infinite

Unlocking Infinite Possibilities Through Curiosity

December 14, 2024 How to Sort Files by Size in Multilevel Directories in Linux / macOS

Managing files on Linux often involves identifying the largest files within a directory and its subdirectories, particularly when disk space is at a premium. This article explains how to search for files in Linux and sort them by size, with the largest files appearing first. We’ll explore a few efficient command-line methods to achieve this.

Using find and ls for Detailed File Information The find command, combined with ls, is a powerful way to locate files and display their sizes in human-readable format. To search through a directory and its subdirectories while sorting files by size, use the following command:

find /path/to/directory -type f -exec ls -lh {} + | sort -k 5 -rhShellScript Explanation of the Command:

find /path/to/directory -type f: This searches for all files (not directories) in the specified path. -exec ls -lh {} +: This lists the files in a human-readable format (-lh) showing file sizes. sort -k 5 -rh: The -k 5 option sorts by the fifth column, which is the file size in the ls -lh output. The -r flag ensures the sorting is in descending order, with the largest files displayed first. The -h option enables human-readable size sorting (e.g., K, M, G). Using find and du for a Simpler Output If you prefer a cleaner output that focuses solely on file size and path, the following command is more appropriate:

find /path/to/directory -type f -exec du -h {} + | sort -rhShellScript Here’s what this command does:

The find command works similarly to the previous example, locating all files in the specified directory. du -h calculates the disk usage of each file and outputs it in a human-readable format. sort -rh arranges the files in descending order by size. Example Output:

2.0G /home/user/documents/large_file.iso 500M /home/user/documents/video.mp4 300K /home/user/documents/small_file.txt Practical Use Cases These commands are particularly useful in scenarios like:

Identifying large files that are consuming disk space. Cleaning up old or unnecessary files. Diagnosing disk space issues on Linux servers. For example, running these commands in /var/log can help locate oversized log files that might be bloating your server’s storage.

Sorting files by size on Linux is straightforward using the find command in combination with either ls or du. Depending on your needs, you can choose between a detailed view with file permissions and timestamps or a simpler output focused on size and path. These methods are invaluable for system administrators and power users seeking to optimize disk usage.

Related Posts Using LFTP and Dynamic Variables to Backup Files to a Remote FTP Server Run Remote Commands with SSH and Certificate Authentication on Linux / macOS Terminal Different Use Cases of Bash and EXP (Expect) Using the RouterOS API to Bulk Reboot MikroTik Router Devices Remotely Using cURL to Upload and Download Files via the SFTP Protocol Code Shell Script Bash Sort Files

Last revised on

December 21, 2024 ←Nvidia, AMD, Intel, and Arm: Understanding the Unique Strengths of Four Chip Giants Resolving Yum Update Issues in CentOS 8: Causes and Fixes→ Comments Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Website

Δ

More posts Model Context Protocol February 26, 2026 Faraday Future: A Persistent Scam December 9, 2025 Afeela: What Brought Honda and Sony Together? December 8, 2025 Loop Quantum Gravity, LQG November 15, 2025 Search

Tags:

Ad-Blocking Administrator Privileges Algorithm Application APT-Get Install Artificial Intelligence Artificial Intelligence Generated Content Bash Certificate File Cloudflare Code Command Line Concept Cryptocurrency Decentralization Developer Digital Certificate DNS over HTTPS DNS Resolver Domain Name Resolution Domain Name System Economic Encrypt Finance Firmware Formula Google Hardware Homebrew Home Lab Home Network Hypertext Transfer Protocol Secure Internet Investment iOS IPv6 Linux Machine Learning macOS Mathematics Microsoft Windows MikroTik Network Network Attached Storage Network File System Networking Network Management Network Security Network Service Network Switch Nginx NVIDIA Open Source Operating System Opinion Optimization Paradox Philosophy Physics Popular Science PowerShell Prediction Privacy Programming Language Proxy Server Python Quantum Computing Redundant Array of Independent Disks ROS Route Router RouterOS Routing Science Explained Secure Sockets Layer Security Shell Script Small Office Home Office Software SSH System Administration System Management Technology Terminal Theory Ubuntu Universe Unlocking Virtual eXtensible Local Area Network Virtualization Virtual Local Area Network Virtual Private Network VXLAN Web Web Server Wi-Fi WinBox Windows 11 Windows Server WireGuard

Making Sense of the Infinite

Proudly powered by WordPress

智能索引记录