miércoles, 18 de febrero de 2009

Bash snippets

This entry is just for storing some useful bash snippets that I may forget...
I'll keep it updated when I find more.

Some of this snippets would do nice if used as aliases ^^

Show the disk usage of the files and directories, sorted by size (useful when cleaning the disk)
du -ms * | sort -nr | $PAGER
If you want it to show all the files in the directory tree...
du -m * | sort -nr | $PAGER

Search all the coincidences of a regular expression (POSIX) recursively in all the files and display the line and context where they were found.
grep --color -nIHr . -e REGULAR_EXPRESSION
Excluding backups, hidden files/directories (beware of excluding "." and ".."), etc...
grep --color --exclude=*[~#] --exclude-dir=.??* -nIHr . -e REGULAR_EXPRESSION

Search and replace a string of text in all the files of a directory tree recursively...
find . -type f -exec sed -i 's/STRING1/STRING2/' {} \;

Archive all the files that haven't been modified within the last 60 days
find . -type f -mtime +60 | xargs tar -cvf `date '+%d%m%Y'_archive.tar`

Create a patch for all the changes between two directories.
diff -urNp OLD_DIRECORY NEW_DIRECTORY > file.patch

Download all the files ending with "PREFIX" found recursively (through 10 levels) from www.example.com
wget -r -l 10 -A PREFIX http://www.example.com/

No hay comentarios: