Register
It is currently Wed Nov 26, 2014 11:22 am

Recursively Execute Command


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Thu Jan 28, 2010 1:59 pm   

Joined: Thu Jan 28, 2010 1:48 pm
Posts: 1
We have over 1000 media files that need to be renamed by executing a pre-existing rename command (rename_adi). Each media file is in it's own directory. Instead of manually going into each directory I am hoping to create a script that will 'find' each folder and then go into each folder and execute our 'rename' command. - any help is appreciated. Thanks


Top
 Profile  
 PostPosted: Fri Jan 29, 2010 1:06 pm   

Joined: Thu Jan 21, 2010 7:23 am
Posts: 6
So, here is a little script that should do the trick, BUT:
1- I am not too familiar with the 'find' command, so bare with me. I am sure there is a simpler way to do this
2- be careful from where you start.
3- the command will be executed in the dir from where you start too. I don't know if this is a problem for you, but I couldn't do it otherwise...

Code:
#!/bin/bash
#===============================================================================
#
#          FILE:  recdir.sh
#
#         USAGE:  ./recdir.sh
#
#   DESCRIPTION:  will go in every directory and subdirectory starting from where you are and execute command
#
#       OPTIONS:  ---
#  REQUIREMENTS:  ---
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR: iesaya (---), ---
#       COMPANY: ---
#       CREATED: 01/29/2010 11:50:44 AM CET
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error

sdir=`pwd` #starting point; it is important that you start at the right place
dir=`find . -type d` #will save the sub/directory tree in var

for i in $dir; do
  cd $i #go into directory
  touch i.was.here #exec command; here change to your rename command
  cd $sdir #go back to start
done



Top
 Profile  
 PostPosted: Tue Feb 02, 2010 4:46 pm   

Joined: Thu Jan 21, 2010 7:23 am
Posts: 6
I don't know, if you are still on this, but here is something that might help... As you told me, that the first script didn't work as expected, I tried to come up with something else. I was actually on the right track with my comment #will save the sub/directory tree in var ... Well, why don't use 'tree' ?!
Instead of using
Code:
dir=`find . -type d`

try this:
Code:
dir=`tree -dfi --noreport`

Again, be careful from where you start. Hope this one will work to your satisfaction.
Best Regards,
iesaya


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP