Skip to main content

Check Disk Usage of Files and Directories

Basic Methods

In Linux, you can use the following commands to check the disk space used by a file or directory:

  1. du command (disk usage):

    • Check the space used by a directory:

      du -sh /path/to/directory

      Option explanations:

      • -s: Show only the total
      • -h: Display in human-readable format (e.g., KB, MB, GB)
    • Check the space used by a file:

      du -sh /path/to/file
  2. ls command (list directory contents):

    • Check the size of a single file:
      ls -lh /path/to/file
      Option explanations:
      • -l: Use long format to list file information
      • -h: Display file size in human-readable format
  3. stat command (display file or file system status):

    • View detailed information about a file or directory, including size:
      stat /path/to/file_or_directory

Additional du Usage

  1. Check the total size of a directory:

    du -sh /path/to/directory

    Here, -s means summary and -h means display in human-readable format (e.g., KB, MB).

  2. Check the size of a directory and its subdirectories:

    du -h /path/to/directory

    This will list the size of each subdirectory under the specified directory.

  3. Check the total size of a directory and its subdirectories, sorted by size:

    du -ah /path/to/directory | sort -h

    Here, -a means show all files and directories, and sort -h means sort in human-readable format.

  4. Show only the top N largest subdirectories and files:

    du -ah /path/to/directory | sort -rh | head -n N

    Here, -r means reverse order and head -n N means take the first N lines.