LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 03-24-2005, 09:29 AM   #1
merlin23
Member
 
Registered: Dec 2004
Location: Vienna
Posts: 46

Rep: Reputation: 15
rename all files in a directory


does any one has an idea how I can rename all files in a directory like file1 - file1000 for example?

I have many files all with different names and I want them to have all the same beginning in the name, appended with a counter , so file1, file2, file3 ....

Is there an easy way to do this?
I work with Bash-shell....
 
Old 03-24-2005, 09:43 AM   #2
Itzac
Member
 
Registered: Feb 2003
Distribution: VectorLinux 5.1
Posts: 116

Rep: Reputation: 15
You'll want to man bash to find out more about bash scripting. You may also find grep or egrep useful, depending on what information you want to pick out of the current file names. Finally, use the mv command to rename a file.
 
Old 03-24-2005, 09:58 AM   #3
merlin23
Member
 
Registered: Dec 2004
Location: Vienna
Posts: 46

Original Poster
Rep: Reputation: 15
could it be something similar like this:


#/bin/sh
cnt=0
for i in 'ls'; cnt++;do mv $i $cnt.jpg;


somehow, the syntax don't seem to be right...

the error-message:

rename_all.sh: line 3: syntax error near unexpected token `cnt++'
rename_all.sh: line 3: `for i in 'ls'; cnt++;do mv $i $cnt.jpg;'


mybe somebody helps me a little with this script?
 
Old 03-24-2005, 10:16 AM   #4
merlin23
Member
 
Registered: Dec 2004
Location: Vienna
Posts: 46

Original Poster
Rep: Reputation: 15
ok, figured something out like this:

#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done

But somehow, not all files get renamed...

If I try it with the mv command, like this:


#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done


I get lots of errors:

mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
...


Why???
Please help me and give me a hint, I feel really lost...
 
Old 03-24-2005, 11:02 AM   #5
ahh
Member
 
Registered: May 2004
Location: UK
Distribution: Gentoo
Posts: 293

Rep: Reputation: 31
Quote:
Originally posted by merlin23
[B]If I try it with the mv command, like this:

#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
rename $i pic$cnt.jpg
done

I get lots of errors:

mv: when moving multiple files, last argument must be a directory
Try `mv --help' for more information.
I presume you mean
Code:
#/bin/sh
cnt=0
for i in ./*; do
let cnt=cnt+1
mv $i pic$cnt.jpg
done
This works on my system.

Do you have file names with spaces in? This would explain why mv thinks you are trying to rename multiple files, as it uses a space as a delimiter between files.

One other point, it looks as though you are running this in the directory where the files are, this means your shell script will be renamed too. It may take a bit of finding afterwards!

Useful tip:- If you want to know what is happening inside a shell script use #!/bin/bash -x

Last edited by ahh; 03-24-2005 at 11:06 AM.
 
  


Reply


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
Bash - rename directory to overwrite existing LarsWestergren Linux - Newbie 18 04-04-2013 02:07 PM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM
rename files allelopath Linux - General 5 07-05-2005 03:00 AM
rename all files in my directory BabaKali Linux - Newbie 2 11-10-2004 04:27 PM
Can not rename files. Maximus2000 Linux - General 0 04-22-2004 01:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

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