Skip to content
Advertisement

Copy a folder contents and save the file with diff name Unix

I have a bunch of .txt files in a directory. I m looking for a command to copy all .txt files and save it with <filename>_2.txt.

Eg: abc.txt -> abc_2.txt (After copy)

Thanks in tons in advance

Advertisement

Answer

EDIT: As per OP’s extension request adding following code now.

for file in *.txt
do
  if [[ ! -f "${file%.*}_MED_2.txt" ]]
  then
      cp "$file" "${file%.*}_MED_2.txt"
  fi
done


Try following.

for file in *.txt
do
  echo "cp $file ${file%.*}_2"
done

Above will print cp commands, if you are ok with them then run following.

for file in *.txt
do
if [[ ! -f "${file%.*}_2" ]]
then
     cp "$file" "${file%.*}_2"
fi
done
3 People found this is helpful
Advertisement