Find and replace filename recursively in a directory

前端 未结 14 2049
野趣味
野趣味 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:18

    If the names are fixed you can visit each directory and perform the renaming in a subshell (to avoid changing the current directory) fairly simply. This is how I renamed a bunch of new_paths.json files each to paths.json:

    for file in $(find root_directory -name new_paths.json)
      do
         (cd $(dirname $file) ; mv new_paths.json paths.json)
      done
    

提交回复
热议问题