13 examples to use curly braces in Linux

This is a small post on how to crate multiple files/folders, sequence generation with flower brackets in-order to save valuable time.

Creating empty files can be done with touch command. We will see how to create multiple files using this command in one shot.

Example 1: Create a file with name abc.txt

touch abc.txt

Example2: Create multiple files abc, cde, efg, hij, klm

touch  abc cde efg hij klm

Example 3: How about creating 1 to 20 files, ie creating multiple files with one command. Its bit tedious job for an admin. Don’t worry Linux provide us with some useful option with “Flower braces” to do expansion. Instead of writing below command

touch 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

We can create 20 files using flower brace {} expansion as shown below..

touch {1..20}

Your shell tries to expand this brace and generate 1 to 20 numbers. The above command will create total 20 files in one shot.

Example 4: How about creating files as 1.txt, 2.txt, 3.txt up to 1000.txt. This can be achieved suffixing it after brackets as shown below.

touch {1..1000}.txt

Example 5: How about creating a1, a2, a3 so on up to a1000 files?

touch a{1..1000}

Example 6:How about generating numbers?. We can generate numbers using flower braces with echo command. The other way to generate numbers is seq command.

echo {1..10}

Example 7: Even we can do sequence for alphabets as shown below

touch {a..z}

Example 8: Generate files from A to Z

touch {A..Z}

Note: The above alphabets generation is done using asci values

Example 9: How about creating 1 to 1000000 files in one shot, by using multiplication/matrix of numbers?. We can achieve this one with multiplying two sequences.

touch {0..1000}{0..1000}

Example10: How about creating files as multiples of 2.

touch {1..100..2}

this will create files as 1, 3, 5, 7, 9, 11 etc.

Note: This interval will work only BASH version 4.0 and above, make a note about this.

Example 11: How about creating files as multiples of 7?

touch {1..100..7}

Note: This interval option will work only bash version 4 and above.

Practical usage of this brackets

1) In bash for loop.

2) Generating sequence of numbers.

As mention in above examples we can create folders similarly in one shot. use -p option if you want to create folders in sub folders too.

Example 12: Create a folder structure as 2012, under this i want to create 12 folders for each month and under each month create 30 folders which corresponding to 30 days.

mkdir -p 2012/{1..12}/{1..30}

This will create folder 2012 under that 1 to 12 folders and under each of these folders 1 to 30 folders in one go.

Example 13: Usage in for loop.

for i in {1..10}
do
echo "Present number is $i"
done

Please feel free to comment your thoughts on this.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.
  • airbender

    Cool. So good to know these 😀

  • picky

    “flower brackets”? Do you mean “braces”, the name used in ISO-8859-1 for the characters “{” and “}”?

  • Subrat N

    Simply superb….

  • http://NA Saurav Srivastava

    Thank you for solving my query , you are really awesome

  • http://gravatar.com/design67 design67

    I saw someone do something like this touch !!:2/{package.json, index.js}
    and it created a package.json and index.js if 4 directories
    when I try it I’m getting an error. do I need some type of plugin

    • no

      It's refering to history '!'

      !! means last command

      !!:2 or !:2 means 2nd argument from the last command

      I imagine they needed to create a file in the directory which they'd just created in the previous command.

      Like below:

      ~/user$ mkdir test test2
      ~/user$ touch !:2/{1,2,3}
      touch test2/{1,2,3}

       

      ~/user$ ls -al test2/
      total 64
      drwxr-xr-x  2 user     user       0 Sep 18 16:20 .
      drwxrwxrwx 2 user user         0 Sep 18 16:20 ..
      -rw-r–r–  1 user     user          0 Sep 18 16:20 1
      -rw-r–r–  1 user     user          0 Sep 18 16:20 2
      -rw-r–r–  1 user     user          0 Sep 18 16:20 3

  • TD Murthy

    Thnaks all my questions about creation of files in one go answered here

  • http://www.webstratege.fr/ patrick

    nothing on this form : touch home/{me,mother,father}/name ?

  • z.coldplayer

    echo {0..10}{0..10}
    00 01 02 03 04 05 06 07 08 09 010 10 11 12 13 14 15 16 17 18 19 110 20 21 22 23 24 25 26 27 28 29 210 30 31 32 33 34 35 36 37 38 39 310 40 41 42 43 44 45 46 47 48 49 410 50 51 52 53 54 55 56 57 58 59 510 60 61 62 63 64 65 66 67 68 69 610 70 71 72 73 74 75 76 77 78 79 710 80 81 82 83 84 85 86 87 88 89 810 90 91 92 93 94 95 96 97 98 99 910 100 101 102 103 104 105 106 107 108 109 1010

    010 110 210 ,… 1010

    see the trouble

    • http://www.linuxnix.com Surendra

      Thats true, which I already mention in example 9

banner