One-liners

SirDice

Administrator
Staff member
Administrator
Moderator
The things that make *nix famous. Crazy one-liners :e

Couple ones I found quite useful:

Code:
# 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:
Code:
# 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:
Code:
# 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.
 
  • Like
Reactions: a6h
For ssh options, add -C for slow connections (compresion) and -c blowfish so it will not use that much CPU time on slower machines.
 
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.

Code:
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
 
Recursive search and replace in text file:
Code:
find . -name "*.txt" -type f -print | xargs perl -pi -e 's/xxx/aaa/g'

Rename files (and dirs):
Code:
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:
Code:
find ./dir_to_zip -print | xargs zip dir.zip

Summary: find and xargs are cool extremely useful.
 
Back
Top