Archive for March, 2008

Handy Shell Commands

Thursday, March 20th, 2008

I thought I would share some commands and small single line bash scripts I have found useful in the past:

AWK

Output every 50th line from a file into a new file :

cat infile.csv | awk ' (NR % 50) == 0 { print $0 } ' > outfile.csv

Print the 13th column (space delimited):

awk ' { print $13} '

Print the 13th column (colon delimited):

awk -F":" '{ print $13 }'

Find

Recursive Grep (starting from current directory):

find . -name "*" -exec grep -i "searchValue" {} /dev/null \;

Compare files in two directories (-N for new files, -a for forcing ASCII, and -r for recursing subdirectories):

diff -Nuar dir1 dir2

Find all files under /dir older than 7 days, and delete them:

find /dir -type f -mtime +7 | xargs rm -f

Do a global search and replace from “html” to “shtml” across all html files in the current directory and all subdirectories:

find . -name "*.html" -exec perl -pi -e 's/\.html/\.shtml/g' {} \;

Rename all html files to shtml files in the current directory and all subdirectories:

find . -name "*.html" | while read f
do
mv ./"$f" "${f%html}shtml";
done

Find the largest files and directories on your hard disc (starting from the current directory):

du -k * | sort -nr | more

List Hardware in your machine:

sudo lshw