Find and replace filename recursively in a directory

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

    Provided you don't have newlines in your filenames:

    find -name '123_*.txt' | while IFS= read -r file; do mv "$file" "${file#123_}"; done
    

    For a really safe way, provided your find supports the -print0 flag (GNU find does):

    find -name '123_*.txt' -print0 | while IFS= read -r -d '' file; do mv "$file" "${file#123_}"; done
    

提交回复
热议问题