I have found the ‘xargs’ command line utility to be very useful sometimes. Often you can avoid writing a special shell script to perform a task by using xargs to perform a task on a list of files. Its use can best be described through an example. I have recenlty be migrating files from one svn repository to another. I began by copying all of the files over using rsync. This copied all of the .svn files from the old repository, which I didn’t want. This left me with at least 30 directories, each with a .svn directory that needed to be removed. What to do? Use xargs. Here is the command:
command-prompt> find -name .svn | xargs rm -rf
This uses the find command to generate a list of all files and directories names ‘.svn’ (case sensistive). This list is then piped to the xargs command, which runs ‘rm -rf’ on each file in the list. That’s it! Note that as this command is written it will seach the current directory and all subdirectories for files with name ‘.svn’. If you want to have ‘find’ search a directory other than the current directory you can add the path of that directory after the file name to search for. See the man page for find for more info.
Jeff,
Your use of xargs was a life saver for me a few days ago. But, since then I have learned a few more tricks, and have some tips to make your tip more robust and predictable.
1) The first argument for find needs to be the directory you want find to look in, i.e., the example should be
% find /path/to/folder -name .svn | xargs rm -rf
2) Using a pipe to xargs as you have done will not work on directories with spaces in their names. e.g., if you have a directory
/path/to/some dir/a
the pipe to xargs will break it up and then xargs will try to execute
rm -rf /path/some
and
rm -rf dir/a
This could lead to disastrous consequences!
Because of 2), it is probably better to use finds own built in exec:
find /path/to/folder -type d -name .svn -exec rm -rf {} ;
This will search out only directories named .svn and remove them, without having to worry about spaces in names.