bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cp command potental problem


From: Bob Proulx
Subject: Re: cp command potental problem
Date: Wed, 17 Jun 2009 11:12:01 -0600
User-agent: Mutt/1.5.18 (2008-05-17)

Doug Stamford wrote:
> One of my co-workers brought to my attention a most unusual problem with
> the cp command. Originally, this problem was discovered on a AIX system
> but he also found this problem exist on the Linux system. He contacted
> IBM directly to inform them, but was told the cp command was functioning
> properly.

They were correct.  The cp command is functioning properly from your
description of the behavior.  But perhaps they didn't explain why well
enough?  I will give it a try.

> The problem on occurs when using the cp command and there are only 2
> source files and the '*' is used, and the destination is inadvertently
> left off. 

The shell expands file glob characters.  The '*' is a file glob
(because it matches a glob of characters) and the cp command never
sees the '*'.  The cp command sees the expansion.  It is not possible
for the cp command to know that you entered a '*' and not two file
names.  The cp command does not receive a '*' argument.  The cp
command recieves actual file arguments, in this case as expanded by
the command shell.  The command shell expands all file globs to match
files and convert them into actual file names.

> mkdir test
> cat /etc/snmp/snmpd.conf > test/file1.txt
> cat /usr/local/apache/conf/httpd.conf > test/file2.txt
> cp test/*.txt <enter>
> 
> You will now notice no error output and the file2.txt is the same as
> file1.txt

Because there were only two files in the directory the '*' was
expanded by the shell to match two file names.  The cp command was
given two filename arguments.

You can use the 'echo' command to show you what is being done.

  echo cp test/*.txt

The output of the echo will show you that the command you are invoking
is:

  cp test/file1.txt test/file2.txt

With that command cp is then copying from one file to the other as it
has been instructed to do.

If there are three program arguments then the last one must be a
directory or it is an error.  But with only two it isn't possible to
determine the caller's intention.

Hope that helps,
Bob




reply via email to

[Prev in Thread] Current Thread [Next in Thread]