Yes, you can check how much space files and folders are taking up directly from your terminal. This is achieved using the du (disk usage) command combined with grep or sort to filter and read the output. This method is faster than opening file managers and provides precise, human-readable statistics for every directory on your system.

What You'll Need

You need a computer with an operating system that includes a Unix-like shell. This includes macOS, Linux, and Windows 10/11 with WSL (Windows Subsystem for Linux) enabled. No external software is required; the tool is built into the core system.

  • A terminal or command prompt application.
  • Basic keyboard navigation skills (typing commands).
  • Administrative privileges (sudo) may be required for some directories.

Step-by-Step

  1. Open your terminal. On macOS or Linux, you can usually find this in the Applications menu under Utilities or by right-clicking the desktop and selecting Open in Terminal. On Windows, open the Start menu, search for PowerShell or Command Prompt, or launch WSL.
  2. Change to the target directory. To analyze a specific location, use the cd (change directory) command followed by the path. For example, to check your home folder, type cd ~. To check a folder named Downloads, type cd ~/Downloads. The ~ symbol represents your user's home directory path.
  3. Run the basic disk usage command. Type du and press Enter. This will display the disk usage in kilobytes (KB) for the current directory and all its subdirectories. The output can be very long and difficult to read because it lists every single file.
  4. Human-readable output. To make the numbers easier to understand, add the -h (human-readable) flag. Type du -h. The output will now show sizes like 4.0K, 1.2M, or 3.4G, where K is kilobytes, M is megabytes, and G is gigabytes.
  5. Check only the current directory. By default, du recurses into subfolders. If you only want to know the size of the files in the folder you are currently standing in, use the -s (summarize) flag. Type du -sh .. The dot (.) represents the current directory. This command gives you one single line of text showing the total size of the current folder.
  6. Find the largest folders. To sort the output by size, pipe the command to sort and use the -rh flags. Type du -sh * | sort -rh. This lists all folders and files in the current directory, sorted from the largest (top) to the smallest (bottom). This is the most common way to identify space hogs.
  7. Search for a specific directory name. If you are looking for a folder named Photos or Videos, you can filter the output immediately. Type du -sh * | grep -i 'photos'. The grep command looks for text matching the pattern, and -i makes the search case-insensitive.
  8. Exclude hidden files. Directories starting with a dot (like .config) are hidden. To ignore them, use the --exclude flag. Type du -sh --exclude='.*' * | sort -rh. This ensures you are only counting files you can see in standard file explorers.
  9. Check a parent directory from a deep path. You do not need to cd into the folder to check it. You can specify the path directly. For example, if you are in your Downloads folder, type du -sh ~/Pictures to check the size of your Pictures folder without navigating there.
  10. Handle permission errors. If you see Permission denied for certain files or folders, it means your user account does not have read access. You can try running the command with sudo (superuser do) at the beginning, but use this with caution as it grants full system access. Type sudo du -sh * | sort -rh.

Common Mistakes

  • Forgetting the dot (.): When using the summarize flag (-s), you must specify the target. If you type du -sh without a path, the command will hang or return an error because it does not know where to start counting.
  • Ignoring hidden files: Users often assume a directory is empty but find it is full because they missed hidden dotfiles. It is good practice to run the --exclude command if you specifically want to ignore these.
  • Sorting without human-readable format: If you run sort -rh on raw kilobyte data, the computer will sort numerically, which is incorrect. Gigabytes (1,000,000,000) will appear before Megabytes (1,000,000). Always use du -h before sorting.
  • Stopping processes: The du command is not a background process. If you run a recursive du on a massive drive, it will consume all available CPU and RAM. If the system becomes unresponsive, you must open the terminal again and kill the process using Ctrl + C.

Tips

  • Use find for specific files. If you need to find a specific file by name, use find instead of du. The command find . -name 'largefile.zip' will locate the file, and you can then use ls -lh to see its size.
  • Monitor progress on slow drives. On older hard drives, recursive commands can take a long time. Adding the -d (max depth) flag limits how many levels deep it looks. For example, du -sh -d 2 will only check the current folder and its immediate subfolders.
  • Combine commands for power users. You can search for a specific size range. For example, to find folders larger than 100MB, type du -sh * | awk '$1 > "100M"'. This uses awk to parse the text output and filter it based on your criteria.
  • Check the root partition. To see how full your main hard drive is, use the path /. Type df -h to get a quick summary of all mounted drives, including the root partition /.

Troubleshooting

If you run a command and nothing happens, or if you get an error message, check these common issues first.

  • No output is returned. This usually means the directory is empty. If you expected files to be there, double-check the path you typed. If you are in a folder called test and type du -sh test, but the folder is actually named Test (case-sensitive), you will get an error.
  • Permission denied. If you see this error frequently, you might be trying to analyze a system directory like /usr or /bin. These are protected for security reasons. You must use sudo to run these commands, but be careful not to delete system files with the rm command.
  • Command not found. If you type du and get an error saying the command is not found, your system shell is not configured correctly. This is rare on standard Linux or macOS installations but can happen on stripped-down distributions or custom builds.
  • System hangs. If the command runs but your mouse and keyboard stop responding, the command has likely entered an infinite loop or is consuming too many resources. Press Ctrl + C immediately to stop it. Try running the command with a lower depth limit to reduce the number of files it scans.

Frequently Asked Questions

What does the 'd' flag do?

The '-d' flag sets the maximum depth of the directory traversal. It tells the command to stop searching inside subdirectories after a certain number of levels. For example, du -d 1 will only show the size of the immediate folders, without listing the contents of those folders.

How do I check disk usage on a specific drive letter?

On Linux and macOS, drives are usually mounted at directories like /mnt/data or /Volumes/Backup. You can check these paths directly with the du command. On Windows, it is easier to use the dir command or the graphical File Explorer, as the command line path for drives can be less intuitive.

Why are my numbers different from the file manager?

The du command counts the actual disk blocks used by the file, which can be slightly less or more than the file size listed by the operating system. Additionally, if a file is sparse (has empty sections), du will count only the space actually occupied on the disk, which can appear much smaller than the logical file size.

Can I sort by file count instead of size?

Yes, but you must combine the commands. You can use find . -type d -print0 | xargs -0 | wc -l to count the number of directories, or use find . -type f | wc -l to count the number of files. du does not have a built-in mode to sort by file count.

Is there a limit to how many files du can handle?

There is no strict limit, but performance will degrade significantly on drives with millions of files. If you are analyzing a database or a massive media library, the command may take hours to complete. In these cases, it is better to analyze specific subfolders rather than the root of the drive.

What is the difference between df and du?

The difference is the scope. df (disk free) shows the total amount of free space available on the entire drive or partition. du (disk usage) shows how much space is used by a specific directory or file. You use df to see if you are running out of space, and du to find out where that space is being used.