Search code examples
bashloopscopy

Bash script for multiple copy 1 file and rename to new name


I want to create bash script for make multiple copy of 1 file in source directory to Destination directory and rename it with specific running number based on my config file.

example :Source directory --> /srv/test/ (have 3 type of file )

  • AB-TEST-A1000000001_20230908005727724.tar
  • AC-ERROR-B1000000009_20230908005727724.tar
  • AD-BUG-K8000000000_20230908005727724.tar

Destination directory --> /srv/rename

In my config file content of (copy from file name;Number of copy; New running number; Rename 2 front character)

config_rename.txt

  • AB-TEST-A1000000001_20230908005727724.tar;2;A1000000001;12
  • AC-ERROR-B1000000009_20230908005727724.tar;3;B1000000009;YX

Expectation from the config file(config_rename.txt)

Destination directory --> /srv/rename Have 2 copy from this file AB-TEST-A1000000001_20230908005727724.tar & rename it

  • 12-TEST-A1000000001_20230908005727724.tar
  • 12-TEST-A1000000002_20230908005727724.tar

Destination directory --> /srv/rename Have 3 copy from this file AC-ERROR-B1000000009_20230908005727724.tar & rename it

  • YX-ERROR-B1000000010_20230908005727724.tar
  • YX-ERROR-B1000000011_20230908005727724.tar
  • YX-ERROR-B1000000012_20230908005727724.tar

I'm try to use this but not sure how to rename file it based on the config file

    cat $Conf_File | while read line
do
for file in "$Source_directory"/*; do 
 f="${file##*/}" 
[[ -f "$file" ]] && cp "$file" "$Destination_directory/$f"
done
done

Solution

  • a.sh

    #!/bin/bash
    f=(`ls test`)  #file array
    r=(`cat config_rename.txt`)  #rule array
    
    for((i=0;i<${#f[*]};i++))
    do
      f0=${f[i]#*-}  #example: TEST-A1000000001_20230908005727724.tar
      f1=${f0%-*}  #type of file, example: TEST
      f2=${f[i]##*_}  #example: 20230908005727724.tar
      for((j=0;j<${#r[*]};j++))
      do
        r_tmp=(${r[j]//;/ })  #temp array, example: r_tmp=(AB-TEST-A1000000001_20230908005727724.tar 2 A1000000001 12)
        f3=${r_tmp[2]%%[0-9]*}  #example: A
        f4=${r_tmp[2]##*[A-Za-z]}  #example: 1000000001
        if [[ ${f[i]} == ${r_tmp[0]} ]];then
          #while((r_tmp[1]--))
          for((k=0;k<${r_tmp[1]};k++))
          do
            f5=${f3}$((${f4}+k))
            cp test/${f[i]} rename/${r_tmp[3]}-${f1}-${f5}_${f2}
          done
        fi
      done
    done
    

    example:

    $ find
    .
    ./test
    ./test/AD-BUG-K8000000000_20230908005727724.tar
    ./test/AC-ERROR-B1000000009_20230908005727724.tar
    ./test/AB-TEST-A1000000001_20230908005727724.tar
    ./rename
    ./a.sh
    ./config_rename.txt
    $ bash a.sh && find
    .
    ./test
    ./test/AD-BUG-K8000000000_20230908005727724.tar
    ./test/AC-ERROR-B1000000009_20230908005727724.tar
    ./test/AB-TEST-A1000000001_20230908005727724.tar
    ./rename
    ./rename/YX-ERROR-B1000000009_20230908005727724.tar
    ./rename/YX-ERROR-B1000000011_20230908005727724.tar
    ./rename/YX-ERROR-B1000000010_20230908005727724.tar
    ./rename/12-TEST-A1000000001_20230908005727724.tar
    ./rename/12-TEST-A1000000002_20230908005727724.tar
    ./a.sh
    ./config_rename.txt
    $ cat config_rename.txt
    AB-TEST-A1000000001_20230908005727724.tar;2;A1000000001;12
    AC-ERROR-B1000000009_20230908005727724.tar;3;B1000000009;YX