Linux Commands

This is a virtual notepad for tracking useful Linux commands. I don’t use the command line enough to keep this stuff in my head.

Count files in a directory and it’s sub-directories

ls -1R | wc -l

Find largest files


If you want to find and print the top 10 largest files names (not directories) in a particular directory and its sub directories

$ find . -printf ‘%s %pn’|sort -nr|head

To restrict the search to the present directory use “-maxdepth 1” with find.

$ find . -maxdepth 1 -printf ‘%s %pn’|sort -nr|head

And to print the top 10 largest “files and directories”:

$ du -a . | sort -nr | head

** Use “head -n X” instead of the only “head” above to print the top X largest files (in all the above examples)

Source: StackOverflow

Delete files and folders recursively

Be very cautious! The “f” ensures that you will not be prompted before it destroys all of the files and directories. There is no undo. Try running it without the “f” first too make sure that you’re in the right place.

rm -rf directory

Check the free space on the disk

The “h” stands for “human readable” and tells it to use KB, MB and GB rather than just bytes.

df -h

Search Log Files

Here’s an example of looking at the last 500 lines of a log file and searching for output from mod_pagespeed.

tail -fn 500 /usr/local/apache/logs/error_log | grep "pagespeed"

You can also search for items on a particular date. For example, February 29 at 14:xx:

cat /usr/local/apache/logs/error_log | grep "Feb 29 14"