Blog
Linux Tips - Use DUCKS to get a concise list of files and sizes
The Linux du (disk usage) command will output an entire directory tree and every file therein, along with a list of sizes for each file in bytes. Its usefulness can be minimized if you don't know what you're doing with it though. First of all, unless you're dealing exclusively with log or configuration files, filesizes in terms of bytes is not very useful. With your average mp3 file containing 3-5 million of them, across hundreds of files or more these numbers add up quickly. Second, unless you need the output to feed into some other sort of parsing program or script, the information returned is going to whiz by faster than you can conceivably read it, since du looks inside every folder recursively. Sometimes you actually want this data, but in some applications the amount of data you receive back is akin to drinking from a firehose. What do we do about this?
du -cks * | sort -n
Great, another command. What does it do? Let's break it down:
- du is the disk usage command.
- -cks are the arguments as follows:
- c displays a neat summary at the bottom of the list that tells you the total amount of space used.
- k displays the filesizes in kilobytes instead of bytes. You could change this to m if you wanted items returned in megabytes, but that ruins the cute acronym (""ducms"" doesn't have quite the same ring to it!)
- s is what stops the recursive behavior of du. Instead of going into every single folder and listing all the files and sizes, it'll just tell you the size of the folder.
- * tells the command to select all files in the current directory. Failing to specify this will default to root, which is probably a lot more information than necessary! You can change this to other values, such as ~/* to get a breakdown of what's in your home directory.
- | is the ""pipe"" character that tells Linux to ""pipe"" the output of the command in front of it directly into the command that comes after it.
- sort -n is just a sorting command that sorts a set of input (in this case, the result of du -cks) in ascending order by number.
This should help you get a handle on managing files based on size in your filesystem. If you're feeling ambitious, you could even set up an alias to perform this command...something like:
alias ducksort=""du -cks * | sort -n""