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.

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.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , ,
Posted in The Linux and Unix Articles!
2 comments on “Curly Brackets Expansion In Bash: With 5 Practical Examples
  1. north face 2012 says:

    Adding this to twitter great info.

  2. Martin says:

    Hi,

    thanks for that but I found a little bug:

    Because the “ls {a,b,c}.txt” says “ls: cannot access a.txt: No such file or directory”, the right statement must be “ls /path/to/files/{a,b,c}.txt” (in example 3)

    Howdy

Leave a Reply to north face 2012 Cancel reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!