Sunday, December 5, 2010

Running a command on multiple items in a directory with a long path

To run a command on more than one file or directory in a directory with a long path, curly braces can be used to group the files and directories together.

touch /a/really/long/path/id_{1,2,3,6}.txt
#creates 4 files named id_1.txt, id_2.txt, id_3.txt and id_6.txt in the directory /a/really/long/path/


mkdir /a/really/long/path/id_{1,2,3,6}
#makes 4 directories named id_1, id_2, id_3 and id_6 in the directory /a/really/long/path/


cat /a/really/long/path/id_{1,2,3,6}.txt
#echoes the contents of 4 files named id_1.txt, id_2.txt, id_3.txt and id_6.txt in the directory /a/really/long/path/


cp /a/really/long/path/id_{1,2,3,6}.txt .
#copies 4 files named id_1.txt, id_2.txt, id_3.txt and id_6.txt from the directory /a/really/long/path/ to the current directory


rm /a/really/long/path/id_{1,2,3,6}.txt
#removes 4 files named id_1.txt, id_2.txt, id_3.txt and id_6.txt from the directory /a/really/long/path/


rm -Rf /a/really/long/path/id_{1,2,3,6,{1,2,3,6}.txt}
#removes 4 directories named id_1, id_2, id_3 & id_6 and 4 files named id_1.txt, id_2.txt, id_3.txt & id_6.txt from the directory /a/really/long/path/

1 comment: