Lui*_*rzi 5 find xargs mv parallelism
我在修改Music/
目录中的文件名时遇到问题。
我有一个像这样的名字列表:
$ ls
01 American Idiot.mp3
01 Articolo 31 - Domani Smetto.mp3
01 Bohemian rapsody.mp3
01 Eye of the Tiger.mp3
04 Halo.mp3
04 Indietro.mp3
04 You Can't Hurry Love.mp3
05 Beautiful girls.mp3
16 Apologize.mp3
16 Christmas Is All Around.mp3
Adam's song.mp3
A far l'amore comincia tu.mp3
All By My Self.MP3
Always.mp3
Angel.mp3
Run Code Online (Sandbox Code Playgroud)
和类似的,我想删除文件名前面的所有数字(不是扩展名中的 3)。
我首先尝试grep
只使用带有find -exec
或编号的文件,xargs
但即使在第一步我也没有成功。能够之后grep
我想做实际的名称更改。
这是我现在尝试过的:
ls > try-expression
grep -E '^[0-9]+' try-expression
Run Code Online (Sandbox Code Playgroud)
通过以上我得到了正确的结果。然后我尝试了下一步:
ls | xargs -0 grep -E '^[0-9]+'
ls | xargs -d '\n' grep -E '^[0-9]+'
find . -name '[0-9]+' -exec grep -E '^[0-9]+' {} \;
ls | parallel bash -c "grep -E '^[0-9]+'" - {}
Run Code Online (Sandbox Code Playgroud)
类似,但我收到了“文件名太长”或根本没有输出之类的错误。我想问题是我在单独命令中使用xargs
或find
作为表达式的方式工作得很好。
感谢您的帮助
要列出目录中所有以数字开头的文件,
find . -maxdepth 1 -regextype "posix-egrep" -regex '.*/[0-9]+.*\.mp3' -type f
Run Code Online (Sandbox Code Playgroud)
您的方法的问题在于find
返回文件的相对路径,而您只需要一个文件名本身。
这是您可以使用 only 做的事情bash
,在条件中使用正则表达式:
#! /bin/bash
# get all files that start with a number
for file in [0-9]* ; do
# only process start start with a number
# followed by one or more space characters
if [[ $file =~ ^[0-9]+[[:blank:]]+(.+) ]] ; then
# display original file name
echo "< $file"
# grab the rest of the filename from
# the regex capture group
newname="${BASH_REMATCH[1]}"
echo "> $newname"
# uncomment to move
# mv "$file" "$newname"
fi
done
Run Code Online (Sandbox Code Playgroud)
在您的示例文件名上运行时,输出为:
< 01 American Idiot.mp3
> American Idiot.mp3
< 01 Articolo 31 - Domani Smetto.mp3
> Articolo 31 - Domani Smetto.mp3
< 01 Bohemian rapsody.mp3
> Bohemian rapsody.mp3
< 01 Eye of the Tiger.mp3
> Eye of the Tiger.mp3
< 04 Halo.mp3
> Halo.mp3
< 04 Indietro.mp3
> Indietro.mp3
< 04 You Can't Hurry Love.mp3
> You Can't Hurry Love.mp3
< 05 Beautiful girls.mp3
> Beautiful girls.mp3
< 16 Apologize.mp3
> Apologize.mp3
< 16 Christmas Is All Around.mp3
> Christmas Is All Around.mp3
Run Code Online (Sandbox Code Playgroud)
在 Debian、Ubuntu 和衍生产品上,使用rename
perl 脚本。
模拟重命名操作:
rename 's/^\d+ //' * -n
Run Code Online (Sandbox Code Playgroud)
删除-n
(无行为)以执行操作:
rename 's/^\d+ //' *
Run Code Online (Sandbox Code Playgroud)
幸运的是 perl rename 也安装/usr/bin/rename
在你的发行版上(有传言说 Fedora 也使用 perl rename)。
有关其他功能的更多详细信息,请参阅perl 重命名手册页。