gpt4 book ai didi

bash - 如何在 mac 上删除文件名第一个点之后的所有部分

转载 作者:行者123 更新时间:2023-12-02 07:59:03 26 4
gpt4 key购买 nike

我正在尝试在 mac 终端上执行此操作

我想去除文件名第一个点之后的所有字段,但在末尾保留扩展名,并且可以有任意长度的点。

输入:

file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3 file4.one.two.three.four.mp3

预期输出:

file1.mp3 file2.mp3 file3.mp3 file4.mp3

我将所有文件都放在同一个文件夹中。

最佳答案

只要保证文件名至少有一个 . 并且在 . 前后有非空字符串,这是一个简单的参数扩展问题。

for f in file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3 file4.one.two.three.four.mp3; do
echo "${f%%.*}.${f##*.}"
done

产生

file1.mp3
file2.mp3
file3.mp3
file4.mp3

在文件退出的目录中,使用类似的东西

for f in *.mp3; do

遍历所有 MP3 文件。

关于bash - 如何在 mac 上删除文件名第一个点之后的所有部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59773658/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com