2

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

Thanks in advance

EDIT Can anyone actually write this code out? I have no idea how to write bash, and it's very confusing to me

2
  • add a test for each $fname, see if it is a directory, then call this script again, with the new directory as the cd target. Also, if this is anything of size, you'll want to read about formatting variables, so you can generate numbers like 001-999 (with the leading zeros) without having to manage prepending strings of 00's in front of your $cnt value. Otherwise you're stuck trying to figure out how to sort 1.png 10.png 2.png ... Good luck. Commented Nov 15, 2011 at 3:40
  • The sorting is not an issue, and I would never have a reason to sort them, but I'm actually looking for someone to write this out, as I have no clue how to write bash and I just get lost looking at it Commented Nov 15, 2011 at 3:49

2 Answers 2

3

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 {} \;

-type d parameter means you want to find only directories

-exec youscript.sh {} \; starts your script for every found directory and pass it this directory name as a parameter

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! That worked amazingly. It included the original folder that it was in, but I fixed that with a simple conditional
I think you can use -mindepth 1 parameter to exclude the parent folder, but your find could not have this parameter. Look in man page for find command in your system or just test it.
I know this post is super old, but I had a question. I am doing something similar, but for some reason I get the error find: myscript.sh: No such file or directory. I am calling the command from inside my parent directory. myscript.sh is also in this directory. Any ideas? @stee1rat
1

Use find(1) to get a list of files, and then do whatever you like with that list.

3 Comments

Sorry, but I have no idea what that means D:
@stumpx - did you read the find man page? What questions do you have? It should be pretty straightforward - just replace the * in your script with some combination of find and its arguments. In your case, maybe $(find . -type f). Watch out for spaces in filenames. Your original script doesn't handle that properly either, though.
Another problem, it's renaming the folders instead of going into them and changing the file names

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.