Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I understand what brace expansion is, but I don't know how best to use it.

When do you use it?
Please teach me some convenient and remarkable examples if you have your own tip.

share|improve this question

5 Answers 5

up vote 33 down vote accepted

Brace expansion is very useful if you have long path names. I use it as a quick way to backup a file:

cp /a/really/long/path/to/some/file.txt{,.bak}

will copy /a/really/long/path/to/some/file.txt to /a/really/long/path/to/some/file.txt.bak

You can also use it in a sequence. I once did so to download lots of pages from the web:

wget http://domain.com/book/page{1..5}.html

or

for i in {1..100}
do
   #do something 100 times
done
share|improve this answer
2  
+1, sequence with wget is fresh. –  Benjamin Jan 19 '11 at 11:29
5  
This can be used for argument duplication as well. Say you have some program which requires a '-f' argument for every file to operate on. instead of doing program -f file1 -f file2 -f file3, you can do program "-f file"{1..3} –  Patrick Feb 1 '11 at 20:42
    
This is a shame. I just discovered it but do the backup thing on extra-long paths for years without knowing rhis. Thanks. –  smonff May 20 at 12:41

I use it when I want to reduce typing:

geany /path/to/file1 /path/to/file2
# versus
geany /path/to/file{1,2}

Another example:

wajig install libpam0g-dev libiw-dev libdb-dev
# versus
wajig install lib{pam0g,iw,db}-dev
share|improve this answer
1  
+1 for wajig install –  Benjamin Jan 19 '11 at 11:30

I use it to compare actual test output to desired test output during development. If test #41 fails, it's easy to see what the difference between the test output (in file tests.output/041) and the desired output (in file tests.out/041):

$ diff tests.{out,output}/041
share|improve this answer

Brace expansion comes very handy when creating large directory structures:

mkdir -p dir1/{subdir1,subdir2}/{subsubdir1,subsubdir2}

This will give you

find dir1 -type d
dir1
dir1/subdir1
dir1/subdir1/subsubdir1
dir1/subdir1/subsubdir2
dir1/subdir2
dir1/subdir2/subsubdir1
dir1/subdir2/subsubdir2

You could even go one step further and put brace expansion into brace expansion:

mkdir -p dir1/{subdir{1,2}}/{subsubdir{1,2}}

This will give you the same directory structure as the example above.

share|improve this answer
3  
+1 for the nesting. Love that feature. –  Jander Jan 31 '11 at 16:36
2  
Nesting is nice, but your nesting example (mkdir -p dir1/{subdir{1,2}}/{subsubdir{1,2}}) doesn't actually serve any purpose. You could have just done this: mkdir -p dir1/subdir{1,2}/subsubdir{1,2}. –  iconoclast Jul 14 '11 at 18:40
    
@iconoclast, it does serve a purpose if you interpret subdir1 and subdir2 in the non-literal sense. Replace subdir1 with cat and subdir2 with dog for example. –  ephsmith Jul 26 '12 at 10:55
1  
@ephsmith: if you interpret "subdir1" as standing in for "cat" and "subdir2" for "dog", then the nesting example fails, since it uses subdir{1,2}, and there is no string that you can replace "subdir" with that will make subdir{1,2} return {cat,dog}. –  iconoclast Jul 26 '12 at 21:04

Some frequent cases for me are:

For renaming:

mv myText.{txt,tex}

or

mv myText.tex{,.old}

or

cp myText.tex{,.backup}

(Although it's less messy to use version control for the last 2 tasks.)

For comparing (already mentioned):

diff path{1,2}/a.txt
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.