[Techtalk] copying similar files in one command

dominik.schramm at gmxpro.net dominik.schramm at gmxpro.net
Tue Nov 2 01:58:00 EST 2004


Hi,

I have just a slight correction to make.

"Kathryn Hogg" <kjh at flyballdogs.com> writes:

>> cp *ed* /home/newdir
>
> I always recommend using a trailing "/" if the destination is a 
> directory:

So do I, but...

> [...] if /home/newdir doesn't exist, the first form will happily
> copy *ed* to a file named /home/newdir.  The result is that it will
> ultimately just contain a copy of the last file.

At least for my version of cp (GNU core utilities v5.2.1) it doesn't 
work that way, but rather like this:

excerpt from man page:

   cp [OPTION]... SOURCE DEST
   cp [OPTION]... SOURCE... DIRECTORY

So, you either have exactly one source and one destination argument,
or multiple source arguments and exactly one directory argument
following it.

This is how my cp behaves:
  $ touch file1 file2 file3
  $ cp file* dir1
  cp: `dir1': specified destination directory does not exist
  Try `cp --help' for more information.
  $

Now, lets create a file called "dir1":
  $ touch dir1
  $ cp file* dir1
  cp: copying multiple files, but last argument `dir1' is not a directory
  Try `cp --help' for more information.
  $

In case of only one source argument:
  $ cp file1 dir1
  $ ls -l dir1
  -rw-r--r--  1 me   users 0 Nov  1 15:51 dir1
  $

And I think this is a very sensible feature because it happens quite
often that I mistype a directory name -- so far, nothing bad resulted
apart from a syntax error message.

If there is only one source argument and the destination argument is 
a filename which already exists, then this file is overwritten unless
the switch "-i" (interactive) is given, which some distributions set
automatically (in bash) by defining the alias "cp=cp -i".

> This isn't the worst thing with cp, but if you were renaming the 
> files:
>
> mv *ed* /home/newdir

mv has the same built-in "intelligence" as cp on my system.

kind regards,
dominik




More information about the Techtalk mailing list