shell-novice icon indicating copy to clipboard operation
shell-novice copied to clipboard

Ep3: Explain recursive copy a bit better

Open gcapes opened this issue 4 years ago • 4 comments
trafficstars

https://swcarpentry.github.io/shell-novice/03-create/index.html#copying-files-and-directories

Here we copy a directory recursively. It isn't a particularly clearly motivated example though, given there is only one file in the directory being copied. I'm not suggesting adding anything to the example other than to attempt to copy the directory without the -r option to show that it doesn't work.

gcapes avatar Oct 27 '21 15:10 gcapes

The line: 'The cp command works very much like mv, except it copies a file instead of moving it.' is misleading and actually not true. The 'cp' command "duplicates" a file and its contents into a new file. In the case of a directory, the only way to duplicate is to use 'cp -r', that is, a recursive copy. The 'mv' command "renames" a file/directory. It does not duplicate it.

clizarraga-UAD7 avatar Jun 06 '22 04:06 clizarraga-UAD7

I'm not sure what's misleading. The syntax is very similar, but it copies a file instead of moving a file.

gcapes avatar Jun 06 '22 07:06 gcapes

In UNIX/Linux files and directories are similar.

A file has only one type of content that can be of different type (text, image, executable binary, executable shell, other). A directory contains files and possibly more subdirectories and files below them (tree hierarchy).

Every file and directory have a specific pointer (address) in the filesystem (disk, RAM,...).

When you rename (mv) a file or directory, you are changing the name only. (Only one file/directory).

When you copy (cp) a file into another, you duplicate that file (new address, with contents of first file). You have 2 objects.

When you want to copy (duplicate) a directory and all its contents (subdirectories and files), you must specify that it has to be a recursive copy, in order to duplicate all the tree substructure, that is why we use a recursive copy (cp -r Dir DirNew, or cp --recursive Dir NewDir). You will end up with 2 copies of the directory.

I like the site https://explainshell.com/ that explains the shell commands and has many examples.

Hope I did not confuse you more.

clizarraga-UAD7 avatar Jun 06 '22 17:06 clizarraga-UAD7

As always I think there's a trade off between accuracy and clarity, and for novices clarity usually trumps accuracy. So here, I think saying the two commands work "very much the same" is fine. cp and mv both result in a file/dir with a new name/path, and the syntax is similar. The lesson doesn't suggest that mv duplicates a file so I'm still struggling to see the confusion I'm afraid.

gcapes avatar Jun 07 '22 08:06 gcapes

Hello, I think in this instance clarity is not trumping accuracy as the differentiation between cp and mv only reads as clear, but in practice (when following along) it gets confusing. What makes it confusing is that there is no input given as to how to exit a directory in the lesson [I have just been using cd -], which could better show the practical impacts of these changes. Without exiting from the directory, it just seems as if I am playing with file names and replicating them, so it becomes harder to tell if I am simply creating replicated copies of files within a specific directories hierarchy [which mv seems to do] or copying a file into a new directory [which I think is the intention of using cp].

This is my novice contribution to this discussion, given the confusions I had about mv seeming more file oriented and cp seeming more directory oriented; but this is only my interpretation after exiting directories myself.

tjjohn93 avatar Dec 15 '22 16:12 tjjohn93

I think everything is well explained here. the mv and cp commands are different: mv moves a file or directory while cp duplicates them.

lemythe avatar Apr 08 '23 17:04 lemythe