(My) Unix useful CLI commands

My own list of CLI commands, here for my own reference. If not stated otherwise they should run nicely in a FreeBSD box with the bash shell. I’m not a black screen guru, so beware this examples can be the most stupid way to do the things they are supposed to.

Operations with find.

Find changed files by date and copy them to their respective sub-directory.
Usually i use this to sync a development version of a website pushing the changes to the production one (normally when i changed a dozen of files). This has a shortcoming, it only works if the directory structure is the same, if there is a new directory with new files it won’t work on those files.

find ./ -type f -mtime -1 | xargs -I {} cp -pv {} /target/dir/{}

Find files with more than x days (example with 30 days below) in directory and subdirectories and delete them. This is immune to the dreaded “too many arguments” issue.

find /target/dir/* -type f -mtime +30 -exec /bin/rm -f {} \;

Replace string inside several files filtered by find patterns (it can be much easily done with deep but we loose all the filtering power of find)

find . -name “*.txt” -print|awk ‘{f=$0;sub(“domain.com.pt”,”domain.com”); 
print “mv “f” “$0}’|sh

Create a tar.gz of files modified in n (ex: 10) days

find . -type f -mtime -10 -print | xargs tar cvfz file.tar.gz

exclude some dirs (dir1 and dir2)

find . -type f -mtime -5 -print  | grep -v dir1 | grep -v dir2 | xargs tar cvfz file.tar.gz


This actually is a Perl script. It saved my ass countless times. Please go and grab it here. So, you can recursively do lots and lots of stuff. My main uses:

Find some string (case insensitive) in a js or php files.

deep find 'mystring' '*.php *.js' --case=0

Replace the string

deep replace 'oldstring' 'newstring' '*.php *.js' --case=0

Convert Windows end of lines CR+LF to Unix LF in a bunch (or one) files recursively down the directory tree

deep replace "\r" "" "*.html" --literal=0

Of course, if you really want some black magic voodoo CLI stuff just follow @climagic, most of the tweets are simply jaw dropping.

Disclaimer: if you are reading this, remember i can not be held accountable if you try some of this commands and ultimately start a chain reaction that destroys the entire known universe.