Find and replace filename recursively in a directory

前端 未结 14 1956
野趣味
野趣味 2020-11-28 21:24

I want to rename all the files in a folder which starts with 123_xxx.txt to xxx.txt.

For example, my directory has:

123_xxx         


        
14条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 22:11

    Tried the answer above but it didn't work for me cause i had the string inside folders and files name at the same time so here is what i did the following bash script:

      for fileType in d f
      do
        find  -type $fileType -iname "stringToSearch*" |while read file
        do
          mv $file $( sed -r "s/stringToSearch/stringToReplaceWith/" <<< $file )
        done
      done
    

    First i began by replacing inside folders name then inside files name.

提交回复
热议问题