Compression and Extraction
# unrar all .rar file in current directory
# o[+|-] Set the overwrite mode
find . -name "*.rar" -exec rar e -o+ '{}' ';'
# unrar .rar file with $PASSWORD
# x - with directory
rar x -p$PASSWORD *.rar
# .tar.bz2 file extraction
tar -xvf fileName.tar.bz2
# create tar.gz or tar.bz2
# http://www.tecmint.com/18-tar-command-examples-in-linux
tar cvzf MyImages-14-09-12.tar.gz /home/MyImages
tar cvjf Phpfiles-org.tar.bz2 /home/php
find . -name 'file-A' | tar czf archive.tgz -T -
find . -name 'file-A' | tar cjf archive.tbz -T -
# use zip to compress files, -r for recursive
zip -r archive.zip archive
find . -name 'file-A' | zip -@ archive.zip
File/Folder Management
# remove all files/directories but file.txt
shopt -s extglob
rm -rf !(file.txt)
# copy remote files/folder to local machine
ssh remote-sys 'tar cf - Documents' | tar xf -
Start Service Upon Boot Up
# configure the server to start NGINX upon reboot
chkconfig nginx on
Backup
# back up /etc /home and /ust/local to /media/backup
# --delete to delete files not exist in source from /media/backup
rsync -av --delete /etc /home /usr/local /media/backup
# back up to remote machine
rsync -av --delete --rsh=ssh /etc /home remote-sys:/backup
Regular Expression
# . - indicate any character
# ^ - the begining anchor
# $ - the ending anchor; ^$ matches blank lines
# [^c] - negation; works only at 1st character; eg, grep -h '[^bg]zip' file.txt
# [A-Z] - range A to Z; eg, grep -h '^[A-Za-z0-9]' file.txt
# ^(gz|bz|zip) - start with gz, bz or zip; eg, grep -Eh '^(gz|bz|zip) file.txt
# ^bz|gz|zip - start with bz or contains gz or contains zip, different from above
# ? - match an element zero or one time
# + - match an element one or more times
# {n,m} - match the preceding element n to m times
# phone number matching
echo "(555) 123-4567" | grep -Eh '^\(?[0-9][0-9][0-9]\)? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$'
echo "555 123-4567" | grep -Eh '^\(?[0-9][0-9][0-9]\)? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$'
echo "(555) 123-4567" | grep -Eh '^\(?[0-9]{3}\)? [0-9]{3}-[0-9]{4}$'
echo "555 123-4567" | grep -Eh '^\(?[0-9]{3}\)? [0-9]{3}-[0-9]{4}$'
# complete sentence matching
echo "This works." | grep -Eh '[[:upper:]][[:upper:][:lower:] ]*\.'
# match lines consisting of groups of one or more alphabetic
# characters saparated by single spaces
echo "a b c" | grep -Eh '^([[:alpha:]]+ ?)+$'
Sort
# sort by file size
ls -l /usr/bin | sort -nr -k 5 | head
# multiple fields sorting, filed 1 then field 2
sort --key=1,1 --key=2n file.txt
# within same field sorting (different position)
# eg, Fedora 10 11/25/2008
sort -k 3.7nbr -k 3.1nbr -k 3.4nbr file.txt
# separated by other character(not space nor tab, like ':')
sort -t ':' -k 7 /etc/passwd | head
# uniq
sort -u file.txt
sort file | uniq
Cut
# extract fields from file, each fileds should be separated by Tab(^I)
# then extract substring from position 7 to 10
cut -f 3 file.txt | cut 7-10
# separated by other character rather than Tab(like ':')
cut -d ':' -f 1 /etc/passwd | head
Paste
# oppsite to Cut, add one or more column of text to a file
paste file_1.txt file_2.txt
Sed
# -n no autoprint option
# print lines where there are no SUSE
sed -n '/SUSE/!p' file.txt
# print 1 to 3 lines
sed -n '1,3p' file.txt
# MM/DD/YYYY to YYYY-MM-DD Date Format
sed 's/\([0-9]\{2\}\)\/\([0-9]\{2\}\)\/\([0-9]\{4\}\)$/\3-\1-\2/' file.txt