3

To copy a directory and its contents cp requires a -r switch.

To move a directory and its contents mv does not require a -r switch.

What is the reason for this?

EDIT: Don't worry. Despite searching, I found out after posting this question that it has been answered here.

0
1

When you use the mv command you're just changing metadata about the top level directory or individual file, when you're using the cp command you're literally making copies of a individual file or a directory tree. In order to differentiate between these two modes, the cp command needs you to specify which behavior you want to enlist.

You can view the metadata using stat.

$ stat /home/vagrant/adir
  File: ‘/home/vagrant/adir’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 794269      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-07-14 10:57:17.103849041 -0400
Modify: 2018-07-14 10:57:17.103849041 -0400
Change: 2018-07-14 10:57:17.103849041 -0400
 Birth: -

After we move it:

$ stat /tmp/adir
  File: ‘/tmp/adir’
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: fd00h/64768d    Inode: 794269      Links: 2
Access: (0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-07-14 10:57:17.103849041 -0400
Modify: 2018-07-14 10:57:17.103849041 -0400
Change: 2018-07-14 10:57:53.323950044 -0400
 Birth: -

The mv command is just manipulating the metadata regarding the directory, and so doesn't need to actually make copies of anything to do this work.

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