LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 06-02-2007, 02:35 PM   #1
almost000
Member
 
Registered: Apr 2007
Distribution: Fedora 7,Windows 3.11-9X/ME,Windows 2000/XP/Vista
Posts: 107

Rep: Reputation: 15
Arrow batch renameing. files without a ext.


Well I have a love of files that I want then to have the .jpg ext in windows I would just

Code:
move .\* .\*.jpg

that would fix that. I thought I could use the mv command but that just put everything in a new folder called .jpg. I see that the mv command is just for renameing folders


2.6.21-1.3194.fc7
 
Old 06-02-2007, 02:51 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
mv can be used for individual files OR directories. To contradict Mr Shakespeare the fault likes in your star(s).

If you mv file file.jpg it would work just fine.

However you told it to move * which means EVERYTHING to jpg so it assumed you meant you wanted to move all the files to a directory. If it HAD worked the way you told it then you'd end up with only a single file named jpg which would be the LAST file it found which would be even worse.

To rename all the files you could do:

for file in `ls [a-z]*`
do mv $file ${file}.jpg
done

The first line says to get a list of files that start with any character a-z (this helps to exclude files that start with . such as .bashrc that you do NOT want to rename as jpg files.). Note the characters here are backticks (same key as ~) rather than single quotes.

You put the braces around file in the do line so that it knows the variable is only $file and not $file.jpg which you haven't defined.

Last edited by MensaWater; 06-02-2007 at 04:26 PM.
 
Old 06-02-2007, 02:55 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
EDIT: Never mind - the equivalent example was already posted by jlightner...

Last edited by gilead; 06-02-2007 at 02:56 PM.
 
Old 06-02-2007, 03:48 PM   #4
almost000
Member
 
Registered: Apr 2007
Distribution: Fedora 7,Windows 3.11-9X/ME,Windows 2000/XP/Vista
Posts: 107

Original Poster
Rep: Reputation: 15
Question Well did I do some thing wrong, it dod not rename thises files.

Code:
[al@al cody2]$ for file in `ls [a-z]*`
> do mv $file ${file}.jpg
> done
ls: cannot access [a-z]*: No such file or directory
[al@al cody2]$ ls
00080447d01  193D1572d01  34C705E1d01  5408B326d01  65C8052Dd01  760A240Ad01

it did reame 90 files. now what do I do and I move them to a new folder.
but 600 files is a lot of files to name.

Last edited by almost000; 06-02-2007 at 05:32 PM.
 
Old 06-02-2007, 05:31 PM   #5
almost000
Member
 
Registered: Apr 2007
Distribution: Fedora 7,Windows 3.11-9X/ME,Windows 2000/XP/Vista
Posts: 107

Original Poster
Rep: Reputation: 15
Cool I see

I see that there are no files with that start with files. thank you for helping me I just change the [a-z] to [0-9]
 
Old 06-02-2007, 05:59 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Right - you could also just use the pattern [a-zA-Z0-9]* - that would find all files that started with a standard alphanumeric character.
 
Old 06-03-2007, 02:58 AM   #7
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 133Reputation: 133
If you're lazy, you could just use Krename - it does all of that and more. If someone spent their time writing this sort of app, why not use it?
 
Old 06-03-2007, 11:54 AM   #8
dawkcid
Member
 
Registered: May 2007
Location: UK
Distribution: LFS,Slackware,Slamd64,NetBSD
Posts: 102

Rep: Reputation: 15
One could also use something like:

Code:
perl -we 'for (@ARGV){rename $_, $_ . ".jpg";}' *
(I'm assuming, of course, that there are no non-jpg files in ./. If there are, then just specify the relevant prefixes, foo* 123*, etc.)

For a large number of files, this will be much faster than spawning an instance of mv for each file.

Quote:
If you're lazy, you could just use Krename - it does all of that and more. If someone spent their time writing this sort of app, why not use it?
Hmm, let me see, can you say "bloatware"?

Besides, with that logic, you could say the same thing about Windows!
 
Old 06-05-2007, 04:05 AM   #9
almost000
Member
 
Registered: Apr 2007
Distribution: Fedora 7,Windows 3.11-9X/ME,Windows 2000/XP/Vista
Posts: 107

Original Poster
Rep: Reputation: 15
Cool perl -we 'for (@ARGV){rename $_, $_ . ".jpg";}' *

I thought perl was hard. my be I should get a book on it. thank you for showing me someing new and simple. that is cool.
 
Old 06-05-2007, 04:37 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You don't need the `ls ...` in the for loop. You can use filename globbing to supply the names:

for file in [0-9A-F]*; do
rm "$file" "${file}.jpg"
done
 
  


Reply

Tags
files, renaming



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
renameing directories?? acidblue Slackware 2 02-02-2006 06:42 PM
Batch files in linux? fuelinjection Linux - General 10 05-19-2005 07:49 AM
Linux batch files denizen Linux - Newbie 3 07-28-2004 03:56 AM
Batch files spotslayer Linux - Software 3 06-18-2004 07:31 PM
Linux Batch Files sfnitro230 Slackware 13 03-09-2004 02:51 PM

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

All times are GMT -5. The time now is 06:52 AM.

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