Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Under what conditions might one wish to use the cp command on a directory yet have it not be recursive? Consider:

$ tree
.
└── old
    └── inner
        └── a.txt

2 directories, 1 file

$ cp old/inner/ .
cp: omitting directory `old/inner/'

$ tree
.
└── old
    └── inner
        └── a.txt

2 directories, 1 file

What did that do at all? So why would one ever not use the -r flag, and therefore why isn't it implied without specifying it? Note that the -r flag has no ill effect on regular files:

$ cp -r old/inner/a.txt .
$ ls
a.txt  old

I do realize that I could alias cp to cp -r but my goal is to understand what is the reason, not fix anything.

share|improve this question
1  
What about cp * new/? –  choroba Jul 10 '13 at 12:20
add comment

2 Answers

up vote 3 down vote accepted

I often want to copy only the files from one directory, why does that strike you as strange? It depends on what you are trying to do. For example, I will sometimes have all the files I am using for a project in a directory, say these files are necessary for my program to run. I may also have various sub directories with old builds or data files or whatever. If I want to transfer this to a server in order to run my program, I will only want the files not the sub directories so cp's default is exactly what I need.

Apart from the simple fact that sometimes you do not want recursive copying, in general recursion needs to be explicitly enabled for just about all programs. This is the standard and this is what a user would expect the first time one uses the program. Therefore, making recursion the default would not be a good idea. Needing to add an -r flag for recursion is the expected behavior, in general you don't want recursion by default (think chmod or grep or ls etc).

share|improve this answer
    
It is quite interesting that mv is a deviation in this standard behavior. –  Raphael Ahrens Jul 10 '13 at 14:00
2  
@RaphaelAhrens it isn't really, when you mv a directory, you are essentially renaming it, the command is not recursive as such. –  terdon Jul 10 '13 at 14:05
    
yeah I know. But many beginners don't expect this. I think the main reason why they have the -r flag is, because they invented there own hierarchy file system and probably before they did it they already had programs like cp, mv and ls. So they just added the new functionality and now we are stuck with it. –  Raphael Ahrens Jul 10 '13 at 14:15
    
Thank you, your first paragraph answers the question exactly. I was looking for a use case. –  dotancohen Jul 10 '13 at 14:41
    
@dotancohen also consider that it is much easier to enable recursion when needed than to exclude directories when needed. –  terdon Jul 10 '13 at 14:46
show 1 more comment

Recursively coping directories has the potential to overwrite a large number of files. If you were to do this unintentionally you could do a lot of harm with one command. The default being non-recursive makes sure that every time you copy one directory into another you are doing so explicitly and thus have theoretically thought about what you typed before you executed it.

This is not the best example, but imagine what would happen if you accidentally typed the following command without realizing both targets were directories:

cp /bin /usr # /usr/bin already exists and has important stuff in it

EDIT1: One solution could be to interactively prompt the user and ask "Are you sure?" instead of adding an explicit flag. This, however, does not quite fit into the UNIX philosopy that well: "Don't insist on interactive input" [McIlroy78]. cp is an ancient UNIX program that has obviously changed some over time, but this lineage can still be seen in many aspects of its design.

EDIT2: I found several other good reasons for this (there are probably more too). One can be found on StackExchange.

  1. Command line options and the fundamental behaviour of programs as old and as common as cp can be hard to change because of their use in shell scripts. Some scripts could depend on this behaviour (refusing to copy directories). An interactive prompt is not something you want in many shell scripts either.

  2. Coping files and coping directories are two fundamentally different actions that necessitate conceptually separate commands or options to handle them: Why unix mv program doesn't need -R (recursive) option for directories but cp does need it?

[McIlroy78] The Bell System Technical Journal. Bell Laboratories. M. D. McIlroy, E. N. Pinson, and B. A. Tague. “Unix Time-Sharing System Forward”. 1978. 57 (6, part 2). p. 1902.

share|improve this answer
    
The "If you were to do this unintentionally you could do a lot of harm with one command." argument would be logical if the restriction were a "Are your sure" prompt. It might be a desirable side effect, but it is obviously not the intention of the flag to add an artificial "make the user think about it" step. –  dotancohen Jul 10 '13 at 12:42
    
@dotancohen See my edit. –  Matthew Mellott Jul 10 '13 at 13:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.