smart grouping shorten long command line

August 25th, 2006 mysurface Posted in curly brackets, Symbol | Hits: 24052 | 4 Comments »

Let say you want to copy certain files from a directory to current directory, which the parent path is long, how the command will looks like?

cp /home/foobar/downloads/one /home/foobar/downloads/two..... .

So most of the people will change directory to downloads first, then copy those files, such as
(Assume the current directory is /home/soomee/downloads )

cd /home/foobar/downloads
cp one two three four five /home/soomee/downloads

That is another alternative way, use curly bracket { }

cp /home/foobar/downloads/{one,two,three,four} .

This command line is much much shorter and it is also very clean.

Another example, which is where I learn the grouping from, when I need to compile my source code in hello.c to executable file hello, I do this

gcc -o hello hello.c

Okay, and it become like that.

gcc -o hello{,.c}

The curly bracket can be used at infront too, let say i want to backup my .vimrc i can do this

cp {,bk}.vimrc

This is equivalent to this

cp .vimrc bk.vimrc

[中文翻译]

4 Responses to “smart grouping shorten long command line”

  1. A formal name for this feature is “Brace Expansion”, I just found it recently in Bash Reference Manual, or you may try man bash then search for “brace expansion”.

  2. [...] I like to do this, combine with curly brackets [...]

  3. [...] This post is to illustrate additional usage of curly brackets { } for range of numbers, continues from smart grouping shorten long command line. [...]

  4. [...] This post is to illustrate additional usage of curly brackets { } for range of numbers, continues from smart grouping shorten long command line. [...]

Leave a Reply