Curly Brackets Expansion In Bash: With 5 Practical Examples

An usefull wildcard trick is the curly bracket (or brace) expansion. I have previously written some articles on wildcards, but in this one I will show you 5 practical examples for using the curly braces { } , in order to make your filename manipulation easily.

Watch Free Movies

Example 1: Use the curly brackets to backup files:

$ cp /path/to/file{,.bak}

The command cp /path/to/file{,.bak} is the same as cp /path/to/file /path/to/file.bak.

As you know, .bak is the common extension for the backup files.

Example 2:

$ touch /path/to/{file1,file2,file3}.txt

The command touch /path/to/{file1,file2,file3}.txt is the same as touch /path/to/file1 /path/to/file2 /path/to/file3.

Example 3:

$ ls {a,b,c}.txt
ls: cannot access a.txt: No such file or directory
ls: cannot access b.txt: No such file or directory
ls: cannot access c.txt: No such file or directory

ls {a,b,c}.txt does ls a.txt, ls b.txt and ls c.txt.

Example 4:

$ touch {one,two}.{txt,doc}
$ ls
one.doc one.txt two.doc two.txt

touch {one,two}.{txt,doc} is the same as touch one.txt one.doc two.txt two.doc.

Example 5:

$ ls *.{txt,doc}

ls *.{txt,doc} does ls on all the txt and doc files, from the current directory.

Scroll to Top