How to automatically remove the file extension in a file name

前端 未结 2 712
你的背包
你的背包 2021-01-20 03:53

I am trying to make a script in bash that requires the removal of the the file extension from a file name, like the following

original:   something.zip
remov         


        
相关标签:
2条回答
  • 2021-01-20 04:28
     f=file.zip
     echo ${f%.zip}
    
     file
    

    The '%' is a parameter modifier, it means, delete from the right side of the value of the variable whatever is after the '%' char, in this case, the string .zip. You can make this more general to remove any trailing extension, by using a wild card like

     echo ${f%.*}
    
     file
    

    IHTH

    0 讨论(0)
  • 2021-01-20 04:34

    If you want to remove the from the last period to the end, try this:

    $ f=some.thing.zip
    $ echo ${f%.*}
    some.thing
    
    0 讨论(0)
提交回复
热议问题