Renaming all files in folder in Bash

Nir*_*mal 1 bash

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
Run Code Online (Sandbox Code Playgroud)

To

apac_02_aug_2017_file_1.txt
emea_02_May_2017_file_1.txt
ger__02_Jun_2017_file_1.txt
Run Code Online (Sandbox Code Playgroud)

Paw*_*uin 7

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
Run Code Online (Sandbox Code Playgroud)

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.

有关更多信息,请参阅:http : //wiki.bash-hackers.org/syntax/pe#from_the_end