Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-24-2015, 07:29 AM   #1
LQ Newbie
Registered: Apr 2015
Posts: 2

Rep: Reputation: Disabled
Script To Recursively Enter Subdirectories And Rename Files Sequentially From Scratch

I am new to Bash scripting.

I have a main directory called Photos which has many subdirectories like People, Places and Things. Each of these subdirectories is populated by other subdirectories and lots of JPG photo images.

The digital cameras name the files in a way that is difficult to manage with web hosting.

I would like to go to each directory and subdirectory and rename the photos 1.jpg, 2.jpg, 3.jpg, etc. so that I can use a simple XML template to access them by specifying only a hosting directory.

I tried to use the following script:

#! /bin/bash

cd /home/paul/test

find . -name "*.jpg" -print0 | rename -v 's/.+/our $i; sprintf("%d.jpg", 1+$i++)/e' * -vn

exit 0

It successfully renames all of the files in all of the directories, but it does not restart the numbering for each new subdirectory. So first it goes through Photos and renames the three JPG files there 1.jpg, 2.jpg and 3. jpg, and then it opens the first subdirectory People and names the three JPG files there 4.jpg, 5.jpg and 6.jpg. Next it moves to the next subdirectory and continues sequential renaming until it is done.

I want it to restart sequential renaming with each new subdirectory, so that after renaming the three JPG files in Photos to 1.jpg, 2.jpg and 3.jpg, it moves to the first subdirectory and renames the JPG files there starting with 1.jpg again.

That way I use the links 1.jpg, 2.jpg, 3.jpg, etc in the XML template and just change the directory name to download the photos from the web.

Thanks for any help you can give me.
Old 04-24-2015, 09:57 AM   #2
Ragnarok Warrior
Registered: Nov 2005
Location: Bangor, ME
Distribution: Debian,Ubuntu,DSL
Posts: 78

Rep: Reputation: 18
You should just need to do a loop where when it leaves a directory its starts the naming all over again.
 for <args> 
<your stuff>
Here is a sample of bash scripting that is used in BLFS 7.6 ( ), it unpacks tarballs, changes to their directories, and configure/make/installs them in the order of a .md5 text file, while this is far from what you are trying to accomplish, some of the logic is sound. Just do some research on what commands are being run and why. From Section VI, Chapter 24, Xorg Libraries:

for package in $(grep -v '^#' ../lib-7.7.md5 | awk '{print $2}')
  tar -xf $package
  pushd $packagedir
  case $packagedir in
    libXfont-[0-9]* )
      ./configure $XORG_CONFIG --disable-devel-docs
    libXt-[0-9]* )
      ./configure $XORG_CONFIG \
    * )
      ./configure $XORG_CONFIG
  as_root make install
  rm -rf $packagedir
  as_root /sbin/ldconfig
Old 04-24-2015, 07:37 PM   #3
LQ Newbie
Registered: Apr 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Stuck on Looping.

Thanks for your reply. I can get rename and find to work through all the subdirectories, but I can't find a way to make rename restart sequential numbering in each subdirectory.

It looks like I need a script to:

1. Identify all subdirectories (maxdepth = 5)
2. Open the first subdirectory and use rename on all the JPG files.
3. Move on to the next subdirectory and restart the renaming on any JPG files there.
4. Keep going until all of the JPG files have been renamed.

This has got to be easy to do, but I can't figure it out.

I can find all of the sudirectories without a problem, but I can't come up with a looping scheme that will open them one by one to let rename start from scratch.
Old 04-25-2015, 09:05 AM   #4
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,661

Rep: Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256Reputation: 1256
You can still use find - just use the option to specify directories ("-type d"), then use that output to do a separate rename for the contents of each directory.

Something like:
find . -type d -print | (
  while read dir; do
    cd $dir
    find *.jpg -print0 | rename -v 's/.+/our $i; sprintf("d.jpg", 1+$i++)/e' * -vn
    cd $h


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively tail follow all files in a directory, all subdirectories, and so on... dragonmaximadriver Linux - General 8 01-14-2015 08:43 AM
[SOLVED] How do I rename files/folders recursively using a simple script? ixcel87 Programming 5 07-11-2012 11:02 AM
Problem running bash script recursively over a number of subdirectories using osx ghadley_00 Linux - Software 2 09-06-2011 10:38 AM
script to rename files recursively rs232 Linux - General 3 05-08-2011 02:05 PM
[SOLVED] rename several files sequentially docaia Programming 4 04-13-2011 04:42 AM

All times are GMT -5. The time now is 01:24 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration