How do I copy a folder keeping owners and permissions intact?
Clash Royale CLAN TAG#URR8PPP
up vote
322
down vote
favorite
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
add a comment |
up vote
322
down vote
favorite
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
20
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
4
@StijnGeukens It's not even that google is faster, it's that evenman
to me is cryptic. In this case,man
at-r
sayssame 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
add a comment |
up vote
322
down vote
favorite
up vote
322
down vote
favorite
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
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
permissions cp
edited Jul 16 '14 at 20:58
Braiam
23k1975137
23k1975137
asked Jul 20 '12 at 15:27
Psachnodaimonia
1,725298
1,725298
20
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
4
@StijnGeukens It's not even that google is faster, it's that evenman
to me is cryptic. In this case,man
at-r
sayssame 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
add a comment |
20
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
4
@StijnGeukens It's not even that google is faster, it's that evenman
to me is cryptic. In this case,man
at-r
sayssame 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
20
20
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
4
4
@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
add a comment |
7 Answers
7
active
oldest
votes
up vote
389
down vote
accepted
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
93
Much better to usecp -a
. This also includes the-r
ecursive 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 usingcp
for a backup, don't use anything other thancp -a
.
– Wildcard
Oct 14 '15 at 3:54
18
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
3
cp -a
doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to usecp -pR
. On my current OS X system though (10.10.15),cp -a
seems to be honored.
– dubiousjim
Sep 30 '17 at 16:30
add a comment |
up vote
85
down vote
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).
3
+1,cp -p
is nice, but I likersync
's output so much more in general that I've aliasedpcp
totime 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 withsudo
as shown in this question/answer.
– Izkata
Jul 20 '12 at 17:59
12
NB:rsync -a
, does not preserve extended attributes (-X
) and no ACLs (-A
) - the short description saysarchive 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
1
Just tested this, whilesudo cp -a
preserves ownership and groups,sudo rsync -a
changes them into root. So, @Perseids is correct.
– John Hamilton
Mar 16 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 withrsync -aX /home /mnt/sdd/
and it worked like a charm.
– Olivier Pons
Oct 18 at 7:04
add a comment |
up vote
40
down vote
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
add a comment |
up vote
19
down vote
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
.
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
1
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
add a comment |
up vote
14
down vote
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.
add a comment |
up vote
11
down vote
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.
add a comment |
up vote
7
down vote
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?).
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
389
down vote
accepted
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
93
Much better to usecp -a
. This also includes the-r
ecursive 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 usingcp
for a backup, don't use anything other thancp -a
.
– Wildcard
Oct 14 '15 at 3:54
18
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
3
cp -a
doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to usecp -pR
. On my current OS X system though (10.10.15),cp -a
seems to be honored.
– dubiousjim
Sep 30 '17 at 16:30
add a comment |
up vote
389
down vote
accepted
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
93
Much better to usecp -a
. This also includes the-r
ecursive 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 usingcp
for a backup, don't use anything other thancp -a
.
– Wildcard
Oct 14 '15 at 3:54
18
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
3
cp -a
doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to usecp -pR
. On my current OS X system though (10.10.15),cp -a
seems to be honored.
– dubiousjim
Sep 30 '17 at 16:30
add a comment |
up vote
389
down vote
accepted
up vote
389
down vote
accepted
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
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
answered Jul 20 '12 at 15:51
guido
5,44811427
5,44811427
93
Much better to usecp -a
. This also includes the-r
ecursive 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 usingcp
for a backup, don't use anything other thancp -a
.
– Wildcard
Oct 14 '15 at 3:54
18
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
3
cp -a
doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to usecp -pR
. On my current OS X system though (10.10.15),cp -a
seems to be honored.
– dubiousjim
Sep 30 '17 at 16:30
add a comment |
93
Much better to usecp -a
. This also includes the-r
ecursive 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 usingcp
for a backup, don't use anything other thancp -a
.
– Wildcard
Oct 14 '15 at 3:54
18
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
3
cp -a
doesn't work on some systems: e.g. OS X, where (in some versions at least) one needed to usecp -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
93
Much better to use
cp -a
. This also includes the -r
ecursive 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 -r
ecursive 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
18
18
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
3
3
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
add a comment |
up vote
85
down vote
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).
3
+1,cp -p
is nice, but I likersync
's output so much more in general that I've aliasedpcp
totime 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 withsudo
as shown in this question/answer.
– Izkata
Jul 20 '12 at 17:59
12
NB:rsync -a
, does not preserve extended attributes (-X
) and no ACLs (-A
) - the short description saysarchive 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
1
Just tested this, whilesudo cp -a
preserves ownership and groups,sudo rsync -a
changes them into root. So, @Perseids is correct.
– John Hamilton
Mar 16 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 withrsync -aX /home /mnt/sdd/
and it worked like a charm.
– Olivier Pons
Oct 18 at 7:04
add a comment |
up vote
85
down vote
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).
3
+1,cp -p
is nice, but I likersync
's output so much more in general that I've aliasedpcp
totime 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 withsudo
as shown in this question/answer.
– Izkata
Jul 20 '12 at 17:59
12
NB:rsync -a
, does not preserve extended attributes (-X
) and no ACLs (-A
) - the short description saysarchive 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
1
Just tested this, whilesudo cp -a
preserves ownership and groups,sudo rsync -a
changes them into root. So, @Perseids is correct.
– John Hamilton
Mar 16 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 withrsync -aX /home /mnt/sdd/
and it worked like a charm.
– Olivier Pons
Oct 18 at 7:04
add a comment |
up vote
85
down vote
up vote
85
down vote
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).
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).
edited May 23 '17 at 12:40
Community♦
1
1
answered Jul 20 '12 at 16:03
student
6,9331663120
6,9331663120
3
+1,cp -p
is nice, but I likersync
's output so much more in general that I've aliasedpcp
totime 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 withsudo
as shown in this question/answer.
– Izkata
Jul 20 '12 at 17:59
12
NB:rsync -a
, does not preserve extended attributes (-X
) and no ACLs (-A
) - the short description saysarchive 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
1
Just tested this, whilesudo cp -a
preserves ownership and groups,sudo rsync -a
changes them into root. So, @Perseids is correct.
– John Hamilton
Mar 16 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 withrsync -aX /home /mnt/sdd/
and it worked like a charm.
– Olivier Pons
Oct 18 at 7:04
add a comment |
3
+1,cp -p
is nice, but I likersync
's output so much more in general that I've aliasedpcp
totime 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 withsudo
as shown in this question/answer.
– Izkata
Jul 20 '12 at 17:59
12
NB:rsync -a
, does not preserve extended attributes (-X
) and no ACLs (-A
) - the short description saysarchive 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
1
Just tested this, whilesudo cp -a
preserves ownership and groups,sudo rsync -a
changes them into root. So, @Perseids is correct.
– John Hamilton
Mar 16 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 withrsync -aX /home /mnt/sdd/
and it worked like a charm.
– Olivier Pons
Oct 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
12
12
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
1
1
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 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 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 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 at 7:04
add a comment |
up vote
40
down vote
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
add a comment |
up vote
40
down vote
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
add a comment |
up vote
40
down vote
up vote
40
down vote
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
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
answered Oct 14 '15 at 0:38
Zaz
1,2351110
1,2351110
add a comment |
add a comment |
up vote
19
down vote
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
.
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
1
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
add a comment |
up vote
19
down vote
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
.
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
1
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
add a comment |
up vote
19
down vote
up vote
19
down vote
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
.
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
.
answered Jul 20 '12 at 16:17
StarNamer
2,04511324
2,04511324
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
1
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
add a comment |
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
1
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
1
1
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
add a comment |
up vote
14
down vote
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.
add a comment |
up vote
14
down vote
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.
add a comment |
up vote
14
down vote
up vote
14
down vote
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.
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.
answered Jul 20 '12 at 15:51
Matteo
6,74543657
6,74543657
add a comment |
add a comment |
up vote
11
down vote
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.
add a comment |
up vote
11
down vote
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.
add a comment |
up vote
11
down vote
up vote
11
down vote
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.
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.
answered Jul 20 '12 at 15:43
Bruce Ediger
34.6k566119
34.6k566119
add a comment |
add a comment |
up vote
7
down vote
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?).
add a comment |
up vote
7
down vote
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?).
add a comment |
up vote
7
down vote
up vote
7
down vote
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?).
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?).
edited Oct 30 '16 at 16:51
Jeff Schaller
37.5k1052121
37.5k1052121
answered Jul 21 '12 at 2:26
buckdeer
791
791
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
20
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
4
@StijnGeukens It's not even that google is faster, it's that even
man
to me is cryptic. In this case,man
at-r
sayssame 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