Register
It is currently Wed Apr 23, 2014 8:51 pm

Help needed with script to rename all files in directory


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Thu Mar 29, 2012 3:39 pm   

Joined: Thu Mar 29, 2012 3:16 pm
Posts: 2
I'm a bit of a newbie at bash script so appologies if this is a "schoolboy" question...

I need to create a script that will rename all files ending in '.jpg' in the current directory to '29-04-11-{current filename}'

I can run the following code at the terminal and it does what I want...

Code:
for x in *.jpg; do mv "$x" "29-04-11-$x"; done


...but when I create a script with the same command in...

Code:
#!/bin/bash
#
# Script to rename all image files in the current directory
# to something like "29-04-11-{original filename}"
#
for x in *.jpg;
do
   mv "$x" "29-04-11-$x"
done


...it fails with a 'command not found'. This is the terminal output showing the read/write/execute bits set alright...

Code:
-r--r--r-- 1 mark mark 793344 2010-08-27 18:58 Bike_3D_small.jpg
-r--r--r-- 1 mark mark 688828 2010-08-27 18:57 L1010071_small.jpg
-r--r--r-- 1 mark mark 680152 2010-08-27 18:57 L1010072_small.jpg
-rwxrwxr-x 1 mark mark    177 2012-03-29 22:07 rename_images.sh
mark@Linux-1:~/script-test$ rename_images.sh
rename_images.sh: command not found
mark@Linux-1:~/script-test$


I know this is probably down to something simple but I just can't figure out why it won't run :-/

Once it's working I want to make the '29-04-11' part a variable that's passed into the script.
Could somebody point me in the right direction please.


Top
 Profile  
 PostPosted: Thu Mar 29, 2012 7:49 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hello Mark!

Is the script executable? Try running it like this:

Code:
/bin/bash -x rename_images.sh


This does two things... First, you're executing the script directly via /bin/bash. Second, if you run it with "-x" and it still has errors, you can see step by step what it's doing, and where it's failing.

I hope this helps!


Top
 Profile YIM  
 PostPosted: Fri Mar 30, 2012 6:03 am   

Joined: Thu Mar 29, 2012 3:16 pm
Posts: 2
Thanks Jeo,
adding the /bin/bash before it on the command line worked :)
but I don't really understand why? I thought that the #!/bin/bash at the top of the script told it where to run?
...maybe there's something wrong with my bashrc file?

The -x parameter didn't show any errors.


Top
 Profile  
 PostPosted: Sat Mar 31, 2012 8:36 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Mark Williamson wrote:
but I don't really understand why? I thought that the #!/bin/bash at the top of the script told it where to run?


Apparently, you missed this question, or thought it was connected with jeo's given command:
jeo wrote:
Is the script executable?



You can't execute your script because it's not executable.
To make it executable, you have to use chmod(1).

Code:
chmod +x rename_images.sh


Top
 Profile  
 PostPosted: Tue Apr 24, 2012 6:25 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Hi,

You are calling the script like so;
Code:
mark@Linux-1:~/script-test$ rename_images.sh


however if the directory script-test is not in your path then it'll not find the rename_images.sh script to run it, giving the error you found. Call the script with the path and it'll be fine;

Code:
mark@Linux-1:~/script-test$ ./rename_images.sh


Note the "./" at the start of the script name.

DW


Top
 Profile  
 PostPosted: Wed May 02, 2012 3:57 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Oh yeah, that too.

@Mark Williamson: you can check out a perl script called rename(1) and mmv(1).


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
cron


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