rename v mv function conflict

Geert Janssens yellowdog-general@lists.terrasoftsolutions.com
Wed, 15 Sep 2004 11:35:08 +0200


Derick Centeno wrote:
> Hi Gang:
> Has anyone else noticed a logical conflict between the rename and mv
> functions?  Correcting this problem may be way beyond the scope of this
> list but I was wondering if any of the luminaries here came up with a
> solution to correct both of them.
> 
> For instance, although man and info regarding rename report the same
> instructions.  When it is applied rename behaves as though it has
> succeeded in its execution, when in fact it has failed!
> 
> mv has the capacity to rename anything but not only are they no
> directions within man or info how to begin such a procedure -- no one
> apparently has noticed the logical conflict of having one function do
> the job of another.  In short, mv is working outside of its functional
> scope; it may have been built that way but unlike the leaning Tower of
> Pisa there is no value in this conflicting feature which properly should
> be the function of rename.
> 
You have just discovered the prime rule for linux computing:
"For every problem, there is at least one solution, but most likely 
more" :-)

I can understand your confusion because of the names, but rename and mv 
are designed with different goals in mind.

rename is designed to rename a large group of similarly named files, 
while mv is a general purpose move/rename command.

For some actions, you can use either, because there is quite some 
overlap in what they can do. I'll give you some examples.

If you only want to partly rename one single file, you can use either 
command. For example to rename "kermit.frog" to "hermit.frog" you could use
rename k h kermit.frog
or
mv kermit.frog hermit.frog

If you want to completely rename one single file, you could still use 
either command, but rename obviously gets less interesting, especially 
if the old name is a rather long name. Take for example the rename of 
"a_very_very_very_long.one" to "short.name":

rename a_very_very_very_long.one short.name a_very_very_very_long.one
mv a_very_very_very_long.one short.name

You can start to see the pattern already:

rename takes at least three parameters:
1. the part of the filename you want to replace
2. what you would want to replace it with
3. the file that should be renamed

mv takes two parameters for a rename:
1. the old filename
2. the new filename

Depending on what you want to do, one or the other command is more 
convenient.

And by the way, it doesn't matter if you are renaming files or 
directories. It works with either:

mv old.directory new.directory
does the same as
rename old new old.directory

You start to think I'm a good magician already ?
Then let's make things more confusing ;-) :
Your reasoning is:
rename's job should be renaming files, mv's job should be moving files 
around.

As you correctly noticed, mv can also rename files. I'll show you now, 
that 'rename' can also move files !

Suppose you have a file jolly.jumper in directory /home/farm/stable, and 
you want to move it to directory /home/farm/meadow. This can be done by 
either
cd /home/farm
mv stable/jolly.jumper meadow

or
cd /home/farm
rename stable meadow stable/jolly.jumper

Note that neither command renames directory stable to meadow, but both 
actually move the file from one directory to the other !

Now that's magic, isn't it ? ;-)

Ok, I'll tell you my secret then.

To understand how all of this works, you should look at filenames as 
starting from the root directory, so in the above example jolly.jumper's 
real filename is actually /home/farm/stable/jolly.jumper.

So to move this file to another directory is actually the same a 
renaming it, only this time it's not the last part that is changed, but 
the part just before the last '/'.

There is only one limitation to this: you must move to an existing 
directory. This is done to prevent that with a simple typo a file gets 
moved into an unknown directory. Chances are you wouldn't find your file 
anymore afterwards.

Remember: all of the above only shows the actions you can perform with 
either command. However, although you can use both commands, depending 
on the action, one or the other is simpler to use, because they use 
other parameters.


Apart from that, they both have actions the other command can't perform.

Some examples:
Renaming many similarly named files is only possible with rename, or a 
home-brewn shell script using mv.
For example a whole directory containing several files ending in .html 
should be renamed to files ending in .htm (this kind of conversions 
could happen when working in a mixed linux-windows environment for example):
rename .html .htm <directory>/*.html

mv has quite a lot of finetuning options in case the destination file 
already exists: you could tune it to make a backup of the old 
destination file:
mv -b old.name new.name
this will first rename the file new.name to new.name~ if it already 
exists, and then rename old.name to new.name.

You could also have mv ask wether or not to overwrite files:
mv -i old.name new.name

Or you could have mv only overwrite the file if the source filename is 
newer:
mv -u old.name new.name


Hopefully this helps explaining things a bit.

Cheers,

Geert