Wednesday, November 22, 2006

UNIX tips for the day

(1) How to run a shell script on a remote machine without copying the script out?
Very simple and obvious, but most people dont do this.
paddu:~$ cat | ssh remote_host /bin/sh

(2) How do I copy a directory structure from one machine to another?
paddu:~$ tar cf - local_dir | ssh remote_host "( cd abs_dest_dir ; tar xf - )"

tar cf - tars and writes to stdout, which is piped to stdin of the next command. The next command ssh's to the remote machine and changes dir to the specified destination directory. And lastly, tar xf - extracts the tar file by reading from stdin

(3) How to change every occurance of a string in multiple files from the command line?
Perl pie!
paddu:~$ perl - p -i -e 's/orig_string/new_string/g' *.cpp

(4) Give me a quick way to recursively delete all empty directories under a given directory structure
paddu:~$ find -d . -print0 | xargs -0 rmdir 2> /dev/null

The find will do a DFS first, and then rmdir will prune only the empty directories



Post a Comment

<< Home