通过目录击循环和重命名每个文件 [英] Bash loop through directory and rename every file
问题描述
我在写作的bash脚本可怕,但我不知道是否有可能递归遍历目录并通过1.png,2.png,重命名等在那里的所有文件,但我需要它须在重新启动进入每一个新的文件夹。这里的脚本作品,但只有它会为一个目录。
I am horrible at writing bash scripts, but I'm wondering if it's possible to recursively loop through a directory and rename all the files in there by "1.png", "2.png", etc, but I need it to restart at one for every new folder it enters. Here's script that works but only does it for one directory.
cd ./directory
cnt=1
for fname in *
do
mv $fname ${cnt}.png
cnt=$(( $cnt + 1 ))
done
在此先感谢
修改
谁能真正写这篇code呢?我不知道怎么写的bash,这是非常令人困惑我
EDIT Can anyone actually write this code out? I have no idea how to write bash, and it's very confusing to me
推荐答案
使用find是一个好主意。您可以使用查找下一个语法来找到你的目录中的所有目录,并应用脚本来找到目录:
Using find is a great idea. You can use find with the next syntax to find all directories inside your directory and apply your script to found directories:
find /directory -type d -exec youscript.sh {} \;
型d参数意味着你只想要查找的目录
-type d parameter means you want to find only directories
-exec youscript.sh {} \\;开始你的脚本,每发现目录,并将它传递这个目录名作为参数
-exec youscript.sh {} \; starts your script for every found directory and pass it this directory name as a parameter
这篇关于通过目录击循环和重命名每个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!