phaqphaq

“a geeks daily life”

Archive for the 'Shells' Category

wipe out directory tree through rsync

Wednesday, December 13th, 2006

To wipe out a directory tree on a rsync server (an export) which you don’t have shell access to (but proper write access of course), just use this command:

# rsync -av –delete /tmp/clear.me/ rsync://some.rsync.host/some.export/

/tmp/clear.me is an example for a local but empty directory (empty as in ‘mkdir /tmp/clear.me).

Putting the slashes at the end of both local and remote path will allow you to sync the empty directory onto the server, effectively removing everything on the receiver side.

Find And Replace Strings

Friday, October 13th, 2006

Imagine you have a directory with thousands of text files, each of which containing some keywords to be replaced.

While there’s a lot of tools out there allowing you to find and replace strings in text files, there’s always a goog reason to use the tools that you already have.

This snippet uses find in combination with perl to achieve the task:

#find /my/path -type f -exec perl -i -p -e ’s/search/replace/g;’ {} \;

This command line instructs to find all files within /my/path, passing them to perl, which is instructed to find and replace given strings instantly.

Rememver however that this is not binary sage so you should run this only on directories containt text files.

The get additional verbosity add the ‘-print’ flag to find’s command line to see which files is being worked on.

If you pass an optional extension to ‘-i’ (eg. ‘-i.bak’) perl is instructed to create a backup by the same filename plus extension.

As always: man find and man perl are your friends.

Enjoy!