How to automatically remove the file extension in a file name

有些话、适合烂在心里 提交于 2019-12-02 03:36:54

问题


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
removed version: something

And I was thinking I could use cut for this, but I am worried that a situation could arise where there might be a file name that has more than one period, etc, something similar to the following

something.v2.zip

having said that, I was wondering if anyone had any recommendations as to what I could do to just remove the last period and the text after it from a line of text/filename? any help would be appreciated, thanks!


回答1:


 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




回答2:


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

$ f=some.thing.zip
$ echo ${f%.*}
some.thing


来源:https://stackoverflow.com/questions/13337953/how-to-automatically-remove-the-file-extension-in-a-file-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!