Home > database >  How can I batch rename folders (cut beginning to end)?
How can I batch rename folders (cut beginning to end)?

Time:01-05

I have a long list of folders formatted like this:

Moscow,_1_November_2021
Khosta,_9_November_2021
Adler,_12_November_2021

I would like them to begin with the date and end with location, like this:

1_November_2021,Moscow
9_November_2021,Khosta
12_November_2021,Adler

How can I batch rename these folders?

CodePudding user response:

This simple loop should do the job (remove the echo once you checked that it does what you want):

for dir in ./*,_[1-3]*_*_20[0-2][0-9]
do
    name=${dir##*/}
    path=${dir%"$name"}
    echo mv -v "$dir" "$path${name#*,_},${name%%,*}"
done

CodePudding user response:

With ls and sed you can build the commands that you need, with xargs you can execute all of them:

ls|sed  's/^\(.*\),_\(.*\)$/\0 \2,\1/'| xargs -L1 mv
  •  Tags:  
  • Related