简体   繁体   English

如何自动删除文件名中的文件扩展名

[英]How to automatically remove the file extension in a file name

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我正在尝试在 bash 中创建一个脚本,该脚本需要从文件名中删除文件扩展名,如下所示

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我想我可以为此使用 cut ,但我担心可能会出现这样的情况,即文件名可能有多个句点等,类似于以下内容

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!任何帮助将不胜感激,谢谢!

 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 . '%' 是一个参数修饰符,这意味着从变量值的右侧删除 '%' 字符之后的任何内容,在本例中为字符串.zip You can make this more general to remove any trailing extension, by using a wild card like您可以使用通配符,例如

 echo "${f%.*}"

 file

If you want to remove the from the last period to the end, try this:如果你想从最后一个时期删除到最后,试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM