LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-20-2004, 02:31 PM   #1
Mr. Gone
LQ Newbie
 
Registered: Mar 2004
Posts: 29

Rep: Reputation: 15
Renaming files doesn't work from console


When I try to rename files from a console, the command 'rename' does not work.

It happens with proper write permissions (I tried both from a normal user account and from a root console). The command exits with 0 and no errors show up; however, the name of the files remains unchanged.

On the contrary, 'mv' works, but this command does not work with several files (in the same directory; that is, using it for renaming instead of moving).

Also, I can rename files from the different file managers I use, but I have to do it one by one and I would like the convenience of being able to rename several files at the same time from a console.

Any hint would be greatly appreciated.

Thanks in advance.

Last edited by Mr. Gone; 03-20-2004 at 02:32 PM.
 
Old 03-20-2004, 02:55 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Sorry ... just crawled out of bed :}


Cheers,
Tink

Last edited by Tinkster; 03-20-2004 at 03:03 PM.
 
Old 03-20-2004, 03:03 PM   #3
lfur
Member
 
Registered: Jul 2003
Location: Slovenia
Distribution: Slackware & FreeBSD
Posts: 209

Rep: Reputation: 30
When using mv for renaming, you do:

mv oldname newname

and it doesn't work??
 
Old 03-20-2004, 03:50 PM   #4
Mr. Gone
LQ Newbie
 
Registered: Mar 2004
Posts: 29

Original Poster
Rep: Reputation: 15
Yes, 'mv' works, but I cannot use it as a substitute of 'rename' to change the name of several files at the same time. Quoting from 'info mv': "It is an error if the last argument is not a directory and more than two files are given."

Of course, I could use 'mv' to rename/move them to another directory and then copy them back to the original directory, deleting the files with the old name, but this adds extra work to the process, and I'd really want to know why 'rename' does not behave as it should.

Many thanks for your help.
 
Old 03-20-2004, 03:58 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Give me an example of what kind of renaming
you're trying to achieve ... there's surely a simple
scripting solution :}


Cheers,
Tink
 
Old 03-20-2004, 04:18 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671Reputation: 671
If you want to rename a group of files, you will need to use the find command, or a for loop to select the files.

For example, to rename all *.JPG files to *.jpg try:
for pic in *.JPG; do mv $pic ${pic%.JPG}.jpg; done

If any of the files have spaces in their names, you will need double quotes around the variables.
for pic in *.JPG; do
> mv "$pic" "${pic%.JPG}.jpg"
> done
 
Old 03-20-2004, 04:26 PM   #7
Mr. Gone
LQ Newbie
 
Registered: Mar 2004
Posts: 29

Original Poster
Rep: Reputation: 15
For example, if I try your formula:

rename oldname newname

It does not work. Right now I did:

$ touch example_file.txt
$ rename example_file.txt new_file.txt

And the file kept being 'example_file.txt'. It doesn't matter if I try to rename one or more files. It doesn't work in any case.

Thanks again.
 
Old 03-20-2004, 04:34 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Have a look at this one :)

http://freshmeat.net/projects/ren/

I just tried it, using e.g.

ren "*.htm" "#1.html"

to change 4 files extensions ...

you could also do a
ren "old*.txt " "new#1.txt"

Cheers,
Tink
 
Old 03-20-2004, 04:47 PM   #9
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 30
The rename command in linux doesn't work as in DOS. The man page says the usage is rename <from> <to> <file>, where <from> is the string you want to rename, <to> is the string you want to rename it to, and <file> are the file(s) you want to rename. From the man page:
Code:
DESCRIPTION
       rename will rename the specified files by replacing the first occurrence of <from> in
       their name by <to>.

       For example, given the files foo1, ..., foo9, foo10, ..., foo278, the commands

              rename foo foo0 foo?
              rename foo foo0 foo??

       will turn them into foo001, ..., foo009, foo010, ..., foo278.

       And
              rename .htm .html *.htm

       will fix the extension of your html files.
 
Old 03-20-2004, 08:59 PM   #10
Mr. Gone
LQ Newbie
 
Registered: Mar 2004
Posts: 29

Original Poster
Rep: Reputation: 15
Bebo, you're damn right. I had read the man page of 'rename' before, but I missed the whole point. Now I understand, and of course if you do it right it works.

Tinkster, thanks for suggesting 'ren'. It seems interesting. What I wanted to do was related with names more than extensions, as until now I used 'krename' to change the latter. But now I can change names and extensions from the command line, which I prefer.

Many thanks to all you guys for your help.

Last edited by Mr. Gone; 03-20-2004 at 09:01 PM.
 
  


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
Renaming multple files.... maginotjr Slackware 4 06-04-2006 11:09 AM
bash help renaming files kahn Programming 6 06-16-2005 08:15 AM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 10:41 PM
Renaming files as they are uploaded Cr4wford Linux - Software 1 04-26-2004 04:41 PM
Renaming files in one go saurya_s Linux - Software 1 01-12-2004 02:16 PM


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