批量修改文件后缀名

    技术2022-05-20  56

    Bash批量修改文件后缀名

    Mv files suffix in batch in one dir

     

    一、# and %

    #! /bin/bash

    FILE=dir1/dir2/dir3/my.file.txt

    echo "The source string"

    echo ${FILE}     #dir1/dir2/dir3/my.file.txt

    echo "Cut the first / and the characters left beside it"

    echo ${FILE#*/}  #dir2/dir3/my.file.txt

    echo "Cut the last / and the characters left beside it"

    echo ${FILE##*/} #my.file.txt

    echo "No function,just put out the source string"

    echo ${FILE#/*}

    echo "Cut the first / and the characters right beside it"

    echo ${FILE%/*}  #dir1/dir2/dir3

    echo "Cut the last / and the characters right beside it"

    echo ${FILE%%/*}  #dir1

    echo "No effect, just put out the source string"

    echo ${FILE%*/}

    #Remember like this

    ## left

    #% right

    #one first

    #two last

     

    FILE2=my.file.txt

    echo "file name"

    echo ${FILE2%%.*}

    echo "suffix"

    echo ${FILE2##*.}

     

    Result

     The source string

    dir1/dir2/dir3/my.file.txt

    Cut the first / and the characters left beside it

    dir2/dir3/my.file.txt

    Cut the last / and the characters left beside it

    my.file.txt

    No function,just put out the source string

    dir1/dir2/dir3/my.file.txt

    Cut the first / and the characters right beside it

    dir1/dir2/dir3

    Cut the last / and the characters right beside it

    dir1

    No effect, just put out the source string

    dir1/dir2/dir3/my.file.txt

    file name

    my

    suffix

    txt

     

    二、Realize

    [braveyly@m-net ~/bakfilerename]$ ls

    1.c            2.c            3.c             4.c          

     

     

    #!/bin/bash

    for file in$(find . -name "*.c" -type f)

    do

    echo $file

    echo "${file%%.*}" # print null ?

    echo "${file%%.*}.o"

    echo "${file%.c}.o" # cut the first . and the c right beside it ????

    mv "$file" "${file%.c}.o"

    done

     

    [braveyly@m-net ~/bakfilerename]$ ./rename.sh

    ./1.c

    .o

    ./1.o

    ./2.c

    .o

    ./2.o

    ./3.c

    .o

    ./3.o

    ./4.c

    .o

    ./4.o

     


    最新回复(0)