Rename multiple files based on pattern in Unix

前端 未结 22 1137
死守一世寂寞
死守一世寂寞 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:17

    This is how sed and mv can be used together to do rename:

    for f in fgh*; do mv "$f" $(echo "$f" | sed 's/^fgh/jkl/g'); done
    

    As per comment below, if the file names have spaces in them, quotes may need to surround the sub-function that returns the name to move the files to:

    for f in fgh*; do mv "$f" "$(echo $f | sed 's/^fgh/jkl/g')"; done
    

提交回复
热议问题