renaming files
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > renaming files




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    renaming files  
junky_fellow@yahoo.co.in


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

I have around 500 files in a directory as *.*.org
I want to rename each of the file by removing .org at the
end.
For eg. I want to rename file 1.test.org ---> 1.test
2.test.org --->  2.test
and so on.

Does anybody know any such command ?

Thanx for any help in advance...






[ Post a follow-up to this message ]



    Re: renaming files  
Ralf Fassel


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

* junky_fellow@yahoo.co.in
| Does anybody know any such command ?

The classic way: for/foreach (depending on your SHELL), basename and mv.

Check the manpage for each of those.
Example usage: http://www.debian-administration.org/articles/150

There also is 'mmv' out there, check e.g.
http://packages.debian.org/stable/utils/mmv
or google for it.

R'






[ Post a follow-up to this message ]



    Re: renaming files  
Igor Pozgaj


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

junky_fellow@yahoo.co.in <junky_fellow@yahoo.co.in> says:
> I have around 500 files in a directory as *.*.org
> I want to rename each of the file by removing .org at the
> end.
> For eg. I want to rename file 1.test.org ---> 1.test
>                               2.test.org --->  2.test
> and so on.

Perl 'rename' utility:

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

If you use zsh you could use:

autoload -U zmv
zmv '(*).(*).org' '$1.$2'

--
Igor Pozgaj | ipozgaj at fly.srk.fer.hr
ICQ: 126002505  | IRC: @thunder (#linux@IdolNet)
PGP: 0xE673B5FD | http://fly.srk.fer.hr/~ipozgaj





[ Post a follow-up to this message ]



    Re: renaming files  
Chuck Dillon


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

junky_fellow@yahoo.co.in wrote:
> I have around 500 files in a directory as *.*.org
> I want to rename each of the file by removing .org at the
> end.
> For eg. I want to rename file 1.test.org ---> 1.test
>                               2.test.org --->  2.test
> and so on.
>
> Does anybody know any such command ?
>
> Thanx for any help in advance...
>

A pipeline methodology I often find useful for these kinds of things...

find . -name "*.*.org" | awk '{ print
"mv",$0,substr($0,1,length($0)-4); }' | sh

or

find . -name \*.org -print | sed 's/^\(.*\).org$/mv \1.org \1/g' | sh

or

find . -name \*.org -exec echo mv "{}" "{}" \; | sed 's/.org$//g' 
| sh

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.





[ Post a follow-up to this message ]



    Re: renaming files  
slowtuna@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-23-05 10:56 PM

Or this PERL script:

#!/usr/bin/perl
# rename script taken from lwall
#

$op = shift;
if ($op eq "") {
$op = "-h";
}
if ($op =~ /^-/) {
if ($op =~ /^-t/) {
$test = 1;
}
if ($op =~ /^-i/) {
$interactive = 1;
}
if ($op =~ /^-h/) {
print "usage: ren [-t -i] pattern files ...\n\n";
print "-t  test, do not rename.\n";
print "-i  interactive, query before renaming.\n\n";
print "Examples:\n\n";
print "ren 's/\.orig\$//' *.orig\n";
print "ren  's/(^)/Beatles - /' *.mp3\n";
print "ren 'y/A-Z/a-z/ unless /^Make/' *\n";
print "ren '$_ .= \".bad\"' *.f\n";
print "ren 'print \"$_: \"; s/foo/bar/ if <stdin> =~ /^y/i'
*\n\n";
print "Don't forget to quote your command!\n\n";
exit;
}
$op = shift;
}
INFILE: for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
if ($test) {
printf("Will rename %s ---> %s\n",$was,$_) unless $was eq $_;
}
else {
if ($was ne $_) {
if ($preview) {
printf("Rename %s ---> %s  [Y/N]?",$was,$_);

$ans = <STDIN>;
if (!($ans =~ /^[Yy]/)) {
next INFILE;
}
}
$res = rename($was,$_);
if ($res) {
printf("Renamed %s ---> %s\n\n",$was,$_);
}
else {
printf("Error renaming %s ---> %s\n\n",$was,$_);
}
}
}
}






[ Post a follow-up to this message ]



    Re: renaming files  
Heiner Steven


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
08-30-05 11:00 PM

junky_fellow@yahoo.co.in wrote:

> I have around 500 files in a directory as *.*.org
> I want to rename each of the file by removing .org at the
> end.
> For eg. I want to rename file 1.test.org ---> 1.test
>                               2.test.org --->  2.test
> and so on.

If you have a POSIX shell (ksh, ksh93, zsh, bash), you
can use

for file in *.*.*.org
do
mv "$file" "${file%.org}"
done

Heiner
--
___ _
/ __| |_ _____ _____ _ _     Heiner STEVEN <heiner.steven@nexgo.de>
\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:09 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register