Rename multiple files based on pattern in Unix

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

    Using mmv:

    mmv "fgh*" "jkl#1"
    
    0 讨论(0)
  • 2020-11-22 07:05

    My version of renaming mass files:

    for i in *; do
        echo "mv $i $i"
    done |
    sed -e "s#from_pattern#to_pattern#g” > result1.sh
    sh result1.sh
    
    0 讨论(0)
  • 2020-11-22 07:07
    #!/bin/sh
    
    #replace all files ended witn .f77 to .f90 in a directory
    
    for filename in *.f77
    do 
        #echo $filename
        #b= echo $filename | cut -d. -f1
        #echo $b    
        mv "${filename}" "${filename%.f77}.f90"    
    done
    
    0 讨论(0)
  • 2020-11-22 07:07

    Using renamer:

    $ renamer --find /^fgh/ --replace jkl * --dry-run
    

    Remove the --dry-run flag once you're happy the output looks correct.

    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2020-11-22 07:08
    rename fgh jkl fgh*
    
    0 讨论(0)
提交回复
热议问题