如何删除 mac 上第一个点之后的文件名的所有部分
How to remove all parts of a filename after the first dot on mac
我正尝试在 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 变量替换,您可以实现以下目标:
for i in file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3
do
echo "${i/.*./.}"
done
${i/.*./.}
表示用.
代替.*.
。也就是说,它将匹配 file1.some.stuff.mp3
中的 .some.stuff.
和 a.b.c.d.e
中的 .b.c.d.
。
在man bash
的参数扩展部分:
${parameter/pattern/string}
Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is
expanded and the longest match of pattern against its value is replaced with string.
for i in `ls *.mp3`; do NAMES=`echo $i|cut -d. -f1`; mv "$i" "$NAMES.mp3"; done
我正尝试在 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 变量替换,您可以实现以下目标:
for i in file1.some.stuff.mp3 file2.other.stuff.stuff.mp3 file3.some.thing.mp3
do
echo "${i/.*./.}"
done
${i/.*./.}
表示用.
代替.*.
。也就是说,它将匹配 file1.some.stuff.mp3
中的 .some.stuff.
和 a.b.c.d.e
中的 .b.c.d.
。
在man bash
的参数扩展部分:
${parameter/pattern/string} Pattern substitution. The pattern is expanded to produce a pattern just as in pathname expansion. Parameter is expanded and the longest match of pattern against its value is replaced with string.
for i in `ls *.mp3`; do NAMES=`echo $i|cut -d. -f1`; mv "$i" "$NAMES.mp3"; done