1

How to rename all the files in the directory in such a way the files get added "_1" before ".txt"

apac_02_aug_2017_file.txt
emea_02_May_2017_file.txt
ger__02_Jun_2017_file.txt

To

apac_02_aug_2017_file_1.txt
emea_02_May_2017_file_1.txt
ger__02_Jun_2017_file_1.txt
Sundeep
  • 12,008
Nirmal
  • 63

2 Answers2

10

With rename

rename .txt _1.txt * should do what you are looking for.

To quote man rename:

rename [options] expression replacement file...

rename will rename the specified files by replacing the first occurrence of expression in their name by replacement.


With common bash commands

Since you said that rename is not installed on your system, here's a solution that uses more standard Bash:

for file in *.txt; do
    mv "$file" "${file%.txt}_1.txt"
done

Explanation: We loop over all files. For each file, we move it to the correct location by making use of something called "parameter expansion" (this is the ${} part). The special character % can be used within parameter expansion to match a pattern at the end of the string and delete it.

For further information, see: http://wiki.bash-hackers.org/syntax/pe#from_the_end

0

Through mmv it's mush easy:

mmv '*.*' '#1_1.#2' *.txt
αғsнιη
  • 41,407