将目录中的所有文件重命名为与目录相同

发布于 2025-01-10 04:20:15 字数 215 浏览 4 评论 0原文

我有一个名为“tokens”的文件夹。在这个文件夹中,我还有其他几个文件夹,称为“guard”、“noble”、“assassin”等。这些文件夹中是具有不同名称和文件格式的图像文件。

我想遍历“令牌”中的所有文件夹和文件,并将每个文件重命名为它所在的任何目录,并附加一个数字。因此,“guard”文件夹中的文件将命名为“guard0001.jpg”,下一个文件将命名为“guard0002.png”。

I have a folder called "tokens". In this folder, I have several other folders, called things like "guard", "noble", "assassin", etc. In those folders are image files with varying names and file formats.

I would like to run through the all the folders and files in "tokens", and rename each file to be whatever directory it is in with a number appended to it. So, a file in the "guard" folder would be named "guard0001.jpg", and the next would be "guard0002.png", for instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手心的海 2025-01-17 04:20:15

当然有更好/更快的方法,但这应该可以解决问题。

find tokens -type f -exec sh -c "path='{}'"'; file="${path##*/}"; dir="${path%/*}"; folder="${dir##*/}"; echo mv "$path $dir/$folder$file"' \;

当您对它产生的输出感到满意时,删除echo

There are certainly better/faster approaches but this should do the trick.

find tokens -type f -exec sh -c "path='{}'"'; file="${path##*/}"; dir="${path%/*}"; folder="${dir##*/}"; echo mv "$path $dir/$folder$file"' \;

Remove the echo when you're satisfied with the output it produced.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文