简体   繁体   English

递归查找,它将目录名附加到任何文件

[英]Recursive find that will append directory name to any file

Any help would be greatly appreciated! 任何帮助将不胜感激!

I need a way to append the parent directory name to any file in any path. 我需要一种将父目录名称附加到任何路径中的任何文件的方法。

An example current directory tree 当前目录树示例

/Hawaii/Surfing/800x600/picture1.jpg  
/Hawaii/Surfing/800x600/picture2.jpg  
/Hawaii/Surfing/800x600/picture3.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.jpg

The goal 目标

/Hawaii/Surfing/800x600/picture1.800x600.jpg  
/Hawaii/Surfing/800x600/picture2.800x600.jpg   
/Hawaii/Surfing/800x600/picture3.800x600.jpg  
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture1.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture2.4096x2160.jpg
/RockClimbing/SouthAfrica/TableMountain/4096x2160/Picture3.4096x2160.jpg

I have found some examples of this but the users all have set directory depths unfortunately I have files at many different levels. 我已经找到了一些示例,但是不幸的是,所有用户都设置了目录深度,但是我有许多不同级别的文件。

find dir -name *.jpg -exec rename -nv -- 's|/(.*)/(.*)$|/$1/$1.jpg|' {}

Your first capture group is matching everything before the last / , not just the last directory name. 您的第一个捕获组将匹配最后一个/之前的所有内容,而不仅仅是最后一个目录名称。 Use /([^/]*)/ instead of /(.*)/ so it won't match across / delimiters. 使用/([^/]*)/而不是/(.*)/这样它就不会与/分隔符匹配。 You're also not splitting up the filename into the name and extension, so you're not inserting the directory name between them. 您也没有将文件名分为名称和扩展名,因此也没有在它们之间插入目录名称。

find dir -name *.jpg -exec rename -nv -- 's|([^/]*)/([^/]*)\.jpg$|$1/$2.$1.jpg|' {} +

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM