vastutsav / command-line-quick-reference

quick reference on command line tools and techniques for the people with limited time
The Unlicense
359 stars 39 forks source link

Suggested One liners #16

Closed vastutsav closed 2 years ago

vastutsav commented 2 years ago

du -ah / 2>/dev/null | sort -hr | less

du is in the GNU coreutils, so it’s installed on most distros. It returns a list of all directories in the specified directory (/ in this case) with their disk usage, tallied up at the end as well.

-a makes it return files as well.

-h makes it return in human readable format (7.2G instead of 7200000000).

sort -hr sorts it based on size (-human readable and -reversed)

less lets you scroll through the output.


-r and -I options for grep searches recursively in the path you give it and ignores binary files, respectively:

$ grep -inrI 'some text' /path/to/dir /path/to/dir/file1:10: this has some text on line 10 /path/to/dir/other_file:2: list of awesome textiles: /path/to/dir/subdir/.hidden_file:537: sOme TExt ThaT is HidDEn In bash, using $(< /path/to/file) instead of $(cat /path/to/file) when you want the contents of a file as a variable. The first form is faster according to bash(1).

Also in bash, arithmetic expansion using $((...)):

$ var1=1 $ var2=2 $ echo $((var1 + var2)) 3 Slight nitpick, my understanding is that $(command) syntax is preferred over command syntax these day. It flows better with the rest of the language. Plus you can nest them nicely with $(command1 $(command2)).