Linux Copy the file a hundred times

I need the correct syntax to copy file.jpg 100 times with one command to get 100 files with the name file001.jpg, file002.jpg, ..., file100.jpg. I use this, but dont work for me any advice

#!/bin/bash for x in `seq 1 100`; do if [[ x -lt 10 ]]; then cp file.jpg file-00$x.jpg; elif [[ x -lt 100 ]]; then cp file.jpg file-0$x.jpg; else cp file.jpg file-$x.jpg; fi done 
+4
source share
2 answers

In your two if conditions, you are missing $ by $x , so you end up comparing the string x , not the number.

As Bazile Starinkevich notes, the printf utility is probably a much better way to go here: $(printf "file-%03d.jpg" $x) gets the desired formatted string in one line.

+3
source

Using bash extension extensions

 for n in {001..100}; do cp file.jpg file-$n.jpg done 
+14
source

Source: https://habr.com/ru/post/1438129/


All Articles