Search code examples
regexbatch-filegrepfile-rename

Bulk renaming files


So I have a series of files with the following format Their real file name followed by the date.

file_1_12DEC2011.pdf
file_2_12DEC2011.pdf
file_3_12DEC2011.pdf
file_4_12DEC2011.pdf
file_5_12DEC2011.pdf

I am trying to figure out How I can Increment the day by one day. For instance, this would be my desired result.

file_1_13DEC2011.pdf
file_2_13DEC2011.pdf
file_3_13DEC2011.pdf
file_4_13DEC2011.pdf
file_5_13DEC2011.pdf

Solution

  • To do it safely, echo before mv:

    for i in file_[1-5]_12DEC2011.pdf; do
        mv $i ${i/12/13}
    done
    

    To do it quickly, use the rename command:

    $ rename 's/12/13/' file_[1-5]_12DEC2011.pdf
    

    UPDATE: You can use awk to generate new file name, and put it in those command above.

    for i in *_*.pdf; do
        mv $i `echo $i | awk -F_ -v OFS=_ '{sub(/[0-9]+/, $NF+1, $NF)}1'`
    done