################################ ## Additional Shell Functions ## ## By: Thiam H Lee ## ## Last Updated: Jun 29 2009 ## ################################ ## Find a file with a regular expression pattern in name: findfile () { if [ $# -eq 0 ]; then echo "findfile: Runs the find command with the given regular expression."; echo ""; echo "Usage: findfile "; echo ""; echo "Example: findfile '.*\.txt'"; echo " searches the directory tree rooted at the current directory"; echo " for all files ending with '.txt'"; echo "" return 1 fi if [ $# -ne 1 ]; then echo "findfile: Exactly 1 argument is required"; return 1 fi find . -type f -regex $* -ls; } ## Find filenames with regular expression pattern $1 and run $2 on it: findfileandrun () { if [ $# -eq 0 ]; then echo "findfileandrun: Executes the find command with the given regular expression"; echo " and runs the given command on each of the matches"; echo ""; echo "Usage: findfileandrun "; echo ""; echo "Example: findfile '.*\.txt' cat"; echo " searches the directory tree rooted at the current directory"; echo " for all files ending with '.txt' and runs the command 'cat'"; echo " on each matched file."; echo "" return 1 fi if [ $# -ne 2 ]; then echo "findfile: Exactly 2 arguments are required"; return 1 fi find . -type f -regex $1 -exec "${2:-file}" {} \; ; } ## Get host info hostinfo () { echo -e "\nHostname: $HOST" echo -e "\nAdditionnal information: " ; uname -a echo -e "\nUsers logged on: " ; w -h echo -e "\nCurrent date : " ; date echo -e "\nMachine stats : " ; uptime echo -e "\nMemory stats : " ; free echo } ## Encode/Decode rot13 messages rot13 () { if [ $# -eq 0 ]; then tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]' else echo $* | tr '[a-m][n-z][A-M][N-Z]' '[n-z][a-m][N-Z][A-M]' fi } ## Compress files roll () { if [ $# -eq 0 ]; then echo "roll: Compress files"; echo ""; echo "Usage: roll [compressed-file] [files ...]"; echo "Supported compression formats: .tar .tar.bz2 .tar.gz .tbz .tgz .zip"; echo ""; echo "Example: roll example.tar.gz file1 file2 file3"; echo " compresses file1, file2 and file3 -> example.tar.gz"; echo "" return 1 fi FILE=$1 case $FILE in *.tar) shift && tar cvf $FILE $* ;; *.tar.bz2) shift && tar cvjf $FILE $* ;; *.tar.gz) shift && tar cvzf $FILE $* ;; *.tbz) shift && tar cvjf $FILE $* ;; *.tgz) shift && tar cvzf $FILE $* ;; *.zip) shift && zip -r $FILE $* ;; *) echo "${FILE} : Unsupported compression format" ;; esac } ## Swap two files swap () { if [ $# -ne 2 ]; then echo "swap: 2 arguments needed"; return 1 fi if [ ! -w $1 ]; then echo "swap: $1 does not exist or is not writable"; return 1 fi if [ ! -w $2 ]; then echo "swap: $2 does not exist or is not writable"; return 1 fi local TMPFILE=tmp.$$ ; mv $1 $TMPFILE ; mv $2 $1 ; mv $TMPFILE $2 } ## Uncompress file unroll () { if [ $# -eq 0 -o $# -ge 2 ]; then echo "unroll: Uncompress file to the present working directory"; echo ""; echo "Usage: unroll [compressed-file]"; echo "Supported formats: .bz2 .gz .rar .tar .tar.bz2 .tar.gz .tbz .tgz .zip"; echo ""; echo "Example: unroll example.tar.gz"; echo " uncompresses example.tar.gz into the"; echo " present working directory."; echo ""; return 1 fi FILE=$1 if [ -f $FILE ]; then case $1 in *.rar) unrar x $FILE ;; *.tar) tar xvf $FILE ;; *.tar.bz2) tar xvjf $FILE ;; *.tar.gz) tar xvzf $FILE ;; *.bz2) bunzip2 $FILE ;; *.gz) gunzip $FILE ;; *.tbz) tar xvjf $FILE ;; *.tgz) tar xvzf $FILE ;; *.zip) unzip $FILE ;; *) echo "$FILE : Unsupported compression format" ;; esac else echo "$FILE does not exist or is not a valid file" fi }