How do I copy a folder keeping owners and permissions intact?Move FreeBSD installation to smaller HDDhow to...

If the Charles SSL Proxy shows me sensitive data, is that data insecure/exposed?

Germany rejected my entry to Schengen countries

How do we properly manage transitions within a descriptive section?

Vehemently against code formatting

Bash - Execute two commands and get exit status 1 if first fails

Does the fact that we can only measure the two-way speed of light undermine the axiom of invariance?

why "American-born", not "America-born"?

How can I prevent Bash expansion from passing files starting with "-" as argument?

How is dynamic resistance of a diode modeled for large voltage variations?

Parse a C++14 integer literal

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

Does science define life as "beginning at conception"?

Do most Taxis give Receipts in London?

How could Dwarves prevent sand from filling up their settlements

Is there a way to generate a mapping graph like this?

Best practice for printing and evaluating formulas with the minimal coding

Farthing / Riding

What quantum phenomena violate the superposition principle in electromagnetism?

Is there a word for pant sleeves?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

Why does an injection from a set to a countable set imply that set is countable?

Ribbon Cable Cross Talk - Is there a fix after the fact

If you attack a Tarrasque while swallowed, what AC do you need to beat to hit it?

Why "strap-on" boosters, and how do other people say it?



How do I copy a folder keeping owners and permissions intact?


Move FreeBSD installation to smaller HDDhow to do checksum on a folder in SolarisUnable to preserve ownership in a copy but able with a move?IzPack Linux Keep same permissionsHow do I archive a folder keeping owners and permissions intact?How to change folder permissions during package installationchange home folder permissions on exit/disconnectI want to change permissions on all folders/files excluding some of themHow does command “pmount” work without “sudo”How to set sftp-server permissions and ownership in a shared sftp-folder? (ACL's ?)Use external drive as a folder in nginxHow to set the future permissions of content in folderUsing Tar, how to preserve permissions of parent folders when given just a specific file?Folder group ownership permissions and problems






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







348















So I was going to back up my home folder by copying it to an external drive as follows:



sudo cp -r /home/my_home /media/backup/my_home


With the result that all folders on the external drives are now owned by root:root. How can I have cp keep the ownership and permissions from the original?










share|improve this question




















  • 23





    Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

    – Stijn Geukens
    Aug 4 '14 at 14:54








  • 3





    I'm glad he asked too

    – Ole
    Apr 7 '16 at 16:28






  • 5





    @StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

    – Kolob Canyon
    Nov 15 '16 at 0:33




















348















So I was going to back up my home folder by copying it to an external drive as follows:



sudo cp -r /home/my_home /media/backup/my_home


With the result that all folders on the external drives are now owned by root:root. How can I have cp keep the ownership and permissions from the original?










share|improve this question




















  • 23





    Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

    – Stijn Geukens
    Aug 4 '14 at 14:54








  • 3





    I'm glad he asked too

    – Ole
    Apr 7 '16 at 16:28






  • 5





    @StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

    – Kolob Canyon
    Nov 15 '16 at 0:33
















348












348








348


108






So I was going to back up my home folder by copying it to an external drive as follows:



sudo cp -r /home/my_home /media/backup/my_home


With the result that all folders on the external drives are now owned by root:root. How can I have cp keep the ownership and permissions from the original?










share|improve this question
















So I was going to back up my home folder by copying it to an external drive as follows:



sudo cp -r /home/my_home /media/backup/my_home


With the result that all folders on the external drives are now owned by root:root. How can I have cp keep the ownership and permissions from the original?







permissions cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 16 '14 at 20:58









Braiam

24k2079144




24k2079144










asked Jul 20 '12 at 15:27









PsachnodaimoniaPsachnodaimonia

1,855398




1,855398








  • 23





    Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

    – Stijn Geukens
    Aug 4 '14 at 14:54








  • 3





    I'm glad he asked too

    – Ole
    Apr 7 '16 at 16:28






  • 5





    @StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

    – Kolob Canyon
    Nov 15 '16 at 0:33
















  • 23





    Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

    – Stijn Geukens
    Aug 4 '14 at 14:54








  • 3





    I'm glad he asked too

    – Ole
    Apr 7 '16 at 16:28






  • 5





    @StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

    – Kolob Canyon
    Nov 15 '16 at 0:33










23




23





Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

– Stijn Geukens
Aug 4 '14 at 14:54







Yes, it would, be I'm glad he asked because google is still faster than 'man' :-).

– Stijn Geukens
Aug 4 '14 at 14:54






3




3





I'm glad he asked too

– Ole
Apr 7 '16 at 16:28





I'm glad he asked too

– Ole
Apr 7 '16 at 16:28




5




5





@StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

– Kolob Canyon
Nov 15 '16 at 0:33







@StijnGeukens It's not even that google is faster, it's that even man to me is cryptic. In this case, man at -r says same as --preserve=mode,ownership,timestamps. How was I supposed to know that means keep the original permissions!?

– Kolob Canyon
Nov 15 '16 at 0:33












8 Answers
8






active

oldest

votes


















423














sudo cp -rp /home/my_home /media/backup/my_home


From cp manpage:



 -p     same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST]
preserve the specified attributes (default: mode,ownership,timestamps),
if possible additional attributes: context, links, xattr, all





share|improve this answer



















  • 111





    Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

    – Wildcard
    Oct 14 '15 at 3:54






  • 22





    It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

    – Paul
    Nov 18 '15 at 17:01






  • 4





    cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

    – dubiousjim
    Sep 30 '17 at 16:30



















93














You can also use rsync.



sudo rsync -a /home/my_home/ /media/backup/my_home/


From the rsync manpage:



 -a, --archive
This is equivalent to -rlptgoD. It is a quick way of saying you want
recursion and want to preserve almost everything (with -H being a notable
omission). The only exception to the above equivalence is when
--files-from is specified, in which case -r is not implied.

Note that -a does not preserve hardlinks, because finding multiply-linked
files is expensive. You must separately specify -H.


See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686



Note the trailing slashes (see manpage for details).






share|improve this answer





















  • 3





    +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

    – Izkata
    Jul 20 '12 at 17:59








  • 14





    NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

    – Perseids
    Sep 23 '14 at 6:44






  • 2





    Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

    – John Hamilton
    Mar 16 '18 at 11:59






  • 3





    @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

    – Olivier Pons
    Oct 18 '18 at 7:04



















52














cp -a


Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).



From man cp:



   -a, --archive
same as -dR --preserve=all





share|improve this answer































    20














    I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).



    PS: -R instead of -r is just habit from using ls -lR.






    share|improve this answer



















    • 1





      cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

      – EM0
      Mar 25 '15 at 12:27













    • Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

      – geckon
      Jul 8 '15 at 13:23






    • 2





      I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

      – StarNamer
      Jul 8 '15 at 16:09





















    14














    cp has an option to preserve file ownership. From the manual page of cp:



    -p    Cause cp to preserve the following attributes of each source file in the copy: modification
    time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
    Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
    be preserved.





    share|improve this answer































      11














      You can do something like this:



      tar cf - my_home | (cd /media/backup; sudo tar xf - )


      tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.






      share|improve this answer































        7














        The answer is simple: cp has a -p option that preserves permissions (here's a fish).



        But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (want to learn how to fish?).






        share|improve this answer

































          0














          you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
          So, do your backup safely with the following command.



          cp -r --preserve=all /home/my_home /media/backup/my_home




          share








          New contributor



          Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.




















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f43605%2fhow-do-i-copy-a-folder-keeping-owners-and-permissions-intact%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            423














            sudo cp -rp /home/my_home /media/backup/my_home


            From cp manpage:



             -p     same as --preserve=mode,ownership,timestamps

            --preserve[=ATTR_LIST]
            preserve the specified attributes (default: mode,ownership,timestamps),
            if possible additional attributes: context, links, xattr, all





            share|improve this answer



















            • 111





              Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

              – Wildcard
              Oct 14 '15 at 3:54






            • 22





              It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

              – Paul
              Nov 18 '15 at 17:01






            • 4





              cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

              – dubiousjim
              Sep 30 '17 at 16:30
















            423














            sudo cp -rp /home/my_home /media/backup/my_home


            From cp manpage:



             -p     same as --preserve=mode,ownership,timestamps

            --preserve[=ATTR_LIST]
            preserve the specified attributes (default: mode,ownership,timestamps),
            if possible additional attributes: context, links, xattr, all





            share|improve this answer



















            • 111





              Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

              – Wildcard
              Oct 14 '15 at 3:54






            • 22





              It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

              – Paul
              Nov 18 '15 at 17:01






            • 4





              cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

              – dubiousjim
              Sep 30 '17 at 16:30














            423












            423








            423







            sudo cp -rp /home/my_home /media/backup/my_home


            From cp manpage:



             -p     same as --preserve=mode,ownership,timestamps

            --preserve[=ATTR_LIST]
            preserve the specified attributes (default: mode,ownership,timestamps),
            if possible additional attributes: context, links, xattr, all





            share|improve this answer













            sudo cp -rp /home/my_home /media/backup/my_home


            From cp manpage:



             -p     same as --preserve=mode,ownership,timestamps

            --preserve[=ATTR_LIST]
            preserve the specified attributes (default: mode,ownership,timestamps),
            if possible additional attributes: context, links, xattr, all






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 20 '12 at 15:51









            guidoguido

            5,80811628




            5,80811628








            • 111





              Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

              – Wildcard
              Oct 14 '15 at 3:54






            • 22





              It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

              – Paul
              Nov 18 '15 at 17:01






            • 4





              cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

              – dubiousjim
              Sep 30 '17 at 16:30














            • 111





              Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

              – Wildcard
              Oct 14 '15 at 3:54






            • 22





              It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

              – Paul
              Nov 18 '15 at 17:01






            • 4





              cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

              – dubiousjim
              Sep 30 '17 at 16:30








            111




            111





            Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

            – Wildcard
            Oct 14 '15 at 3:54





            Much better to use cp -a. This also includes the -recursive flag, but it does more than that—it preserves everything about the file; SELinux attributes, links, xattr, everything. It's "archive mode." There are better tools for making a backup, but if you're using cp for a backup, don't use anything other than cp -a.

            – Wildcard
            Oct 14 '15 at 3:54




            22




            22





            It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

            – Paul
            Nov 18 '15 at 17:01





            It works, but Patience is Good here. The command will only set everything right when it finishes: While it's still copying a directory, and you're running cp as root, the directory will be owned by root. It will only set it to the right permissions when it finishes with this directory.

            – Paul
            Nov 18 '15 at 17:01




            4




            4





            cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

            – dubiousjim
            Sep 30 '17 at 16:30





            cp -a doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to use cp -pR. On my current OS X system though (10.10.15), cp -a seems to be honored.

            – dubiousjim
            Sep 30 '17 at 16:30













            93














            You can also use rsync.



            sudo rsync -a /home/my_home/ /media/backup/my_home/


            From the rsync manpage:



             -a, --archive
            This is equivalent to -rlptgoD. It is a quick way of saying you want
            recursion and want to preserve almost everything (with -H being a notable
            omission). The only exception to the above equivalence is when
            --files-from is specified, in which case -r is not implied.

            Note that -a does not preserve hardlinks, because finding multiply-linked
            files is expensive. You must separately specify -H.


            See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686



            Note the trailing slashes (see manpage for details).






            share|improve this answer





















            • 3





              +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

              – Izkata
              Jul 20 '12 at 17:59








            • 14





              NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

              – Perseids
              Sep 23 '14 at 6:44






            • 2





              Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

              – John Hamilton
              Mar 16 '18 at 11:59






            • 3





              @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

              – Olivier Pons
              Oct 18 '18 at 7:04
















            93














            You can also use rsync.



            sudo rsync -a /home/my_home/ /media/backup/my_home/


            From the rsync manpage:



             -a, --archive
            This is equivalent to -rlptgoD. It is a quick way of saying you want
            recursion and want to preserve almost everything (with -H being a notable
            omission). The only exception to the above equivalence is when
            --files-from is specified, in which case -r is not implied.

            Note that -a does not preserve hardlinks, because finding multiply-linked
            files is expensive. You must separately specify -H.


            See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686



            Note the trailing slashes (see manpage for details).






            share|improve this answer





















            • 3





              +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

              – Izkata
              Jul 20 '12 at 17:59








            • 14





              NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

              – Perseids
              Sep 23 '14 at 6:44






            • 2





              Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

              – John Hamilton
              Mar 16 '18 at 11:59






            • 3





              @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

              – Olivier Pons
              Oct 18 '18 at 7:04














            93












            93








            93







            You can also use rsync.



            sudo rsync -a /home/my_home/ /media/backup/my_home/


            From the rsync manpage:



             -a, --archive
            This is equivalent to -rlptgoD. It is a quick way of saying you want
            recursion and want to preserve almost everything (with -H being a notable
            omission). The only exception to the above equivalence is when
            --files-from is specified, in which case -r is not implied.

            Note that -a does not preserve hardlinks, because finding multiply-linked
            files is expensive. You must separately specify -H.


            See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686



            Note the trailing slashes (see manpage for details).






            share|improve this answer















            You can also use rsync.



            sudo rsync -a /home/my_home/ /media/backup/my_home/


            From the rsync manpage:



             -a, --archive
            This is equivalent to -rlptgoD. It is a quick way of saying you want
            recursion and want to preserve almost everything (with -H being a notable
            omission). The only exception to the above equivalence is when
            --files-from is specified, in which case -r is not implied.

            Note that -a does not preserve hardlinks, because finding multiply-linked
            files is expensive. You must separately specify -H.


            See this question for a comparison between cp and rsync: https://stackoverflow.com/q/6339287/406686



            Note the trailing slashes (see manpage for details).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:40









            Community

            1




            1










            answered Jul 20 '12 at 16:03









            studentstudent

            7,2551768130




            7,2551768130








            • 3





              +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

              – Izkata
              Jul 20 '12 at 17:59








            • 14





              NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

              – Perseids
              Sep 23 '14 at 6:44






            • 2





              Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

              – John Hamilton
              Mar 16 '18 at 11:59






            • 3





              @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

              – Olivier Pons
              Oct 18 '18 at 7:04














            • 3





              +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

              – Izkata
              Jul 20 '12 at 17:59








            • 14





              NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

              – Perseids
              Sep 23 '14 at 6:44






            • 2





              Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

              – John Hamilton
              Mar 16 '18 at 11:59






            • 3





              @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

              – Olivier Pons
              Oct 18 '18 at 7:04








            3




            3





            +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

            – Izkata
            Jul 20 '12 at 17:59







            +1, cp -p is nice, but I like rsync's output so much more in general that I've aliased pcp to time rsync --progress -ah. Stands for "progress copy" in my mind. And both accept -r, so it works well for general terminal usage - but unfortunately, not in combination with sudo as shown in this question/answer.

            – Izkata
            Jul 20 '12 at 17:59






            14




            14





            NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

            – Perseids
            Sep 23 '14 at 6:44





            NB: rsync -a, does not preserve extended attributes (-X) and no ACLs (-A) - the short description says archive mode; equals -rlptgoD (no -H,-A,-X). E.g. SELinux contexts will not be preserverd without -X. For many use cases this is fine, but if you make a backup of your system partition, missing -X might break quite a lot. As far as I know, cp -a really preserves all file attributes.

            – Perseids
            Sep 23 '14 at 6:44




            2




            2





            Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

            – John Hamilton
            Mar 16 '18 at 11:59





            Just tested this, while sudo cp -a preserves ownership and groups, sudo rsync -a changes them into root. So, @Perseids is correct.

            – John Hamilton
            Mar 16 '18 at 11:59




            3




            3





            @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

            – Olivier Pons
            Oct 18 '18 at 7:04





            @JohnHamilton Under Mint, this works perfectly... it only changes ownership and groups later on (I can't tell when). I've just copied my whole /home folder with rsync -aX /home /mnt/sdd/ and it worked like a charm.

            – Olivier Pons
            Oct 18 '18 at 7:04











            52














            cp -a


            Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).



            From man cp:



               -a, --archive
            same as -dR --preserve=all





            share|improve this answer




























              52














              cp -a


              Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).



              From man cp:



                 -a, --archive
              same as -dR --preserve=all





              share|improve this answer


























                52












                52








                52







                cp -a


                Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).



                From man cp:



                   -a, --archive
                same as -dR --preserve=all





                share|improve this answer













                cp -a


                Where -a is short for --archive — basically it copies a directory exactly as it is; the files retain all their attributes, and symlinks are not dereferenced (-d).



                From man cp:



                   -a, --archive
                same as -dR --preserve=all






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Oct 14 '15 at 0:38









                ZazZaz

                1,4591313




                1,4591313























                    20














                    I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).



                    PS: -R instead of -r is just habit from using ls -lR.






                    share|improve this answer



















                    • 1





                      cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                      – EM0
                      Mar 25 '15 at 12:27













                    • Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                      – geckon
                      Jul 8 '15 at 13:23






                    • 2





                      I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                      – StarNamer
                      Jul 8 '15 at 16:09


















                    20














                    I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).



                    PS: -R instead of -r is just habit from using ls -lR.






                    share|improve this answer



















                    • 1





                      cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                      – EM0
                      Mar 25 '15 at 12:27













                    • Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                      – geckon
                      Jul 8 '15 at 13:23






                    • 2





                      I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                      – StarNamer
                      Jul 8 '15 at 16:09
















                    20












                    20








                    20







                    I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).



                    PS: -R instead of -r is just habit from using ls -lR.






                    share|improve this answer













                    I use cp -pdRx which will -p preserve mode, ownership & timestamps, -d preserve links (so you get symlinks instead the file contents copied), -R do it recursively and -x stay on one file system (only really useful if you're copying / or something with an active mount point).



                    PS: -R instead of -r is just habit from using ls -lR.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jul 20 '12 at 16:17









                    StarNamerStarNamer

                    2,17511426




                    2,17511426








                    • 1





                      cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                      – EM0
                      Mar 25 '15 at 12:27













                    • Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                      – geckon
                      Jul 8 '15 at 13:23






                    • 2





                      I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                      – StarNamer
                      Jul 8 '15 at 16:09
















                    • 1





                      cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                      – EM0
                      Mar 25 '15 at 12:27













                    • Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                      – geckon
                      Jul 8 '15 at 13:23






                    • 2





                      I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                      – StarNamer
                      Jul 8 '15 at 16:09










                    1




                    1





                    cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                    – EM0
                    Mar 25 '15 at 12:27







                    cp -ax is a slightly shorter version of the same thing. This worked great - thank you!

                    – EM0
                    Mar 25 '15 at 12:27















                    Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                    – geckon
                    Jul 8 '15 at 13:23





                    Actually there is a difference between -r and -R. Check the man page (even the particular part too long to be quoted here).

                    – geckon
                    Jul 8 '15 at 13:23




                    2




                    2





                    I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                    – StarNamer
                    Jul 8 '15 at 16:09







                    I don't think there is (although there may once have been and may still be on some versions of Unix). See man7.org/linux/man-pages/man1/cp.1.html It simply says -R, -r, --recursive copy directories recursively.

                    – StarNamer
                    Jul 8 '15 at 16:09













                    14














                    cp has an option to preserve file ownership. From the manual page of cp:



                    -p    Cause cp to preserve the following attributes of each source file in the copy: modification
                    time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
                    Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
                    be preserved.





                    share|improve this answer




























                      14














                      cp has an option to preserve file ownership. From the manual page of cp:



                      -p    Cause cp to preserve the following attributes of each source file in the copy: modification
                      time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
                      Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
                      be preserved.





                      share|improve this answer


























                        14












                        14








                        14







                        cp has an option to preserve file ownership. From the manual page of cp:



                        -p    Cause cp to preserve the following attributes of each source file in the copy: modification
                        time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
                        Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
                        be preserved.





                        share|improve this answer













                        cp has an option to preserve file ownership. From the manual page of cp:



                        -p    Cause cp to preserve the following attributes of each source file in the copy: modification
                        time, access time, file flags, file mode, user ID, and group ID, as allowed by permissions.
                        Access Control Lists (ACLs) and Extended Attributes (EAs), including resource forks, will also
                        be preserved.






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 20 '12 at 15:51









                        MatteoMatteo

                        7,01043759




                        7,01043759























                            11














                            You can do something like this:



                            tar cf - my_home | (cd /media/backup; sudo tar xf - )


                            tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.






                            share|improve this answer




























                              11














                              You can do something like this:



                              tar cf - my_home | (cd /media/backup; sudo tar xf - )


                              tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.






                              share|improve this answer


























                                11












                                11








                                11







                                You can do something like this:



                                tar cf - my_home | (cd /media/backup; sudo tar xf - )


                                tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.






                                share|improve this answer













                                You can do something like this:



                                tar cf - my_home | (cd /media/backup; sudo tar xf - )


                                tar keeps permissions, ownership and directory structure intact, but converts everything into a stream of bytes. You run a "subshell" (the parenthesized commands) that change directory, and then get tar to reverse the conversion. A steam of bytes becomes directories and files with correct ownership and permissions.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jul 20 '12 at 15:43









                                Bruce EdigerBruce Ediger

                                36k670120




                                36k670120























                                    7














                                    The answer is simple: cp has a -p option that preserves permissions (here's a fish).



                                    But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (want to learn how to fish?).






                                    share|improve this answer






























                                      7














                                      The answer is simple: cp has a -p option that preserves permissions (here's a fish).



                                      But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (want to learn how to fish?).






                                      share|improve this answer




























                                        7












                                        7








                                        7







                                        The answer is simple: cp has a -p option that preserves permissions (here's a fish).



                                        But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (want to learn how to fish?).






                                        share|improve this answer















                                        The answer is simple: cp has a -p option that preserves permissions (here's a fish).



                                        But as Wojtek says in his comment, man cp (reading the fine manual) would be a good starting point (want to learn how to fish?).







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Oct 30 '16 at 16:51









                                        Jeff Schaller

                                        45.9k1165150




                                        45.9k1165150










                                        answered Jul 21 '12 at 2:26









                                        buckdeerbuckdeer

                                        791




                                        791























                                            0














                                            you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
                                            So, do your backup safely with the following command.



                                            cp -r --preserve=all /home/my_home /media/backup/my_home




                                            share








                                            New contributor



                                            Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                            Check out our Code of Conduct.
























                                              0














                                              you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
                                              So, do your backup safely with the following command.



                                              cp -r --preserve=all /home/my_home /media/backup/my_home




                                              share








                                              New contributor



                                              Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                              Check out our Code of Conduct.






















                                                0












                                                0








                                                0







                                                you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
                                                So, do your backup safely with the following command.



                                                cp -r --preserve=all /home/my_home /media/backup/my_home




                                                share








                                                New contributor



                                                Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.









                                                you can use preserve=all, then your copy will keep all attributes like owner, group and timestamp of your files.
                                                So, do your backup safely with the following command.



                                                cp -r --preserve=all /home/my_home /media/backup/my_home





                                                share








                                                New contributor



                                                Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.







                                                share


                                                share






                                                New contributor



                                                Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.








                                                answered 9 mins ago









                                                Majid ZarrinMajid Zarrin

                                                1




                                                1




                                                New contributor



                                                Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.




                                                New contributor




                                                Majid Zarrin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                                                Check out our Code of Conduct.
































                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f43605%2fhow-do-i-copy-a-folder-keeping-owners-and-permissions-intact%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                                    Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m