Subject: Re: bug#16889: erratic behavior of cp command i.e while copying files (using *) from one directory to another directory

From: Pádraig Brady <[email-masked]>
Date: Wed, 26 Feb 2014 16:57:31 +0000

tag 16889 notabug
close 16889
stop

On 02/26/2014 12:36 PM, Anil Kumar wrote:
> Hello
> 
> I have observed, one erratic behavior of cp command i.e while copying
> files (using *) from one directory to another directory if
> we miss destination directory then cp command copies content of 1st
> file into 2nd file of same source directory instead of giving any
> error.
> 
> Here is the scenario:
> 
> #ls -l
> total 8
> drwxr-xr-x 2 root root 4096 Feb 26 17:34 ddir
> drwxr-xr-x 2 root root 4096 Feb 26 17:36 sdir
> 
> 
> # echo "hello" > sdir/hello.txt
> # echo "bye" > sdir/bye.txt
> 
> # cat sdir/hello.txt
> hello
> 
> # cat sdir/bye.txt
> bye
> 
> # cp sdir/*.txt
> 
> # cat sdir/hello.txt
> bye
> 
> # cat sdir/bye.txt
> bye
> 
> [Anil] Not sure, why "cp sdir/*.txt" command has copied data of
> bye.txt into hello.txt ? I think, its not desired behavior.
>  'cp' command should have produced error e.g destination is missing etc .
> 
> One more example of similar behavior:
> # echo "hello" > sdir/hello.txt
> # echo "bye" > sdir/bye.txt
> 
> # cat sdir/hello.txt
> hello
> 
> # cat sdir/bye.txt
> bye
> 
> # cp sdir/*
> 
> # cat sdir/hello.txt
> bye
> 
> # cat sdir/bye.txt
> bye
> 
> [Anil]Here again "bye" has been copied into hello.txt which is not
> desired behavior.

So cp doesn't expand the sdir/* itself.
The shell does that and just passes the separate arguments to cp.
This is a disadvantage of the globbing being done by the shell,
but there are advantages to.

thanks,
Pádraig.