Using iteration to copy multiple unrelated files


I love Unix/Linux. Even after all these years, I still learn new tricks. Using iteration, one can copy multiple files from one place to another in a single command. You might say that's easy with wildcards, but what if the filenames have nothing in common? That's where iteration, using {this,that,other} comes in.

cp /etc/{passwd,group,hosts} /tmp/junk/

This will copy all three files, which are all in the same place but have names with nothing in common, to the same destination. Yes, you could cd to the directory first or do a for-loop. But it's always good to have multiple ways to solve a problem because sometimes only one way will work in the way needed.

12/13/2008