How to create 100 files with the names a1, a2, a3, .... a100 in the terminal? Can this be done in one team?

  • 3
    It depends on the shell. touch a{1..100} in bash, for example - Alexey Ten

1 answer 1

the greatest “complexity” is to get a sequence of numbers. to add them with letters and hand them over to the touch program is simple.

As far as I know, the posix standard does not have any “onboard” tools that simplify the generation process. You can use some universal language:

  1. using the awk program:

     $ awk 'BEGIN { for(i=1;i<=5;i++) printf "a%d\n", i }' a1 a2 a3 a4 a5 

    to create files, just transfer the list to the touch program:

     $ touch $(awk 'BEGIN { for(i=1;i<=5;i++) printf "a%d\n", i }') 
  2. using the bc program:

     $ echo 'for (i=1; i<=5; i++) {print "a",i,"\n"}' | bc a1 a2 a3 a4 a5 

    to create files, just transfer the list to the touch program:

     $ touch $(echo 'for (i=1; i<=5; i++) {print "a",i,"\n"}' | bc) 
  3. in the gnu operating system, there is a seq program that just simplifies the generation:

     $ seq -f 'a%g' 1 5 a1 a2 a3 a4 a5 

    to create files, just transfer the list to the touch program:

     $ touch $(seq -f 'a%g' 1 5) 
  4. well, modern “heaped” bash / zsh shells understand the {1..5} construct:

     $ echo {1..5} 1 2 3 4 5 

    those. you can create files again by passing the list to the touch program:

     $ touch a{1..5}