Rename multiple files based on pattern in Unix

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

    I wrote this script to search for all .mkv files recursively renaming found files to .avi. You can customize it to your neeeds. I've added some other things such as getting file directory, extension, file name from a file path just incase you need to refer to something in the future.

    find . -type f -name "*.mkv" | while read fp; do 
    fd=$(dirname "${fp}");
    fn=$(basename "${fp}");
    ext="${fn##*.}";
    f="${fn%.*}";
    new_fp="${fd}/${f}.avi"
    mv -v "$fp" "$new_fp" 
    done;
    

提交回复
热议问题