Unix Programming - renaming files

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > August 2005 > renaming files





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author renaming files
junky_fellow@yahoo.co.in

2005-08-23, 5: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...

Ralf Fassel

2005-08-23, 5: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'

Igor Pozgaj

2005-08-23, 5: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
Chuck Dillon

2005-08-23, 5: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.
slowtuna@gmail.com

2005-08-23, 5: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,$_);
}
}
}
}

Heiner Steven

2005-08-30, 6: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/
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com