I have a file named, for example, ascdrgi.txt, with the following contents:


I want to duplicate this file a (variable) number of times by changing the last character of the filename (ignoring the extension). For example, in this case if I made 3 copies, they would be named:

  • ascdrgj.txt
  • ascdrgk.txt
  • ascdrgl.txt

If the filename ends with a number, that number should increase instead, so copies of ascdrg1.txt would be:

  • ascdrg2.txt
  • ascdrg3.txt
  • ascdrg4.txt

If the file already exists, the script should skip that name and move onto the next one. If we reach the last character (z, Z, or 9), it should loop around to the beginning (the next would be a, A, or 1, respectively).

In addition to duplicating the original file, I need to modify the first line of each file to say which file it is (numerically), as well as the total number of files. Using the first ascdrgi.txt example, that file would now contain:

tiger number(1,4)

The next file, ascdrgj.txt, would contain:

tiger number(2,4)

and so on.

  • awk '/^----/{f="file"(++c)".txt"}c{print$0>f}' filename. here i am searching by pattern and dividing the file. but i need the exact file but the last character will be incrementing
    – dils
    Dec 29, 2015 at 15:41
  • I edited the question to include all the comments and removed them. There was a conflict between what the question said ("If the file already exists, I should get an error saying already exists.") and one of the comments ("if A already exist then it should go for B."). I went with the comment since it was more recent Dec 29, 2015 at 17:03
  • Thank You Michael for sorting it Properly. sorry for the inconvenience caused.
    – dils
    Dec 29, 2015 at 17:11
  • Michael. let me know if you can help me with this script creation. For "already file exist" part it is ok if we skip it also as in my case duplicate file will not be there
    – dils
    Dec 29, 2015 at 17:30

2 Answers 2


The following shell script will perform (most) of what you need. It will not modify the original file (not add 'number' to it) - only the newly created files.

Hopefully the comments are clear enough. Using somewhat convoluted expr instead of bash's parameter expansion should make it more portable:


orig=ascdrg3.txt # start with this file

count=1 #loop variable
max=5   #number of files to create
while test "$count" -le "$max" ; do
    # Remove extension
    base=$(basename "$in" .txt)

    # get the prefix
    prefix=$(expr substr "$base" 1 $((${#base}-1)))

    # get last letter
    last=$(expr substr "$base" ${#base} 1)

    while true ;
        # Advance letter, while the file doesn't exist
        last=$(echo "$last" | tr A-Z B-ZA)
        last=$(echo "$last" | tr a-z b-za)
        last=$(echo "$last" | tr 0-9 1-90)

        # construct new file name

        # continue if it doesn't exist
        # (otherwise, advance the last letter and try again)
        test -e "$new" || break

        test "$new" = "$orig" \
            && { echo "error: looped back to original file" >&2 ; exit 1; }

    # Create new file
    cp "$orig" "$new"

    # Modify first line of new file
    sed -i "1s/\$/number($count,$max)/" "$new"

    # Advance counter

    # loop again

For the digit case: , if its like ab1.txt and you want ab2.txt and ab3.txt and so on:

  for i in `seq 2 3` ; do cp ab1 ab$i.txt ; done

You can do a similar thing for the alphabet case.

   for i in `echo {d..f}` ; do cp abc.txt ab$i.txt ; done

will result in abd.txt abe.txt abf.txt

  • Thanks ljas. Is it possible to have a generic one as the file name cannot be predicted.
    – dils
    Dec 29, 2015 at 16:41
  • This doesn't seem to cover the second part of the question Dec 29, 2015 at 17:05
  • this is just a hint , he needs to work on it further
    – Ijaz Ahmad
    Dec 29, 2015 at 17:06
  • yes , you can make it generic by examining the file name and decide the range for seq $start $end and echo {$start...$end}
    – Ijaz Ahmad
    Dec 29, 2015 at 17:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .