How to determine if cp -u actually copied the file?
I'm looking for a concise way to check if a file was copied using cp -u
.
I have a little shell script which updates quite a number of files. At the moment the script echo
es a little message for each file which gets copied.
Now I want to change the script in such a way that the echo
only gets executed when the file actually was updated. I've checked if cp -u
returned an error code if it didn't copy the file, but this isn't the case.
Is there a better way than manually comparing the timestamps of the files?
cp timestamps
|
show 3 more comments
I'm looking for a concise way to check if a file was copied using cp -u
.
I have a little shell script which updates quite a number of files. At the moment the script echo
es a little message for each file which gets copied.
Now I want to change the script in such a way that the echo
only gets executed when the file actually was updated. I've checked if cp -u
returned an error code if it didn't copy the file, but this isn't the case.
Is there a better way than manually comparing the timestamps of the files?
cp timestamps
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?
– Archemar
Oct 9 '14 at 7:52
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
1
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
1
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59
|
show 3 more comments
I'm looking for a concise way to check if a file was copied using cp -u
.
I have a little shell script which updates quite a number of files. At the moment the script echo
es a little message for each file which gets copied.
Now I want to change the script in such a way that the echo
only gets executed when the file actually was updated. I've checked if cp -u
returned an error code if it didn't copy the file, but this isn't the case.
Is there a better way than manually comparing the timestamps of the files?
cp timestamps
I'm looking for a concise way to check if a file was copied using cp -u
.
I have a little shell script which updates quite a number of files. At the moment the script echo
es a little message for each file which gets copied.
Now I want to change the script in such a way that the echo
only gets executed when the file actually was updated. I've checked if cp -u
returned an error code if it didn't copy the file, but this isn't the case.
Is there a better way than manually comparing the timestamps of the files?
cp timestamps
cp timestamps
edited Dec 20 '18 at 0:13
Rui F Ribeiro
39k1479130
39k1479130
asked Oct 9 '14 at 7:34
Zeeker
1284
1284
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?
– Archemar
Oct 9 '14 at 7:52
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
1
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
1
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59
|
show 3 more comments
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?
– Archemar
Oct 9 '14 at 7:52
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
1
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
1
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?– Archemar
Oct 9 '14 at 7:52
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?– Archemar
Oct 9 '14 at 7:52
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
1
1
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
1
1
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59
|
show 3 more comments
1 Answer
1
active
oldest
votes
if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi
Update
Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".
if [[ "$(cp -uv source destination)" =~ -> ]]; then echo copied; else echo not copied; fi
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
add a comment |
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
});
}
});
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%2f160163%2fhow-to-determine-if-cp-u-actually-copied-the-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi
Update
Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".
if [[ "$(cp -uv source destination)" =~ -> ]]; then echo copied; else echo not copied; fi
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
add a comment |
if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi
Update
Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".
if [[ "$(cp -uv source destination)" =~ -> ]]; then echo copied; else echo not copied; fi
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
add a comment |
if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi
Update
Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".
if [[ "$(cp -uv source destination)" =~ -> ]]; then echo copied; else echo not copied; fi
if [ "$(cp -uv source destination)" != "" ]; then echo copied; else echo not copied; fi
Update
Match "->" in cp's verbose output. It only occurs if it could be successfully copied and if your filenames do not contain "->".
if [[ "$(cp -uv source destination)" =~ -> ]]; then echo copied; else echo not copied; fi
edited Oct 9 '14 at 7:54
answered Oct 9 '14 at 7:38
Cyrus
7,2612835
7,2612835
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
add a comment |
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
1
1
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
I'm kinda ashamed that I didn't thought of this. Thank you!
– Zeeker
Oct 9 '14 at 7:46
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%2f160163%2fhow-to-determine-if-cp-u-actually-copied-the-file%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
if cp -uv source destination; then echo copied; else echo not copied; fi
didn't work ?– Archemar
Oct 9 '14 at 7:52
How come it didn't return a return code? jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf.copy jpkroehling@cupuacu ~ $ echo $? 0 jpkroehling@cupuacu ~ $ cp -u nginx.conf nginx.conf cp: ‘nginx.conf’ and ‘nginx.conf’ are the same file jpkroehling@cupuacu ~ $ echo $? 1
– jpkrohling
Oct 9 '14 at 10:42
Ok, I give up in trying to get it printing nicely: pastebin.com/Bz4n8MVW
– jpkrohling
Oct 9 '14 at 10:43
1
It always returns 0, even if the file wasn't copied. At least on my system it does.
– Zeeker
Oct 9 '14 at 10:43
1
@jpkrohling: in second case you tried to copied nginx.conf to nginx.conf and not to nginx.conf.copy.
– Cyrus
Oct 9 '14 at 14:59