bash 查找上一级目录名称的更改名称文件

bash find change name file for directory name 2 levels up

您好,感谢阅读!
我有这样的清单:

user@laptop:~/lol$ find ./ \( -iname 'latest*' \) -type f -printf "%p\n"
/leagueoflegends/images/e/e6/CaitlynSquare.png/revision/latest?cb=20150402215548
/leagueoflegends/images/e/ef/WillumpSquare.png/revision/latest?cb=20150402222523
/leagueoflegends/images/d/d8/MorganaSquare.png/revision/latest?cb=20150402220702 
/leagueoflegends/images/d/d1/UdyrSquare.png/revision/latest?cb=20150402221631

(更长)我想将每个图像("latest?cb=20150402221631" 是 png 图像)复制到另一个文件夹并将其重命名为 2 级目录的名称,如下所示:

latest?cb=20150402221631 --> to --> UdyrSquare.png

如果可能,删除 Square。

latest?cb=20150402221631 --> to --> Udyr.png

如果您有 Perl rename 命令(在您的发行版中可能是 prenameperl-rename),您可以使用正则表达式来重命名文件。它可以从输入中读取文件名:

find . -iname 'latest*' -type f | rename -n ':Square.png.*cb=\d+:.png:'

或者,更具体地说:

find . -iname 'latest*' -type f | rename -n 's:Square.png/[^/]*/latest\?cb=\d+:.png:'

例如:

$ cat foo.txt| perl-rename -n 's:Square.png/[^/]*/latest\?cb=\d+:.png:'      
/leagueoflegends/images/e/e6/CaitlynSquare.png/revision/latest?cb=20150402215548 -> /leagueoflegends/images/e/e6/Caitlyn.png
/leagueoflegends/images/e/ef/WillumpSquare.png/revision/latest?cb=20150402222523 -> /leagueoflegends/images/e/ef/Willump.png
/leagueoflegends/images/d/d8/MorganaSquare.png/revision/latest?cb=20150402220702  -> /leagueoflegends/images/d/d8/Morgana.png 
/leagueoflegends/images/d/d1/UdyrSquare.png/revision/latest?cb=20150402221631 -> /leagueoflegends/images/d/d1/Udyr.png

-n选项用于测试命令。一旦您对结果感到满意,运行 没有它。

与bash:

追加:

| while read -r line; do echo mv -v "$line" "${line%Square*}.png"; done

如果一切正常,删除 echo