How to use sed to change file extensions?

前端 未结 6 706
粉色の甜心
粉色の甜心 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:08

    This may work:

    find . -name "*.txt" | 
    sed -e 's|./||g' | 
    awk '{print "mv",$1, $1"c"}' | 
    sed -e "s|\.txtc|\.c|g" > table;
    chmod u+x table;
    ./table
    

    I don't know why you can't use a loop. It makes life much easier :

    newex="c";  # Give your new extension
    for file in *.*;  # You can replace with *.txt instead of *.*
    do 
     ex="${file##*.}";    # This retrieves the file extension
     ne=$(echo "$file" | sed -e "s|$ex|$newex|g"); # Replaces current with the new one
     echo "$ex";echo "$ne";
     mv "$file" "$ne";
    done
    

提交回复
热议问题