[comp.unix.wizards] simple question about mv

DEG41560@ua1vm.ua.edu (Rob Smith) (01/27/89)

HI,

 I know this is simple, but then again, so am I.  What if I want to mv a bunch
of files with the same suffix to another suffix. The following does not
work

    mv *.flip *.flop

what does?  I'm under Ultrix 2.2.  I'm doing it by hand, but I'd like to
be able to do wild card renames, ala DOS.


                                         Thanks,
                                            Rob.

dcmartin@cs.wisc.edu (David C. Martin) (01/28/89)

foreach i ( *.flip )
mv $i $i:r.flop
end

dcm

moss@BRL.MIL (VLD/VMB) (01/28/89)

< I know this is simple, but then again, so am I.  What if I want to mv a bunch
< of files with the same suffix to another suffix. The following does not
< work
< 
<    mv *.flip *.flop
Well, this should work under a Bourne shell:
$ for i in *.flip
> do	mv $i `basename $i .flip` .flop
> done
$
-moss

rob@pbhyf.PacBell.COM (Rob Bernardo) (01/28/89)

In article <18217@adm.BRL.MIL> DEG41560@ua1vm.ua.edu (Rob Smith) writes:
+ I know this is simple, but then again, so am I.  What if I want to mv a bunch
+of files with the same suffix to another suffix. The following does not
+work
+
+    mv *.flip *.flop
+
+what does?  I'm under Ultrix 2.2.  I'm doing it by hand, but I'd like to
+be able to do wild card renames, ala DOS.

If you have ksh do this:

for file in *.flip
do
	mv $file ${file%%flip}flop
done

otherwise you could resort to using sed to generate the new file name,
something like this (but pardon any typos):

for file in *.flip
do
	mv $file `echo $file | sed 's/flip$/flop/'`
done
-- 
Rob Bernardo, Pacific Bell UNIX/C Reusable Code Library
Email:     ...![backbone]!pacbell!pbhyf!rob   OR  rob@pbhyf.PacBell.COM
Office:    (415) 823-2417  Room 4E750A, San Ramon Valley Administrative Center
Residence: (415) 827-4301  R Bar JB, Concord, California

dave@lsuc.uucp (David Sherman) (01/29/89)

In article <18230@adm.BRL.MIL> moss@BRL.MIL (VLD/VMB) writes:
|< I know this is simple, but then again, so am I.  What if I want to mv a bunch
|< of files with the same suffix to another suffix. The following does not
|< work
|< 
|<    mv *.flip *.flop
|Well, this should work under a Bourne shell:
|$ for i in *.flip
|> do	mv $i `basename $i .flip` .flop
|> done
|$

Moss's suggestion is the best, once you know what you're
doing.  For people who are a little scared of such magic
before they're fully conversant with sh, it's more comforting
to edit a script which has the mv's explicitly.  Just use good old ed:

$ ls *.flip > junk
$ ed junk
g/.flip/s///
g/.*/s//mv &.flip &.flop/p
w
q
$ sh -x junk

(Personally, I used qed and would say
	$ qed
	<ls *.flip
	,s/\(.*\).flip$/mv & \1.flop
	>sh -x
	Q
but not that many people have qed.)

The point is that you can see what the mv commands look like,
in the editor, before you shovel them at the shell.  It's reassuring.
Note also the sh -x, which lets you watch the commands as they run.

David Sherman
Toronto
-- 
Moderator, mail.yiddish
{ uunet!attcan  att  pyramid!utai  utzoo } !lsuc!dave

lew@gsg.UUCP (Paul Lew) (01/29/89)

In article <18217@adm.BRL.MIL> DEG41560@ua1vm.ua.edu (Rob Smith) writes:
>
>						   What if I want to mv a bunch
>of files with the same suffix to another suffix. The following does not
>work
>
>    mv *.flip *.flop
>
>what does?  I'm under Ultrix 2.2.  I'm doing it by hand, but I'd like to
>be able to do wild card renames, ala DOS.

I assume you are using Csh, add the following in your .cshrc:

	alias	ren	'set noglob; rename \!*; unset noglob'

You can then issue command like:

	$ ren *.flip *.flop

Rename is a shell script for the exercise of the readers.  I can mail it
to those who dont want to reinvent the wheel.  If enough interest, I will
post it to comp.sources.misc.
-- 
Paul Lew			{oliveb,harvard,decvax}!gsg!lew	(UUCP)
General Systems Group, 5 Manor Parkway, Salem, NH 03079	(603) 893-1000

logan@vsedev.VSE.COM (James Logan III) (01/30/89)

In article <18217@adm.BRL.MIL> DEG41560@ua1vm.ua.edu (Rob Smith) writes:
#  I know this is simple, but then again, so am I.  What if I want to mv a bunch
# of files with the same suffix to another suffix. The following does not
# work
# 
#     mv *.flip *.flop
# 
# what does?  I'm under Ultrix 2.2.  I'm doing it by hand, but I'd like to
# be able to do wild card renames, ala DOS.

You will have to do this with a "for" loop.  If you are using the
Bourne or the Korn shell, you can use the following loop.  If you're
using the C shell, you're on your own.

for FILE in *.flip; do
	NOEXTEN=`basename $FILE .flip`;
	echo $FILE ${NOEXTEN}.flop;
	mv $FILE ${NOEXTEN}.flop;
done;

I like having the echo in the above example so that if I make a
typo I can see the problem, before it's too late, and hit the
interrupt key.  

That loop can be generalized into a shell script like this one:
-------------------------------------------------------
:
#	mvex.sh		James Logan	Sun Jan 29 15:44:09 EST 1989
#		Move all files with one extension to a new extension.
#

#
# Check the parameters.
#
if test $# -ne 2; then
	echo >&2 "usage: $0 oldextension newextension";
	exit 2;
fi;

#
# These variables can be optimized out, but are used for clarity.
#
OLDEXTENSION=$1;
NEWEXTENSION=$2;

#
# Move each file one at a time.
#
for FILE in *$OLDEXTENSION; do
	NOEXTEN=`basename $FILE $OLDEXTENSION`;
	mv $FILE ${NOEXTEN}${NEWEXTENSION};
done;

exit 0;
-------------------------------------------------------

			-Jim

-- 
Jim Logan                           logan@vsedev.vse.com
VSE Software Development Lab        uucp:  ..!uunet!vsedev!logan
(703) 418-0002                      inet:  logan%vsedev.vse.com@uunet.uu.net

eirik@lurch.stanford.edu (Eirik Fuller) (01/30/89)

In article <1989Jan28.203519.3521@lsuc.uucp>, dave@lsuc (David Sherman) writes:
)  ...
) $ ls *.flip > junk
) $ ed junk
) g/.flip/s///
) g/.*/s//mv &.flip &.flop/p
) w
) q
) $ sh -x junk
) 
)  ...
) 
) The point is that you can see what the mv commands look like,
) in the editor, before you shovel them at the shell.  It's reassuring.
) Note also the sh -x, which lets you watch the commands as they run.
) ...

I agree with the general strategy of constructing commands on the fly
and, when they look right, feeding them to a shell.  However, I don't
see the need for a junk file; what's wrong with pipes?

I would have done this as

ls *.flip | sed -e 's/\(.*\)flip$/mv \1flip \1flop/'

When I'm happy with the output (after the usual half-dozen
iterations), I say (in csh)

!! | sh -x

richard@torch.UUCP (Richard Nuttall) (02/06/89)

DEG41560@ua1vm.ua.edu (Rob Smith) writes:

>HI,

> I know this is simple, but then again, so am I.  What if I want to mv a bunch
>of files with the same suffix to another suffix. The following does not
>work

>    mv *.flip *.flop

I prefer the C shell foreach, which is really neat :

foreach i (*.flip)
echo $i
mv $i $i:r.flop
end


I use these kind of commands all the time, and usually include the echo line
(not actually necessary) so I can see what's happening.


Richard.

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (02/16/89)

In article <228@torch.UUCP> richard@torch.UUCP (Richard Nuttall) writes:
: DEG41560@ua1vm.ua.edu (Rob Smith) writes:
: 
: >HI,
: 
: > I know this is simple, but then again, so am I.  What if I want to mv a bunch
: >of files with the same suffix to another suffix. The following does not
: >work
: 
: >    mv *.flip *.flop
: 
: I prefer the C shell foreach, which is really neat :
: 
: foreach i (*.flip)
: echo $i
: mv $i $i:r.flop
: end

Yes, you can use csh for that, but it's an awful lot of typing.  If you
have perl, you can put this script into your system as "rename":

#!/usr/bin/perl

$subst = shift;
@ARGV = <*> if $#ARGV < 0;

foreach $name (@ARGV) {
    $_ = $name;
    eval "$subst;";
    die $@ if $@;
    rename($name,$_) unless $name eq $_;
}


You can now say things like:

	rename 's/\.flip$/.flop/'	# rename *.flip to *.flop
	rename s/flip/flop/		# rename *flip* to *flop*
	rename 's/^s\.(.*)/$1.X/'	# switch sccs filenames around
	rename 's/$/.orig/ */*.[ch]'	# add .orig to your source files in */
	rename 'y/A-Z/a-z/'		# lowercase all filenames in .
	rename 'y/A-Z/a-z/ if -B'	# same, but just binaries!

    or even

	rename chop *~			# restore all ~ backup files

The script could of course be modified to be verbose, or to confirm, or
to get filenames from find, or to not wipe out existing files.  For that
matter, you could probably modify it into a C compiler if you tried hard
enough.  :-)

Larry Wall
lwall@jpl-devvax.jpl.nasa.gov