Some useful one line unix scripts

 

Display processes in order of desending paging space utilization (VSZ).

ps -efo "%p %z %a" | sort -r +1 -2 | more

 

Create a 1GB file and check how long it takes

time dd if=/dev/zero of=bigfile bs=1024 count=1000000

 

View the contents of a file without the commented out and blank lines.

cat somefile | egrep -v "^#|^$"

 

Move files from one directory to another without getting the "Argument list too long" error.

In this case we will move gzip files from /opt to /archive

cd /opt
find . -name "*gz" | xargs -i -t mv {} /archive/

 

Replace multiple whitespace (spaces, tabs) with a single whitespace (space).

In this case /tmp/ps.out contains the output from ps -ef with multiple whitespace between columns and /tmp/ps.space will contain the same ps -ef output but with only one space between columns. This is useful if you then want to replace whitespace with commas, with multiple spaces you will end up with multiple commas between columns instead of one comma.

sed "s/^ *//;s/ *$//;s/ \{1,\}/ /g" /tmp/ps.out > /tmp/ps.space 

 

Replace whitespace (spaces, tabs) with a comma

If you want to replace multiple whitespace with a single comma then first run the command above.

sed -e 's/\s/,/g' /tmp/ps.space > /tmp/ps.comma