How to determine if cp -u actually copied the file?












5














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 echoes 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?










share|improve this question
























  • if cp -uv source destination; then echo copied; else echo not copied; fididn'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
















5














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 echoes 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?










share|improve this question
























  • if cp -uv source destination; then echo copied; else echo not copied; fididn'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














5












5








5


1





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 echoes 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?










share|improve this question















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 echoes 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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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; fididn'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; fididn'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; fididn't work ?
– Archemar
Oct 9 '14 at 7:52




if cp -uv source destination; then echo copied; else echo not copied; fididn'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










1 Answer
1






active

oldest

votes


















7














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





share|improve this answer



















  • 1




    I'm kinda ashamed that I didn't thought of this. Thank you!
    – Zeeker
    Oct 9 '14 at 7:46











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%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









7














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





share|improve this answer



















  • 1




    I'm kinda ashamed that I didn't thought of this. Thank you!
    – Zeeker
    Oct 9 '14 at 7:46
















7














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





share|improve this answer



















  • 1




    I'm kinda ashamed that I didn't thought of this. Thank you!
    – Zeeker
    Oct 9 '14 at 7:46














7












7








7






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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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














  • 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


















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.





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.




draft saved


draft discarded














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





















































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

Morgemoulin

Scott Moir

Souastre