Copy files in Linux, avoid the copy if files do exist in destination
up vote
22
down vote
favorite
I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.
I use
cp -r /origin /destination
sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.
I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.
linux bash
migrated from stackoverflow.com Mar 11 '10 at 16:51
This question came from our site for professional and enthusiast programmers.
add a comment |
up vote
22
down vote
favorite
I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.
I use
cp -r /origin /destination
sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.
I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.
linux bash
migrated from stackoverflow.com Mar 11 '10 at 16:51
This question came from our site for professional and enthusiast programmers.
1
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08
add a comment |
up vote
22
down vote
favorite
up vote
22
down vote
favorite
I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.
I use
cp -r /origin /destination
sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.
I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.
linux bash
I need to copy a /home/user folder from one hard disk to another one. It has 100,000 files and around 10G size.
I use
cp -r /origin /destination
sometimes I get some errors due to broken links, permissions and so on. So I fix the error, and need to start again the copy.
I wonder how could I tell the command "cp", once it tries to copy again, not to copy files again if they exist in the destination folder.
linux bash
linux bash
edited Feb 11 '11 at 16:31
studiohack♦
11.3k1880113
11.3k1880113
asked Mar 11 '10 at 15:59
flow
1,986133964
1,986133964
migrated from stackoverflow.com Mar 11 '10 at 16:51
This question came from our site for professional and enthusiast programmers.
migrated from stackoverflow.com Mar 11 '10 at 16:51
This question came from our site for professional and enthusiast programmers.
1
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08
add a comment |
1
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08
1
1
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08
add a comment |
7 Answers
7
active
oldest
votes
up vote
17
down vote
accepted
cp -R -u -p /source /destination
The -u
(or --update
) flag does just this:
From the man page for cp:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
4
To literally only copy files that don't exist and not update existing ones,yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
add a comment |
up vote
20
down vote
Just use cp -n <source> <dest>
.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
add a comment |
up vote
16
down vote
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
add a comment |
up vote
4
down vote
Look up the "-u" option for the cp
command.
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
add a comment |
up vote
1
down vote
You should be copying as root to maintain permissions/ownership
# cp -au
Also look at rsync
add a comment |
up vote
1
down vote
All above answers are correct but if you are doing this recursively then
you should do:
cp -rn SOURCE_PATH DESTINATION_PATH
add a comment |
up vote
0
down vote
POSIX solution
Other answers use -u
or -n
options of cp
. Neither of these is required by POSIX; nor is rsync
from yet another answer; nor is yes
used in one of the comments.
Still we can reproduce yes n
with a while
loop. This leads to the following POSIX solution:
while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
accepted
cp -R -u -p /source /destination
The -u
(or --update
) flag does just this:
From the man page for cp:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
4
To literally only copy files that don't exist and not update existing ones,yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
add a comment |
up vote
17
down vote
accepted
cp -R -u -p /source /destination
The -u
(or --update
) flag does just this:
From the man page for cp:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
4
To literally only copy files that don't exist and not update existing ones,yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
add a comment |
up vote
17
down vote
accepted
up vote
17
down vote
accepted
cp -R -u -p /source /destination
The -u
(or --update
) flag does just this:
From the man page for cp:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
cp -R -u -p /source /destination
The -u
(or --update
) flag does just this:
From the man page for cp:
-u, --update
copy only when the SOURCE file is newer than the destination file or when the destination file is missing
edited Nov 23 at 0:39
ifconfig
1731215
1731215
answered Mar 11 '10 at 16:02
user31894
2,105129
2,105129
4
To literally only copy files that don't exist and not update existing ones,yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
add a comment |
4
To literally only copy files that don't exist and not update existing ones,yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
4
4
To literally only copy files that don't exist and not update existing ones,
yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
To literally only copy files that don't exist and not update existing ones,
yes n | cp -i /source/* /destination 2>/dev/null
– sventechie
Nov 21 '13 at 20:14
3
3
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
-u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing -p same as --preserve=mode,ownership,timestamps --preserve[=ATTR_LIST] preserve the specified attributes (default: mode,ownership,time‐ stamps), if possible additional attributes: context, links, xattr, all
– Covich
Feb 16 '16 at 15:29
add a comment |
up vote
20
down vote
Just use cp -n <source> <dest>
.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
add a comment |
up vote
20
down vote
Just use cp -n <source> <dest>
.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
add a comment |
up vote
20
down vote
up vote
20
down vote
Just use cp -n <source> <dest>
.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
Just use cp -n <source> <dest>
.
From man page:
-n, --no-clobber
do NOT overwrite an existing file (overrides a previous -i option)
edited Jan 25 '17 at 15:59
Adalee
312410
312410
answered Jan 25 '17 at 13:45
Balmipour
32926
32926
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
add a comment |
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
4
4
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
This is the only true answer to the question.
– sebix
Mar 21 '17 at 18:19
add a comment |
up vote
16
down vote
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
add a comment |
up vote
16
down vote
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
add a comment |
up vote
16
down vote
up vote
16
down vote
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
rsync -aq /src /dest
Apart from only copying newer files, it will even only copy the newer parts of files if the file has changed. It's intended for copying over network links where you want to minimise the amount of data - but it also works great locally.
edited Apr 24 '16 at 21:17
answered Mar 11 '10 at 16:12
Martin Beckett
4,75311925
4,75311925
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
add a comment |
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
I can't add `` formatting to your post because it's only two characters. How would you feel about editing this and adding `` characters around your command?
– culix
Apr 24 '16 at 18:02
add a comment |
up vote
4
down vote
Look up the "-u" option for the cp
command.
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
add a comment |
up vote
4
down vote
Look up the "-u" option for the cp
command.
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
add a comment |
up vote
4
down vote
up vote
4
down vote
Look up the "-u" option for the cp
command.
Look up the "-u" option for the cp
command.
answered Mar 11 '10 at 16:01
Pointy
5931520
5931520
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
add a comment |
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
2
2
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
Disagree that using -u is a good idea. -u copies only when source is newer or destination is missing. Original poster's issue was caused by file copy breaking for whatever reason. OP would therefore have half-written file that wasn't updated when re-running the copy. That file might be important to someone! rsync solves this problem.
– hazymat
Dec 21 '13 at 21:47
add a comment |
up vote
1
down vote
You should be copying as root to maintain permissions/ownership
# cp -au
Also look at rsync
add a comment |
up vote
1
down vote
You should be copying as root to maintain permissions/ownership
# cp -au
Also look at rsync
add a comment |
up vote
1
down vote
up vote
1
down vote
You should be copying as root to maintain permissions/ownership
# cp -au
Also look at rsync
You should be copying as root to maintain permissions/ownership
# cp -au
Also look at rsync
answered Mar 11 '10 at 16:09
pixelbeat
85066
85066
add a comment |
add a comment |
up vote
1
down vote
All above answers are correct but if you are doing this recursively then
you should do:
cp -rn SOURCE_PATH DESTINATION_PATH
add a comment |
up vote
1
down vote
All above answers are correct but if you are doing this recursively then
you should do:
cp -rn SOURCE_PATH DESTINATION_PATH
add a comment |
up vote
1
down vote
up vote
1
down vote
All above answers are correct but if you are doing this recursively then
you should do:
cp -rn SOURCE_PATH DESTINATION_PATH
All above answers are correct but if you are doing this recursively then
you should do:
cp -rn SOURCE_PATH DESTINATION_PATH
answered Jul 8 at 4:58
nanoseconds
1236
1236
add a comment |
add a comment |
up vote
0
down vote
POSIX solution
Other answers use -u
or -n
options of cp
. Neither of these is required by POSIX; nor is rsync
from yet another answer; nor is yes
used in one of the comments.
Still we can reproduce yes n
with a while
loop. This leads to the following POSIX solution:
while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
add a comment |
up vote
0
down vote
POSIX solution
Other answers use -u
or -n
options of cp
. Neither of these is required by POSIX; nor is rsync
from yet another answer; nor is yes
used in one of the comments.
Still we can reproduce yes n
with a while
loop. This leads to the following POSIX solution:
while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
add a comment |
up vote
0
down vote
up vote
0
down vote
POSIX solution
Other answers use -u
or -n
options of cp
. Neither of these is required by POSIX; nor is rsync
from yet another answer; nor is yes
used in one of the comments.
Still we can reproduce yes n
with a while
loop. This leads to the following POSIX solution:
while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
POSIX solution
Other answers use -u
or -n
options of cp
. Neither of these is required by POSIX; nor is rsync
from yet another answer; nor is yes
used in one of the comments.
Still we can reproduce yes n
with a while
loop. This leads to the following POSIX solution:
while true; do echo n; done | cp -Ri /origin /destination 2>/dev/null
answered Nov 23 at 6:33
Kamil Maciorowski
22.8k155072
22.8k155072
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f118781%2fcopy-files-in-linux-avoid-the-copy-if-files-do-exist-in-destination%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
1
Don't us cp. Use rsync instead.
– William Pursell
Mar 11 '10 at 16:08