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
- 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.
- 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, typecd ~. To check a folder namedDownloads, typecd ~/Downloads. The~symbol represents your user's home directory path. - Run the basic disk usage command. Type
duand 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. - Human-readable output. To make the numbers easier to understand, add the
-h(human-readable) flag. Typedu -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. - Check only the current directory. By default,
durecurses 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. Typedu -sh .. The dot (.) represents the current directory. This command gives you one single line of text showing the total size of the current folder. - Find the largest folders. To sort the output by size, pipe the command to
sortand use the-rhflags. Typedu -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. - Search for a specific directory name. If you are looking for a folder named
PhotosorVideos, you can filter the output immediately. Typedu -sh * | grep -i 'photos'. Thegrepcommand looks for text matching the pattern, and-imakes the search case-insensitive. - Exclude hidden files. Directories starting with a dot (like
.config) are hidden. To ignore them, use the--excludeflag. Typedu -sh --exclude='.*' * | sort -rh. This ensures you are only counting files you can see in standard file explorers. - Check a parent directory from a deep path. You do not need to
cdinto the folder to check it. You can specify the path directly. For example, if you are in yourDownloadsfolder, typedu -sh ~/Picturesto check the size of your Pictures folder without navigating there. - Handle permission errors. If you see
Permission deniedfor certain files or folders, it means your user account does not have read access. You can try running the command withsudo(superuser do) at the beginning, but use this with caution as it grants full system access. Typesudo du -sh * | sort -rh.
Common Mistakes
- Forgetting the dot (.): When using the summarize flag (
-s), you must specify the target. If you typedu -shwithout 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
--excludecommand if you specifically want to ignore these. - Sorting without human-readable format: If you run
sort -rhon raw kilobyte data, the computer will sort numerically, which is incorrect. Gigabytes (1,000,000,000) will appear before Megabytes (1,000,000). Always usedu -hbefore sorting. - Stopping processes: The
ducommand is not a background process. If you run a recursiveduon 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 usingCtrl + C.
Tips
- Use
findfor specific files. If you need to find a specific file by name, usefindinstead ofdu. The commandfind . -name 'largefile.zip'will locate the file, and you can then usels -lhto 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 2will 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 usesawkto 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
/. Typedf -hto 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
testand typedu -sh test, but the folder is actually namedTest(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
/usror/bin. These are protected for security reasons. You must usesudoto run these commands, but be careful not to delete system files with thermcommand. - Command not found. If you type
duand 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 + Cimmediately 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.