Some useful linux commands

A few useful linux commands and their explanations.

Recursively find in files…

find . -name \*.php -exec grep -inH discount {} \;

This finds all files ending with .php (-name \*.php) starting in the current path (.) and recurse through all subdirectories, then search (grep) case-insensitive (i) within those files ({}) for the word “discount” and outputs the filename (H), line number (n), and the line for any matches.

Find all files older than 14 days…

find /home/*/Maildir/.Junk/cur -type f -mtime +14 -exec ls {} \;
find /home/*/Maildir/.Trash/cur -type f -mtime +14 -exec ls {} \;

These two commands find files within system users’ Maildir .Junk and .Trash folders that are older than 14 days. You could change the “-exec ls” to “-exec rm -rf” to remove the files.

Find top 10 directories using disk space…

du -x --block-size=1024K | sort -nr | head -10

This finds and lists the top 10 directories using disk space in the current path. Useful if you’re trying to track down what is filling up your disk.

Create and extracting a tar.gz archive of a directory…

tar -czvf (output filename) (directory to archive)

e.g. tar -czvf myhome.tar.gz /home/myhome/

To extract this archive while preserving permissions, use:

tar --preserve -zxvf (filename)

Change permissions of directories and files…

find . -type d -exec chmod 755 {} \;

This finds all directories (indicated by the -type d) in the current path (.) and then chmods them to 755. If you wanted to recursively chmod files, you could issue this:

find . -type f -exec chmod 644 {} \;

This is really for my own reference, but you may find it useful.

One thought on “Some useful linux commands

  1. Pingback: Bookmarks about Commands

Comments are closed.