Friday, January 30, 2009

The joy of xargs

I had a situation where I had a directory with about 230 subdirectories, comprising about 900GB. I had a number of 450GB drives I needed to back it up to, and I didn't want to have to do a 230 different cp commands. Enter xargs. I love xargs. You give it a source of input, and a command to run, and it will run the command, using the supplied input as arguments.

So for my 230 directories, I need to hand a certain number of those directories as an argument to cp. I decided to copy anything starting with a through i to one drive, j through r to another, and s through z to a third. From the source directory, you do this:

ls -d [a-i]* | xargs -J name cp -Rp name /path/to/backup/folder/

The -d flag is important, as that will make ls just return the name of the directory. The [a-i]* will match anything starting with a through i, followed by any number of characters. The -J lets you specify a replacement string. This takes the input supplied via the pipe, and for each line of input (in this case, the name of a directory), runs cp, replacing "name" with, in this case, the name of a folder.

And, of course, -Rp to recursively copy everything in those folders, preserving permissions, special attributes, ACLs, etc.

No comments:

Post a Comment