Rename multiple files based on pattern in Unix

前端 未结 22 933
死守一世寂寞
死守一世寂寞 2020-11-22 06:31

There are multiple files in a directory that begin with prefix fgh, for example:

fghfilea
fghfileb
fghfilec

I want to rename a

22条回答
  •  不思量自难忘°
    2020-11-22 07:09

    A generic script to run a sed expression on a list of files (combines the sed solution with the rename solution):

    #!/bin/sh
    
    e=$1
    shift
    
    for f in $*; do
        fNew=$(echo "$f" | sed "$e")
        mv "$f" "$fNew";
    done
    

    Invoke by passing the script a sed expression, and then any list of files, just like a version of rename:

    script.sh 's/^fgh/jkl/' fgh*
    

提交回复
热议问题