Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with "mv" wildcard command

1 view
Skip to first unread message

Kurt M. Hockenbury

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to
James Piwowarczyk (j...@earth.execpc.com) wrote:
: Without writing a script, is there a unix command that I can use to
: change a number of "files.htm" to" files.html"? I have tried "mv *.htm
: *.html" and got an error message about the ending arguement needing to be
: a directory. Thanks!!!

Well, it's a command line, not a script, but... :-) [why are people afraid
of writing shell scripts?]

Under sh/ksh/bash:

for i in *.htm ; do mv $i "$i"l ; done

Under csh/tcsh:
foreach i (*.htm)
mv $i "$i"l
end

Don't forget to update the href's in your files to reflect the new names.
-Kurt

snail://USA/07030/NJ/Hoboken/PO Box 5136/Kurt M. Hockenbury
http://linux.stevens-tech.edu/kmh/kmh-fun.html

John Wingate

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to
James Piwowarczyk (j...@earth.execpc.com) wrote:
: Without writing a script, is there a unix command that I can use to
: change a number of "files.htm" to" files.html"? I have tried "mv *.htm
: *.html" and got an error message about the ending arguement needing to be
: a directory. Thanks!!!

You can use a loop entered by hand at the shell prompt. A ksh example
(which works for bash too) has been posted. A csh version would have
different syntax.

The reason for your error message is that the shell handles the
wild-card expansion; mv, cp, and other commands do not use wild cards
at all. If you have file1.htm, file2.htm, file3.htm and newfile.html
in your directory, the command executed for "mv *.htm *.html" will be
"mv file1.htm file2.htm file3.htm newfile.html", probably not what you
want.

Having the shell do the expansion automatically gives a consistent
semantic interpretation to asterisks wherever they occur. With
"mv *.htm *.html" as you tried it, the "*.htm" refers to existing files
and the "*.html" to (presumably) nonexisting files with names generated
from the others. This is possible with the corresponding DOS and VMS
commands, which don't take an arbitrary list of arguments. Also
handling more than one wild-card under a DOS-like interpretation is
problematic: supposing you had a file named "abracadabra", how would
you want "mv a*b*a a*x*a" to behave?

I prefer the consistency of the unix shell approach, even if it does
make some things a little harder to do.

________________________________________________________________________
John Wingate win...@clark.net jwi...@nswc.navy.mil

James Piwowarczyk

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to

Scott Frohman

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to j...@earth.execpc.com

James-

'Fraid not. When the shell sees "*.html" in your command, it looks in the
current directory and tries to fill in that wildcard spec with actual files.
Since they don't exist (yet), it will not succeed. But the script isn't too
dangerous! See (ksh):

$ for FILE in *htm
> do
> mv FILE ${FILE%htm}html
> done(ksh):

the ${FILE%htm} stuff says, "take the rightmost part of the variable $FILE,
chopping off `htm'."

You could write a script to do this using something else in place for "htm" &
"html"...

Regards,
Scott Frohman

--
___ ____
/___ /__ sfro...@csci.csc.com GE d++ s++: C+ UH+++
\_____/cott / rohman Computer Sciences Corporation P+ W+ N++ w--- M++
(301) 897-1178 V-- PS+ t+ tv e+++

The opinions expressed here do not necessarily make sense.


Jim Davis

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to
On 18 Aug 1995, James Piwowarczyk wrote:

> Without writing a script, is there a unix command that I can use to
> change a number of "files.htm" to" files.html"? I have tried "mv *.htm
> *.html" and got an error message about the ending arguement needing to be
> a directory. Thanks!!!

ls *.htm |xargs -i -t mv {} {}l

Jim

Scott Frohman

unread,
Aug 18, 1995, 3:00:00 AM8/18/95
to j...@earth.execpc.com
Oops.. delete the stuff above the "^^^" below! And I accidentally deleted a
'$' before the FILE on the 3rd line. It's corrected below. Sorry.

-S

$ for FILE in *htm
> do

> mv $FILE ${FILE%htm}html
> done(ksh):
^^^^^^

Gokhan Gezmisoglu

unread,
Aug 20, 1995, 3:00:00 AM8/20/95
to
find . -name "*.htm" -exec mv {} {}l \;
--
gokhan


Max Heffler

unread,
Aug 20, 1995, 3:00:00 AM8/20/95
to
In <412kan$k...@daily-planet.execpc.com> j...@earth.execpc.com (James Piwowarczyk) writes:

>Without writing a script, is there a unix command that I can use to
>change a number of "files.htm" to" files.html"? I have tried "mv *.htm
>*.html" and got an error message about the ending arguement needing to be
>a directory. Thanks!!!

Bourne shell:

for i in *.htm
do
mv $i $i{l}
done

C shell:

foreach i (*.htm)
mv $i ${i}l
done
--
Max Heffler, Senior Software Design Engineer home: m...@texsys.com
Home page: http://www.texmicro.com/~max - (an exercise in egomania)
___________________________________________________
/ // /// // /// // /// // /// // /// // /// // ///
---------------------------------------------------

Andrew Fitzgibbon

unread,
Aug 22, 1995, 3:00:00 AM8/22/95
to
--text follows this line--

In article <412kan$k...@daily-planet.execpc.com> j...@earth.execpc.com (James Piwowarczyk) writes:

> Without writing a script, is there a unix command that I can use to
> change a number of "files.htm" to" files.html"? I have tried "mv *.htm
> *.html" and got an error message about the ending arguement needing to be
> a directory. Thanks!!!

Here's a perl script which does this... not well tested but it works in your case.
For safety, it only prints out the moves, you should pipe them through sh
to execute them. Obviously this can be changed.

For ultimate convenience, you need to persuade your shell not to glob using
an alias. In tcsh, that's

alias mva '(set noglob; exec $HOME/bin/share/mva \!*)'

For bash, my best guess is

alias mva='set -o noglob ; exec_and_reset_glob $HOME/bin/share/mva '
exec_and_reset_glob () { $* ; set +o noglob }

A.

-----------------------------------------------------------------------------
#!/usr/local/bin/perl4 -w
# Copyright July 1995
# Andrew Fitzgibbon (andr...@ed.ac.uk)
sub usage {
print '
Usage: mva "<template>" "<template>"
<template>s may contain the following globules:
* converted to regexp (.*)
{a,b,c} converted to regexp (a|b|c)

Beware of ~expansions.

';
exit;
};

open (STDERR, "/dev/null");
print STDERR "MVA: ";

# Check that two args have been specified
&usage if ($#ARGV != 1);

# Divide source and dest into an array of basic blocks.
# The block types are Star, Alternative and Literal.

$src = $ARGV[0];
$dest = $ARGV[1];
$glob1="\\*";
$glob2="{[^}]+}";
$glob="($glob1)|($glob2)";

@src_array = split(/$glob/, $src);
@dest_array = split(/$glob/, $dest);

# Convert the shell glob into a perl regexp, sources as expected,
# destinations to "\$matchindex++"

$re_src = "^";
$re_dest = "";
$matchindex = 0;
for ($aryindex = 0; $aryindex <= $#src_array; $aryindex++ ) {
$cmp = $src_array[$aryindex];
if ($cmp) {
print STDERR "Parsing [$cmp]: ";
if ($cmp eq "*") {
print STDERR "Star\n";
$re_src = $re_src . "(.*)";
$matchindex++;
$re_dest = $re_dest . "\\$matchindex";
} elsif ($cmp =~ $glob2) {
print STDERR "Alternative\n";
$cmp =~ s/[{}]//g; #
$cmp =~ s/,/|/g;
$re_src = $re_src . "($cmp)";
$matchindex++;
$re_dest = $re_dest . "\\$matchindex";
} else {
print STDERR "Literal\n";
$cmp =~ s/(\W)/\\$1/g;
$re_src = $re_src . $cmp;
$re_dest = $re_dest . $dest_array[$aryindex];
}
}
}
$re_src = $re_src . "\$";

print STDERR "$src -> $re_src\n";
print STDERR "Dest = $re_dest\n";

# use csh to glob original pattern as sh doesn't do {} construct
open(FILELIST, "/bin/csh -fc \"echo $src\" | tr -s ' \t\r\f' '\\012\\012\\012\\012'|");
while ($_ = <FILELIST>) {
chop;
print "mv $_\t";
eval "s!$re_src!$re_dest!";
print STDERR "1: $1 2: $2\n";
print "$_\n";
}
-----------------------------------------------------------------------------
--
Andrew Fitzgibbon (Research Associate), andr...@ed.ac.uk
Artificial Intelligence, Edinburgh University. +44 031 650 4504
<a href=http://www.dai.ed.ac.uk/staff/personal_pages/andrewfg> Home Page </a>
"Never say there is no way" -- me.

Roman Fietze

unread,
Aug 23, 1995, 3:00:00 AM8/23/95
to
In article <412kan$k...@daily-planet.execpc.com> j...@earth.execpc.com (James Piwowarczyk) writes:

> Without writing a script, is there a unix command that I can use to
> change a number of "files.htm" to" files.html"? I have tried "mv *.htm
> *.html" and got an error message about the ending arguement needing to be
> a directory. Thanks!!!

for file in *.htm; do mv $file ${file}l; done

... and e.g to change *.abc to *.xyz you could do

for file in *.abc; do mv $file ${file%.abc}.xyz; done

This is for sh (???) and ksh.

Roman
--
Roman Fietze (Mail Code 5023) Kodak AG Stuttgart/Germany
fie...@kagcpd01.ag01.kodak.COM fie...@kodak.COM

0 new messages