Script To Recursively Enter Subdirectories And Rename Files Sequentially From Scratch
Linux - NewbieThis 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!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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.
You should just need to do a loop where when it leaves a directory its starts the naming all over again.
Code:
for <args>
do
<your stuff>
Here is a sample of bash scripting that is used in BLFS 7.6 ( http://www.linuxfromscratch.org/blfs...-nochunks.html ), 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:
Code:
for package in $(grep -v '^#' ../lib-7.7.md5 | awk '{print $2}')
do
packagedir=${package%.tar.bz2}
tar -xf $package
pushd $packagedir
case $packagedir in
libXfont-[0-9]* )
./configure $XORG_CONFIG --disable-devel-docs
;;
libXt-[0-9]* )
./configure $XORG_CONFIG \
--with-appdefaultdir=/etc/X11/app-defaults
;;
* )
./configure $XORG_CONFIG
;;
esac
make
as_root make install
popd
rm -rf $packagedir
as_root /sbin/ldconfig
done
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.
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:
Code:
h=`pwd`
find . -type d -print | (
while read dir; do
cd $dir
i=0
find *.jpg -print0 | rename -v 's/.+/our $i; sprintf("d.jpg", 1+$i++)/e' * -vn
cd $h
done
)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.