如何自动删除文件名中的文件扩展名 [英] How to automatically remove the file extension in a file name
问题描述
我正在尝试使用bash编写一个脚本,该脚本要求从文件名中删除文件扩展名,如下所示
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
我当时想我可以使用cut,但是我担心这样的情况会出现,即文件名可能包含多个句点,等等,类似于以下内容
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!
推荐答案
f=file.zip
echo ${f%.zip}
file
'%'是参数修饰符,表示从'%'字符后的变量值的右侧删除,在此情况下为字符串.zip
.您可以使用
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
这篇关于如何自动删除文件名中的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!