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.
Latest posts by Surendra Anne (see all)
- Docker: How to copy files to/from docker container - June 30, 2020
- Anisble: ERROR! unexpected parameter type in action:
Fix - June 29, 2020 - FREE: JOIN OUR DEVOPS TELEGRAM GROUPS - August 2, 2019
- Review: Whizlabs Practice Tests for AWS Certified Solutions Architect Professional (CSAP) - August 27, 2018
- How to use ohai/chef-shell to get node attributes - July 19, 2018