LinuxQuestions.org
Register a domain and help support LQ
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, 03: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, 03:51 PM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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 05:26 PM.
 
Old 06-02-2007, 03:55 PM   #3
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,125

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

Last edited by gilead; 06-02-2007 at 03:56 PM.
 
Old 06-02-2007, 04: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 06:32 PM.
 
Old 06-02-2007, 06: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, 06:59 PM   #6
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
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, 03:58 AM   #7
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
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, 12:54 PM   #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, 05: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, 05:37 AM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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


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


All times are GMT -5. The time now is 06:42 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration