LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-10-2011, 05:20 AM   #1
armandino
Member
 
Registered: Oct 2005
Posts: 72

Rep: Reputation: 15
Weird behaviour of the "cp" command


I noticed something that looks strange to me, maybe because I'm not so familiar with Linux yet.

Supposing I'm in a directory containing various files and subdirectories, one of which is called BACKUP and is initially empty, what exactly happens if I launch the following command line:

Code:
cp -R . BACKUP
???

If I'm not mistaken, all the content of the current directory should be copied into the BACKUP directory, apart from the directory BACKUP itself.
Actually I get a message saying something like:

Code:
cannot copy a directory onto itself
BUT when I look into the BACKUP directory I find another directory called BACKUP which I did not expect to find.
Moreover, if I launch the same command again I get the same message (repeated two or more times) and then I find that inside the "second" BACKUP directory there is a third one, with the same name again. In other words, every time I launch the command I get a deeper tree of "BACKUP" directories, as if the command was messing everything up.
I know that most probably I'm the one who's messing things up and I also know the "problem" can be easily bypassed by using as a destination for the copy a directory located elsewhere, but I'd like anyway to understand the reason for such a behaviour.

Thanks for any help.
 
Old 01-10-2011, 06:33 AM   #2
andrewthomas
Senior Member
 
Registered: May 2010
Location: Chicago Metro
Distribution: Arch, Gentoo, Slackware
Posts: 1,690

Rep: Reputation: 312Reputation: 312Reputation: 312Reputation: 312
Quote:
Originally Posted by armandino View Post
Code:
cp -R . BACKUP
???

If I'm not mistaken, all the content of the current directory should be copied into the BACKUP directory, apart from the directory BACKUP itself.
You are mistaken.

How would the command know to exclude the BACKUP subdirectory?

You answered the question yourself.

When using the recursive option of cp, one must make the destination be in a different directory.
 
Old 01-10-2011, 07:43 AM   #3
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
All that is saying is to copy EVERYTHING in this directory to NEW directory, which technically includes the directory you are trying to copy into. If anything, copy into an outside directory, or use something like
Code:
cp -r [!.]*
What that does is copies everything except for files that start with . I use it for that except purpose, but maybe you can come up with something to suit your needs.
 
Old 01-11-2011, 04:51 AM   #4
armandino
Member
 
Registered: Oct 2005
Posts: 72

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by andrewthomas View Post
You are mistaken.
How would the command know to exclude the BACKUP subdirectory?
I'm not saying I'm not mistaken.
There obviously is a mistake and it is most probably on my side.

Actually, however, the command seems to know very well that the BACKUP subdirectory has an... ambiguous role in the operation. Otherwise, how could it show the message:
Code:
cannot copy a directory onto itself
?

Anyway, if such a use of the cp command is erratic and can lead to unpredictable results, maybe the command should not accept such a command line or, at least, warn the user about the "danger".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ns:"error when calling class OldSim"&tclsh:"invalid command+child process exits abn." shojaru Linux - Newbie 0 03-05-2009 04:23 AM
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 03:27 AM
Feeding the output of "diff" or "cat" command to dpkg --purge kushalkoolwal Debian 9 06-19-2008 07:27 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Tiny Sofa 2.0 - I thought "halt", "reboot" were only root command ?? sorcerer Linux - Distributions 1 08-21-2004 03:28 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration