第一次和第二次调用是为了进行比较。这是我第三次尝试去上班。
$ ls -a1 *mp3
DaftPunk_VeridisQuo.mp3
French79_AfterParty.mp3
French79_BetweentheButtons.mp3
French79_Hometown.mp3
$ find . -maxdepth 1 -type f -name '*mp3'
./French79_AfterParty.mp3
./French79_Hometown.mp3
./DaftPunk_VeridisQuo.mp3
./French79_BetweentheButtons.mp3
$ for x in "$(ls -a *mp3)"; do mv "./$x" "./electro_$x"; done
mv: cannot stat './DaftPunk_VeridisQuo.mp3'$'\n''French79_AfterParty.mp3'$'\n''French79_BetweentheButtons.mp3'$'\n''French79_Hometown.mp3': No such file or directory
答案1
我更喜欢直接使用它:
for x in *.mp3
do
mv ./"$x" "electro_$x"
done
答案2
通过引用$(..)
,您将得到一个标记,而不是 N 个标记:
我从几个示例文件开始:
$ ls
a.mp3 b.mp3 c.mp3
如果我按照你的做法,我会得到这三行的一行:
for i in "$(ls *.mp3)"; do
echo "--> $i"
done
--> a.mp3 b.mp3 c.mp3
如果我省略 周围的引号$(...)
,我会得到三个不同的输出行:
for i in $(ls *.mp3); do
echo "--> $i"
done
--> a.mp3
--> b.mp3
--> c.mp3
如果您的文件带有空格,那么类似这样的事情可能会解决您的问题(请注意,这仅适用于当前目录中的文件):
前
$ ls
'DaftPunk VeridisQuo.mp3' 'French79 AfterParty.mp3' 'French79 BetweentheButtons.mp3' 'French79 Hometown.mp3'
用于find
重命名:
$ find *.mp3 -maxdepth 1 -type f -name *.mp3 -exec mv {} "electro_{}" \;
$ ls
'electro_DaftPunk VeridisQuo.mp3' 'electro_French79 AfterParty.mp3' 'electro_French79 BetweentheButtons.mp3' 'electro_French79 Hometown.mp3'
为什么我建议find *.mp3
而不是简单地建议find . -type f -name '*.mp3' ...
?
$ find . -maxdepth 1 -type f -name '*.mp3' -exec mv {} "electro_{}" \;
mv: cannot move './French79 Hometown.mp3' to 'electro_./French79 Hometown.mp3': No such file or directory
mv: cannot move './French79 BetweentheButtons.mp3' to 'electro_./French79 BetweentheButtons.mp3': No such file or directory
mv: cannot move './French79 AfterParty.mp3' to 'electro_./French79 AfterParty.mp3': No such file or directory
mv: cannot move './DaftPunk VeridisQuo.mp3' to 'electro_./DaftPunk VeridisQuo.mp3': No such file or directory
答案3
您想要做的是一个简单的批量重命名,可以通过 perlrename
实用程序(又名prename
或file-rename
)轻松处理。这是不是rename
与包中的实用程序相同util-linux
(具有完全不同且不兼容的命令行选项和功能)。
尝试
rename -n 's/^/electro_/' *.mp3
这-n
选项使其成为一次试运行,只会展示如果您允许,您将如何重命名 .mp3 文件。要实际重命名它们,请删除-n
或将其替换-v
为详细输出。
$ touch DaftPunk_VeridisQuo.mp3 French79_AfterParty.mp3 French79_BetweentheButtons.mp3
$ ls -l
total 2
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 DaftPunk_VeridisQuo.mp3
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 French79_AfterParty.mp3
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 French79_BetweentheButtons.mp3
$ rename -v 's/^/electro_/' *.mp3
DaftPunk_VeridisQuo.mp3 renamed as electro_DaftPunk_VeridisQuo.mp3
French79_AfterParty.mp3 renamed as electro_French79_AfterParty.mp3
French79_BetweentheButtons.mp3 renamed as electro_French79_BetweentheButtons.mp3
$ ls -l
total 2
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 electro_DaftPunk_VeridisQuo.mp3
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 electro_French79_AfterParty.mp3
-rw-r--r-- 1 cas cas 0 Oct 21 14:30 electro_French79_BetweentheButtons.mp3