Chances are you've come across expansions in BASH, whenever you are going back to the user home directory, we use the cd command alongside a tilde character (~), e.g:
cd ~
This would take you back to your home directory, and that is because the tilde character represents the value of the user's home variable, this is called the tilde expansion.
Another type of expansion that we are concerned about in this guide is the brace expansion, this is a handy command that can save you a ton with repeated commands when working with a range of terms.
For example, to create a multiple files with the touch command using the brace expansion, we do:
Note: Create a temporary directory for this example: mkdir cat
touch {persian,maine,siamese,bengal}
This would create the four file all in one go, you see how handy that is, you don't have to keep repeating the command one after the order.
Yeah, this is cool but what if you want to create hundreds or thousands of files at once, then we can use interpolation, this is creating files from know name in a given range.
To create a file_1 - file_100, we do:
touch file_{1..100}
This would create a 100 of new files, you can in fact create a thousand of file with this method. If you try listing the files created with this command, you would see that they aren't properly sorted, for example, here is the output of the one I just created:
user@server:~/cat$ ls
file_1 file_17 file_25 file_33 file_41 file_5 file_58 file_66 file_74 file_82 file_90 file_99
file_10 file_18 file_26 file_34 file_42 file_50 file_59 file_67 file_75 file_83 file_91
file_100 file_19 file_27 file_35 file_43 file_51 file_6 file_68 file_76 file_84 file_92
file_11 file_2 file_28 file_36 file_44 file_52 file_60 file_69 file_77 file_85 file_93
file_12 file_20 file_29 file_37 file_45 file_53 file_61 file_7 file_78 file_86 file_94
file_13 file_21 file_3 file_38 file_46 file_54 file_62 file_70 file_79 file_87 file_95
file_14 file_22 file_30 file_39 file_47 file_55 file_63 file_71 file_8 file_88 file_96
file_15 file_23 file_31 file_4 file_48 file_56 file_64 file_72 file_80 file_89 file_97
file_16 file_24 file_32 file_40 file_49 file_57 file_65 file_73 file_81 file_9 file_98
user@server:~/cat$
This is really disorganized, which isn't something I would want. To correct this we can use zero padding, if you are testing this in a test directory remove the ones we have created before with rm *
Now, to pad it, we simply use one zero:
touch file_{01..100}
Even if you are padding thousands of files, one zero is all it takes, here is the output of the above command:
ls
file_001 file_011 file_021 file_031 file_041 file_051 file_061 file_071 file_081 file_091
file_002 file_012 file_022 file_032 file_042 file_052 file_062 file_072 file_082 file_092
file_003 file_013 file_023 file_033 file_043 file_053 file_063 file_073 file_083 file_093
file_004 file_014 file_024 file_034 file_044 file_054 file_064 file_074 file_084 file_094
file_005 file_015 file_025 file_035 file_045 file_055 file_065 file_075 file_085 file_095
file_006 file_016 file_026 file_036 file_046 file_056 file_066 file_076 file_086 file_096
file_007 file_017 file_027 file_037 file_047 file_057 file_067 file_077 file_087 file_097
file_008 file_018 file_028 file_038 file_048 file_058 file_068 file_078 file_088 file_098
file_009 file_019 file_029 file_039 file_049 file_059 file_069 file_079 file_089 file_099
file_010 file_020 file_030 file_040 file_050 file_060 file_070 file_080 file_090 file_100
You see how organized this is. You don't have to stop, you can also create a specific file extension in one go, to create hundreds of txt files at once, we do:
touch book{01..100}.txt
ls
ls
book001.txt book014.txt book027.txt book040.txt book053.txt book066.txt book079.txt book092.txt
book002.txt book015.txt book028.txt book041.txt book054.txt book067.txt book080.txt book093.txt
book003.txt book016.txt book029.txt book042.txt book055.txt book068.txt book081.txt book094.txt
book004.txt book017.txt book030.txt book043.txt book056.txt book069.txt book082.txt book095.txt
book005.txt book018.txt book031.txt book044.txt book057.txt book070.txt book083.txt book096.txt
book006.txt book019.txt book032.txt book045.txt book058.txt book071.txt book084.txt book097.txt
book007.txt book020.txt book033.txt book046.txt book059.txt book072.txt book085.txt book098.txt
book008.txt book021.txt book034.txt book047.txt book060.txt book073.txt book086.txt book099.txt
book009.txt book022.txt book035.txt book048.txt book061.txt book074.txt book087.txt book100.txt
book010.txt book023.txt book036.txt book049.txt book062.txt book075.txt book088.txt
book011.txt book024.txt book037.txt book050.txt book063.txt book076.txt book089.txt
book012.txt book025.txt book038.txt book051.txt book064.txt book077.txt book090.txt
book013.txt book026.txt book039.txt book052.txt book065.txt book078.txt book091.txt
This is super handy!
We can take things a bit further by specifying an interval, to create a number between 1 and 10, counting by two we do:
user@server:~/cat$ echo {1..10..2}
1 3 5 7 9
To create by 3, you do:
user@server:~/cat$ echo {1..10..3}
1 4 7 10
Just make sure you are using an absolute integer, you can create an actual file, we can use touch together with the interval:
touch book{1..10..3}.txt
Interval from A..Z
user@server: echo {A..Z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Interval from A..z
pascal@blog:~/cat$ echo {A..z}
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z
The above would not only cover the capital letter but also the small letters.
You can also use intervals with letters as well:
echo {P..Z..2}
P R T V X Z
This would count them in 2's, to create a backward range, we do:
user@server:~/cat$ echo {Z..P..2}
Z X V T R P