Search code examples
bashshellunixmv

How to batch rename all files with the same additional text?


Can mv or some other command in Unix append a string all files with a certain extension? Like I have a 3 files in a directory ending with .txt. Lets say following are they:

one.txt
two.txt
three.txt

I want to rename them to

one_renamed.txt
two_renamed.txt
three_renamed.txt

Is there a way we can batch rename all the files with the same additional text?


Solution

  • Use for loop and shell parameter expansion:

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

    The result of the expansion "${file%.txt}" is the value of variable file with the string .txt deleted from the end.