Copy recursively files and folders, but preserving everything while gaining ownership

permissionterminal

I would like to copy files and folders recursively from an external drive to my internal SSD.

However, when I drag and drop, and, subsequently, when I use the copied files, I am always asked to enter an administrator credentials. I don't know if it is a chmod or chown issue, or both (difficulties to making the difference between them).

Is there a way (through Terminal I guess) to copy the files:

  • while preserving all the files and folders details (i.e. original date/time stamps and whatever metadata necessary/useful);
  • but gaining ownership (so I don't have to enter administrator credentials each time I want to look, modify or move a file).

I guess it is either cp or rsync, but I must admit having difficulties sorting out and understanding all the available switches.

Best Answer

In order to successfully copy the files in the first place, you need at least have read access to the files at the original location.

To make sure that you can read them, you can either change their permissions with sudo chmod -R o+rx /drag/your/original/folder/from/Finder/here or as suggested in the comments of your question have ownership of the drive ignored for the time-being, which leaves the original files unchanged.

To actually copy the files over, use the following command:

cp -R /drag/your/original/folder/from/Finder/here /drag/your/target/folder/from/Finder/here/

For more information during the copy process, use the following command:

cp -Rv /drag/your/original/folder/from/Finder/here /drag/your/target/folder/from/Finder/here/

Mind the slash at the end of the target folder or the target folder itself will be overwritten by the contents.

Understanding permissions when moving/copying files in UNIX/BSD (Mac OS X's underlying system is based on BSD):

  1. When moving a file from A to B on the same partition/volume, the ownership of the files will not change
  2. When moving a file from A to B between different partitions/volumes, the ownership of the files will change to the user moving them (applying sudo to the copy command will have the target files ownership set to root)
  3. When copying a file from A to B regardless of the location will apply the ownership of the target files to the user copying them (applying sudo to the copy command will have the target files ownership set to root)

With number 1 only the file descriptors will change internally, but no file will be physically moved. With 2 and 3 the files are physically copied over to the new location with step 2 doing a second step and deleting them from the original location after each copy.

UPDATE (to address comment):

If you copied over the files using sudo, you can modify the permissions of the target files using chown and chmod.

Commands are:

sudo chown -R <user>[:group] /drag/your/target/folder/from/Finder/here

<user> is the account name of your user (not the full name)
:group is a group, which by default for any user is staff and for any admin is admin. Since it is not required, you can omit to leave it as is (hence the square brackets)

sudo chmod -R 770 /drag/your/target/folder/from/Finder/here

This will give you and the primary group r/w and execution/traversal rights on the files.