Find and replace filename recursively in a directory

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

    you could check 'rename' tool

    for example

    rename 's/^123_//' *.txt
    

    or (gawk is needed)

    find . -name '123_*.txt'|awk '{print "mv "$0" "gensub(/\/123_(.*\.txt)$/,"/\\1","g");}'|sh
    

    test:

    kent$  tree
    .
    |-- 123_a.txt
    |-- 123_b.txt
    |-- 123_c.txt
    |-- 123_d.txt
    |-- 123_e.txt
    `-- u
        |-- 123_a.txt
        |-- 123_b.txt
        |-- 123_c.txt
        |-- 123_d.txt
        `-- 123_e.txt
    
    1 directory, 10 files
    
    kent$  find . -name '123_*.txt'|awk '{print "mv "$0" "gensub(/\/123_(.*\.txt)$/,"/\\1","g");}'|sh
    
    kent$  tree
    .
    |-- a.txt
    |-- b.txt
    |-- c.txt
    |-- d.txt
    |-- e.txt
    `-- u
        |-- a.txt
        |-- b.txt
        |-- c.txt
        |-- d.txt
        `-- e.txt
    
    1 directory, 10 files
    

提交回复
热议问题