"Cp" terminal command syntax

Discussion in 'Mac Basics and Help' started by macstatic, Jul 25, 2016.

  1. macstatic macrumors 6502a

    Oct 21, 2005
    I'd like to make a complete backup copy of an external drive including any invisible files/folders and believe "cp -Rv" (copy recursively with verbose) is the way to go, but I'm a little confused about the syntax -more to the point if the directories should have a slash at the end or not, if I need to define everything by using an asterix, and if I can create the destination folder in the same command line, or if I need to create it first?
    I have a few ideas:

    1) cp -Rv /Volumes/Toshiba/ /Users/Alex/Desktop/Toshiba_backup/

    2) cp -Rv /Volumes/Toshiba /Users/Alex/Desktop/Toshiba_backup/

    3) cp -Rv /Volumes/Toshiba /Users/Alex/Desktop/Toshiba_backup

    4) cp -Rv /Volumes/Toshiba/* /Users/Alex/Desktop/Toshiba_backup/

    5) mkdir /Users/Alex/Desktop/Toshiba_backup/
    cp -Rv /Volumes/Toshiba/* /Users/Alex/Desktop/Toshiba_backup/
  2. bradl macrumors 68040


    Jun 16, 2008
    Here's what you should do.

    If you want to keep the same directory name, while you can pass -R to cp, you'd be safer with -a. From the cp(1) man page:

    Here is where it gets confusing, and why the italicized is actually incorrect.

    If you follow what the other options are that -a encapsulates, you'll notice that the italicized is indeed incorrect. -p does indeed preserve the attributes and structure of the file like mentioned. -P states that it if the -R flag is specified, no symbolic links will be copied. But if you look above to the -L flag, it states that if -R is specified, all symbolic links are followed.

    Confusing, isn't it?

    Let's bring in -R. if the source is a directory, cp will copy the directory and entire subdirectory from that point (meaning, directory structure).

    So in essence, if you use -a, you'll get the structure and attributes of the files preserved, all symbolic links followed and preserved, and a recursive copy with the subdirectories intact.

    So let's look at what would happen if you did each of your steps.

    With the command you have as is, it will copy everything in /Volumes/Toshiba (subdirectories included) to /Users/Alex/Desktop/Toshiba_backup. However, file attributes and permissions will not be preserved. Additionally, file ownership will also not be preserved. If there is a file in that directory that you are not the owner of, when cp -R is run, you will become the owner of the files and directories at the destination. For example, if the user 'joe' owns /Volumes/Toshiba/joes_stuff', and Alex is running the cp command, when it is copied to /Users/Alex/Desktop/Toshiba_backup, Alex will own /Users/Alex/Desktop/Toshiba_backup/joes_stuff, not joe.

    Make sense? The -P option was missing, so any permissions and ownership joe had will be lost on the destination files. Those attributes on the original files will remain intact.

    With cp -av, you'll have everything in /Volumes/Toshiba (subdirectories included) copied to /Users/Alex/Desktop/Toshiba_backup. The cp command will create the /Users/Alex/Desktop/Toshiba_backup directory with the same attributes/permissions that is on the /Volumes/Toshiba directory. So when copied by Alex, Joe will still have his ownership and permissions on everything in /Users/Alex/Desktop/Toshiba_backup/joes_stuff.

    Make sense?

    Same as #1. with cp -a, you will get the same result. trailing slash will not matter.

    You will still be dealing with the attributes/permissions/ownership issue here. Hence why I recommend cp -av.

    Same as #1. Again, trailing slash will not matter.

    You will still be dealing with the attributes/permissions/ownership issue here. Hence why I recommend cp -av.

    This one will matter. As you are using /Volumes/Toshiba/*, you are indicating that it is the files and directories inside /Volumes/Toshiba that will be copied, not the directory itself. Additionally, since you are talking files at this point, cp will NOT create the /Users/Alex/Desktop/Toshiba_backup directory. It will need to be manually created. Finally, since you are dealing with the files, no hidden files will be copied.

    You will still be dealing with the attributes/permissions/ownership issue here. Hence why I recommend cp -av.

    Same as #4. While you are creating the destination directory (a good thing) and then running the cp -Rv command, you are going to get the same result with missing any hidden files in the top level directory. For example, if you have these files:


    /Volumes/Toshiba/temp/bookmarks will be copied to /Users/Alex/Desktop/Toshiba_backup, but
    /Volumes/Toshiba/.bash_history, /Volumes/Toshiba/.bashrc, and /Volumes/Toshiba/.git/locationdata.txt will NOT be copied. This is because those files and that directory are hidden to * in the top level directory. Additionally, you'll run into the attributes/ownership/permissions problem mentioned above.

    Again, what I would recommend is this:

    cp -av /Volumes/Toshiba /Users/Alex/Desktop/Toshiba_backup

    -a gives you -pPR (explained above), and -v gets you the verbose, which you already know.

    Hope this helps.

  3. chown33 macrumors 604

    Aug 9, 2009
    Since an entire disk is being backed up, I recommend "Disk Utility.app" to create a new disk image from the Toshiba volume.

    If one insists on using the command-line, I'd use 'rsync' instead of 'cp'. The -a option of rsync will be helpful.
  4. bradl macrumors 68040


    Jun 16, 2008
    I'd agree with that as well.

  5. macstatic thread starter macrumors 6502a

    Oct 21, 2005
    Thanks to both for insightful info.
    Actually I prefer the simpler Mac way of doing things rather than the command line, but since the command line is often more powerful and versatile I've learnt that sometimes there's just no other way..

    I had completely forgotten about Disk Utility's ability to create disk images. But since it's a matter of drag & drop (dragging the source drive over to the disk image, then dropping it), will that ensure invisible files/folders will also be included?

    As for "rsync -a", how would that differ from "cp -av"?
  6. chown33 macrumors 604

    Aug 9, 2009
    It should replicate the entire disk exactly. If you're concerned, try it, then look on the disk image to see if it copied some invisibles you know exist on the original.

    IIRC, it doesn't need to be drag-n-drop. There should be a command under one of the menus that lets you choose a source volume using the "Choose a THING" dialog, then another dialog that lets you choose where to write it, and offers options like what format (e.g. read-only, compressed).

    The options and capabilities of Disk Utility vary by OS version. It might help to post what OS version you're using.
  7. macstatic thread starter macrumors 6502a

    Oct 21, 2005
  8. Cineplex macrumors 6502a


    Jan 1, 2016
    Don't forget about the ditto command. I much prefer it over cp. If you have any old OS 9 files mixed in it will copy the resource fork if you specify the flag.

Share This Page