PDA

View Full Version : One-liners


SirDice
December 10th, 2008, 20:38
The things that make *nix famous. Crazy one-liners :e

Couple ones I found quite useful:


# tar -C /usr -cf - home | tar -C /storage/export -xvf -

Why not use cp or mv to move /usr/home to /storage/export/home? Weird things happen to hard and softlinks when you mv or cp. Try it and remember that a mv between different filesystems is actually a copy and delete.

A little variation on the theme:

# tar -C /usr -cf - home | ssh user@somemachine tar -C /storage/export -xvf -

But user@somemachine must have write access of course, so sometimes it's easier to pull instead of push:

# ssh user@somemachine tar -C /usr -cf - home | tar -C /storage/export -xvf -


You may want to add the -z switch to the tar commands. It will add compression but it depends on the type of data and your connection speed if it really improves transfer speeds.

vermaden
December 11th, 2008, 04:36
For ssh options, add -C for slow connections (compresion) and -c blowfish so it will not use that much CPU time on slower machines.

hemi
December 17th, 2008, 20:21
One of my personal favorites is one I worked up while trying to find a better font for my xterms. This will spew a bunch of xterms on to your screen (one for every monospace, scalable font recognized by X or whatever) with 'man ls' as the contents.

fc-list :spacing=mono:scalable=true family | sort | tr '\n' '\0' | xargs -0 -I FONT echo xterm -fg white -bg black -cr orange -T \'FONT\' -fa \'FONT\' -fs 12 -e man ls \& > xterm_test; sh xterm_test

estrabd
January 5th, 2009, 18:52
Recursive search and replace in text file:

find . -name "*.txt" -type f -print | xargs perl -pi -e 's/xxx/aaa/g'


Rename files (and dirs):

find . -print | perl -ne 'chomp; next unless -e; $oldname = $_; s/aaa/bbb/; next if -e; rename $oldname, $_'


Zip up a directory and everything in it:

find ./dir_to_zip -print | xargs zip dir.zip


Summary: find and xargs are cool extremely useful.