How to use sed to change file extensions?

前端 未结 6 755
粉色の甜心
粉色の甜心 2021-01-01 06:33

I have to do a sed line (also using pipes in Linux) to change a file extension, so I can do some kind of mv *.1stextension *.2ndextension like mv *.txt *.

6条回答
  •  情话喂你
    2021-01-01 07:15

    You can use find to find all of the files and then pipe that into a while read loop:

    $ find . -name "*.ext1" -print0 | while read -d $'\0' file
    do
       mv $file "${file%.*}.ext2"
    done
    

    The ${file%.*} is the small right pattern filter. The % marks the pattern to remove from the right side (matching the smallest glob pattern possible), The .* is the pattern (the last . followed by the characters after the .).

    The -print0 will separate file names with the NUL character instead of \n. The -d $'\0' will read in file names separated by the NUL character. This way, file names with spaces, tabs, \n, or other wacky characters will be processed correctly.

提交回复
热议问题