LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2007, 03:57 PM   #1
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Rep: Reputation: 15
Rename files with script


Hi,

I have about 400 files in the following format
Code:
A.fmtchanged
B.fmtchanged
C.fmtchanged
D.fmtchanged
E.fmtchanged
F.fmtchanged
neuser.fmtchanged
ov.fmtchanged
manage.fmtchanged
...
...
I need to change the file names to the followin format:
Code:
A.fmt
B.fmt
C.fmt
D.fmt
E.fmt
F.fmt
neuser.fmt
ov.fmt
manage.fmt
I need to remove the word "changed" from the filename.

Anyone know how to do this?
Thanks
 
Old 05-31-2007, 05:04 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Yes. The easiest way to do it is with bash (though you'll probably dip a bit into sed along the way). This could be your portal to having fun with shell scripts!

Do this at the command line:

Code:
man bash
Concentrate on the for statement. Write a script that does as much as you can, and play with it a bit. When you have a question, come back to this thread, post your question, and we'll take it to the next step.

Enjoy!
 
Old 05-31-2007, 07:17 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Aye ... and my take on it would indeed be sed.

Code:
for i in `ls *fmtchanged`; do newname=`echo $i|sed -i 's/changed$//'`; mv $i $newname; done

Cheers,
Tink
 
Old 05-31-2007, 07:19 PM   #4
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Alternatively, if you are familiar with vim, you can also do this with its awesome recording feature.
 
Old 05-31-2007, 08:19 PM   #5
sibtay
Member
 
Registered: Aug 2004
Location: U.S
Distribution: Ubuntu
Posts: 145

Rep: Reputation: 15
Quote:
Originally Posted by sibtay
Alternatively, if you are familiar with vim, you can also do this with its awesome recording feature.
I am sorry I misread your initial question. This is not the solution to your problem.

Follow what Tinkster and wjevans_7d1 have explained
 
Old 05-31-2007, 10:31 PM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
in bash
Code:
#!/bin/sh
for files in *fmtchanged
do
   mv $files ${files//changed/} 
done
 
Old 06-01-2007, 02:17 AM   #7
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by sharathkv25
Hi,

I have about 400 files in the following format
Code:
A.fmtchanged
...
manage.fmtchanged
...
...
I need to change the file names to the followin format:
Code:
A.fmt
...
manage.fmt
I need to remove the word "changed" from the filename.


Code:
for file in *.fmtchanged
do
  mv -i "$file" "${file%changed}"
done
 
Old 06-01-2007, 03:01 AM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Ghostdog, cfaj .. I'll have to read "man bash" more closely, I guess :}

Awesome.



Cheers,
Tink
 
Old 06-01-2007, 03:56 AM   #9
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Geez, you guys never heard of the 'rename' command?

Code:
$ rename fmtchanged fmt *.fmtchanged
Done...
 
Old 06-01-2007, 04:17 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bulliver
Geez, you guys never heard of the 'rename' command?

Code:
$ rename fmtchanged fmt *.fmtchanged
Done...
not every *nix has it. anyway, here's a makeshift rename
Code:
#!/bin/sh
from=$1
to=$2
files=$3
for file in "$files"
do
   mv $file ${file/$from/$to}
done
Code:
./rename fmtchanged fmt *fmtchanged
 
Old 06-01-2007, 04:53 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by bulliver
Geez, you guys never heard of the 'rename' command?

Code:
$ rename fmtchanged fmt *.fmtchanged
I certainly have (and have advocated it more than once);
but debi(li)an and its derivatives have a perl-script
by the same name instead that works quite differently.



Cheers,
Tink
Done... ;)
 
Old 06-01-2007, 01:43 PM   #12
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
but debi(li)an and its derivatives have a perl-script
by the same name instead that works quite differently.
Ahh, it becomes clear now...

On a ML I subscribe to a fellow asked for help with rename. He was trying to give it a PCRE and I informed him that rename wants three arguments. He responded that he had read the manpage and was aware of that. Seems he had found some (Debian) tutorial that used the PCRE 'rename' hence his confusion, and the 'real' rename's failure.

Although I have no doubt the Perl version may be very powerful, I don't think it is wise for Debian et al to overwrite apps from the util-linux package. Did they clobber cal and agetty with a Perl version as well?

Anyway, I just thought it was odd that after 6 posts and several shellscripts no one had mentioned rename, so I did...
 
Old 06-01-2007, 02:17 PM   #13
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by bulliver
Although I have no doubt the Perl version may be very powerful, I don't think it is wise for Debian et al to overwrite apps from the util-linux package. Did they clobber cal and agetty with a Perl version as well?

I believe the Perl version preceded the other version, so it is util-linux that overwrote an existing (in some distros) command.

Whichever it was, it is a non-standard command, and is not portable. I avoid it.

 
Old 06-03-2007, 05:59 PM   #14
msantinho
Member
 
Registered: Oct 2005
Location: Lisbon
Distribution: Slackware
Posts: 56

Rep: Reputation: 17
and if you're using Zsh:
Code:
for i in *.fmtchanged; do mv $i ${i:r}.fmt; done
Miguel
 
Old 06-25-2007, 03:00 AM   #15
secretlydead
Member
 
Registered: Sep 2003
Location: Qingdao, China
Distribution: mandriva, slack, red flag
Posts: 249

Rep: Reputation: 31
I need to change some filenames.

They all need to be called piers.*csv because they need to go into a MySQL database into a table called piers with the mysqlimport command.

So, the first file should be called piers.1csv, the second piers.2csv, etc.

...

i'd still be interested in how to do this in perl or something, but i just found this solution:

(the first thing was to look for a program in the repository with the filename 'rename')

use gwenrename to rename them all piers001.csv

then use the rename utility to rename them from s0 -> s.0 and then .c -> c

now they can be imported to MySQL the way I need.

Last edited by secretlydead; 06-25-2007 at 03:14 AM.
 
  


Reply



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
Script to rename files nazs Programming 15 03-31-2007 02:12 PM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 01:45 PM
script to rename muliple files cranium2004 Programming 3 02-21-2006 10:59 PM
rename script all files in dir Longinus Linux - Newbie 8 08-02-2004 12:24 AM
Need script to rename files joe_stevensen Programming 5 12-05-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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