LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-01-2009, 06:53 PM   #1
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Rep: Reputation: 15
renaming files in Linux


Hello, I'm pretty new to the Linux world. I'm having a hard time understanding how to go about changing file names. I have searched through this forum board for possible solutions to what I am trying to do but the ones that were on the board did not seem to make sense to me, or work.

I am using the Terminal Linux - bash shell, I hope that makes sense?

I am trying to figure out a way to change files in multiple different ways. The only one that I can see that fits is the following command:

mv file1.txt file.txt

Which renames file1.txt to file.txt.

Now I tried to use the rename command, but I did not understand that one at all. I even tried doing the solutions that were found on the board, but I couldn't figure it out (understand it).

Am I missing something?
 
Old 03-01-2009, 07:00 PM   #2
TBC Cosmo
Member
 
Registered: Feb 2004
Location: NY
Distribution: Fedora 10, CentOS 5.4, Debian 5 Sparc64
Posts: 355

Rep: Reputation: 43
I guess it would help to know what your goal is to see why mv file1.txt file.txt does not fit your needs.
 
Old 03-01-2009, 07:06 PM   #3
jay73
LQ Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
Well, if you take a look at the man page for RENAME, you will see that you need to have some familiarity with the perl scripting language.

Quote:
RENAME(1) Perl Programmers Reference Guide RENAME(1)

NAME
rename - renames multiple files

SYNOPSIS
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]

DESCRIPTION
"rename" renames the filenames supplied according to the rule specified
as the first argument. The perlexpr argument is a Perl expression
which is expected to modify the $_ string in Perl for at least some of
the filenames specified. If a given filename is not modified by the
expression, it will not be renamed. If no filenames are given on the
command line, filenames will be read via standard input.

For example, to rename all files matching "*.bak" to strip the
extension, you might say

rename 's/\.bak$//' *.bak

To translate uppercase names to lower, youíd use

rename 'y/A-Z/a-z/' *

OPTIONS
-v, --verbose
Verbose: print names of files successfully renamed.

-n, --no-act
No Action: show what files would have been renamed.

-f, --force
Force: overwrite existing files.

ENVIRONMENT
No environment variables are used.

AUTHOR
Larry Wall

SEE ALSO
mv(1), perl(1)

DIAGNOSTICS
If you give an invalid Perl expression youíll get a syntax error.

BUGS
The original "rename" did not check for the existence of target
filenames, so had to be used with care. I hope Iíve fixed that (Robin
Barker).

perl v5.10.0 2008-12-23 RENAME(1)
 
Old 03-01-2009, 07:09 PM   #4
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
When I bring up the man rename page this is all I see:

Code:
RENAME(1)          Linux Programmer‚s Manual          RENAME(1)

NAME
       rename - Rename files

SYNOPSIS
       rename from to file...

DESCRIPTION
       rename  will rename the specified files by replacing the
       first occurrence of from in their name by to.
RENAME(1)          Linux Programmer‚s Manual          RENAME(1)

NAME
       rename - Rename files

SYNOPSIS
       rename from to file...

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.

SEE ALSO
       mv(1)
So how is it different between the two of us? I copied and pasted this from Terminal window.
 
Old 03-01-2009, 07:16 PM   #5
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,332

Rep: Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407
It would be helpful if you could give more information on exactly what you are trying to accomplish. What type of files you are trying to change and what about them you want to change.
 
Old 03-01-2009, 07:32 PM   #6
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
I just want to rename the file names and figured there was an easier way to doing so than the follow command I have, which does work:

text1.txt, text2.txt, text3.txt to chap1.txt, chap2.txt, chap3.txt

I know that the following command works:

mv text1.txt chap1.txt; mv text2.txt chap2.txt; mv text3.txt chap3.txt

Isn't there an easier way to change those three text*.txt to chap*.txt?

Last edited by shorte85; 03-01-2009 at 07:44 PM.
 
Old 03-01-2009, 07:55 PM   #7
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
Code:
rename text chap text*.txt
should work just fine ...
 
Old 03-01-2009, 08:02 PM   #8
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 7,332

Rep: Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407Reputation: 1407
Yes.

rename text chap text*

In a terminal, cd to the directory these files are in for it to work. This will change any file in the directory beginning with "text" to "chap" so if you have files you don't want to change you will need to move them. I expect that "text" is not the actual name so...

Last edited by yancek; 03-01-2009 at 08:04 PM.
 
Old 03-01-2009, 08:04 PM   #9
shorte85
Member
 
Registered: Mar 2009
Posts: 47

Original Poster
Rep: Reputation: 15
Thank you so much! I was trying to do it that way but kept getting it wrong, that rename command is kind of confusing if you ask me. Thank you so much though! I appreciate it!!
 
Old 03-01-2009, 08:39 PM   #10
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
You're most welcome ... the command is not that hard if you manage to
understand the man-page ... it's just as it says in the brief summary:
Quote:
rename from to file...
In anything file (or other names following [which the ellipsis represents]
replace the first occurrence of the string from with the string to.

In your example all files have text which you want to replace with chap,
and you want to do that to all files that have a beginning of text, followed
by a number and the extension .txt.



Cheers,
Tink
 
Old 03-02-2009, 12:20 AM   #11
replica9000
Senior Member
 
Registered: Jul 2006
Location: USA
Distribution: Debian, FreeBSD, Android
Posts: 1,053
Blog Entries: 2

Rep: Reputation: 227Reputation: 227Reputation: 227
You could try something like KRename rather than using a terminal.
 
Old 05-26-2010, 11:07 AM   #12
jessmo02
LQ Newbie
 
Registered: May 2010
Posts: 1

Rep: Reputation: 0
Rename command

Let us say there are 3 files: text1.txt, text2.txt and text3.txt. Give two sets of commands to rename these files to chap1.txt, chap2.txt and chap3.txt, rename command.
Here is the rename command:

Rename text chap *
 
Old 05-26-2010, 11:25 AM   #13
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 3,304
Blog Entries: 1

Rep: Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686
Code:
for INPUT in chap*.txt
do 
  OUTPUT=`echo $INPUT | sed 's/chap/new/'`
  mv $INPUT $OUTPUT
done
or as a one liner

Code:
for INPUT in chap*.txt; do OUTPUT=`echo $INPUT | sed 's/chap/new/'`; mv $INPUT $OUTPUT; done
This route you can change mv to echo to see if what it's going to attempt is what you intended to be attempted. And probably a dozen other ways. If you have funky filenames you might need to do fancy stuff with mv. mv -- "old_odd_name" "new_odd_name". Or would it be single quotes? Or I could be wrong.
 
  


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
help renaming files please balistic Linux - Newbie 2 07-29-2007 04:35 PM
renaming files starwarsfan982 Linux - Software 7 10-30-2006 03:06 PM
Linux 101 - Renaming Multiple Files ????? perry Linux - Newbie 3 04-08-2006 10:09 PM
Renaming Windows Files from Linux Lonely_warrior Linux - General 3 07-13-2004 10:58 PM

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

All times are GMT -5. The time now is 02:37 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