9

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.

3
  • Does it make any difference to drag & drop if you set Ignore Ownership on the external drive? [from Get Info]
    – Tetsujin
    Apr 15, 2015 at 9:00
  • Ignoring ownership on an external drive will allow you to access those files with all permissions available, but will not help after they are copied over.
    – Phoenix
    Apr 15, 2015 at 9:56
  • Get Info does not show any Ignore Ownership switch for the drive... Does not seem to be an option (using Mac OS X 10.10.3).
    – pdeli
    Apr 15, 2015 at 11:04

2 Answers 2

8

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.

5
  • Tried cp -R /original_folder/ /destination_folder/ but Terminal says Operation not permitted. So I sudoed the command, but the copied files have administrator access level. The original_foldercomes from an Administrator privileged disk, but the destination folder has no administrator privileges. Any idea on how to solve this with cp or do I have to resort to chmod?
    – pdeli
    Apr 15, 2015 at 10:59
  • Updated my answer to address your comment/question.
    – Phoenix
    Apr 15, 2015 at 17:05
  • Just did what you suggested Phoenix, but when I try to move the folder (containing files) through drag-&-drop the System wants to copy the files and/or folders, whereas when I do that with any other files and/or folders created on the disk, they are moved (not copied, with a small plus sign embedded in round green circle).
    – pdeli
    Apr 15, 2015 at 17:37
  • In other words, I would like that the files I copy from the external hard drive to my profile in the internal SSD of my computer to act like all the other files I created and that are residing in my profile of the SSD inside my Mac. :)
    – pdeli
    Apr 15, 2015 at 17:47
  • Keep in mind that when you cross the boundaries of a drive, you will always copy the files by default. You can hold down either Cmd or Alt (I always confuse them, but cannot verify it right now) to switch between copying and moving. If you do not have the proper permissions, you will ALWAYS copy the files instead of moving them. To help that, you need to run the two last commands I posted in my answer on the folder holding the files. On any drive OTHER THAN the boot drive, you can also run those commands on the entire drive.
    – Phoenix
    Apr 15, 2015 at 18:01
6

Copying files without using chmod or chown: create a tar.gz archive of what you want to copy:

tar cvzf backup.tgz <folder or files you want to copy>

copy the tgz file to your destination and expand it there:

tar xvzf backup.tgz

The advantage of this method is that it works for folders, keeping the permissions of each file, the folder can be of any size and it can work with different file system formats, for example I used an external hdd with exfat to copy my tgz from a mac to a linux without any problem.

2
  • 2
    you can also use tar without an intermediate file if you don't have enough spare disk space by using - as the filename (meaning stdin/stdout) and piping one tar into another - something like tar cf - <folder or files> | tar xvf - (which will create a copy of the source in the current directory - tweak the tar options to suit your use case)
    – CupawnTae
    Aug 24, 2019 at 15:41
  • excellent solution
    – Enrique
    Apr 1, 2020 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .