Can I get the backup option of cp and mv to work with directories?












2















--backup[=CONTROL]
make a backup of each existing destination file


The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this work with directories as well? I want the destination directory, if it exists, to be backed up in the same way that a file would be backed up.










share|improve this question
















bumped to the homepage by Community 20 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

    – Ian D. Allen
    Oct 14 '13 at 15:32











  • @IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

    – phunehehe
    Oct 15 '13 at 3:52
















2















--backup[=CONTROL]
make a backup of each existing destination file


The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this work with directories as well? I want the destination directory, if it exists, to be backed up in the same way that a file would be backed up.










share|improve this question
















bumped to the homepage by Community 20 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

    – Ian D. Allen
    Oct 14 '13 at 15:32











  • @IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

    – phunehehe
    Oct 15 '13 at 3:52














2












2








2








--backup[=CONTROL]
make a backup of each existing destination file


The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this work with directories as well? I want the destination directory, if it exists, to be backed up in the same way that a file would be backed up.










share|improve this question
















--backup[=CONTROL]
make a backup of each existing destination file


The man page does say that it works on file. It doesn't seem to have any effect on directories. Is there anyway to make this work with directories as well? I want the destination directory, if it exists, to be backed up in the same way that a file would be backed up.







shell-script backup rename cp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 15 '13 at 3:54







phunehehe

















asked Feb 22 '12 at 13:58









phunehehephunehehe

12.3k1882141




12.3k1882141





bumped to the homepage by Community 20 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 20 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

    – Ian D. Allen
    Oct 14 '13 at 15:32











  • @IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

    – phunehehe
    Oct 15 '13 at 3:52



















  • What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

    – Ian D. Allen
    Oct 14 '13 at 15:32











  • @IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

    – phunehehe
    Oct 15 '13 at 3:52

















What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

– Ian D. Allen
Oct 14 '13 at 15:32





What does "work" mean for a directory? Are you trying to back up the information about the directory itself, or are you trying to organize the backed-up files into a separate back-up directory (instead of having each file backed up in the directory in which it is found)? Either way, "cp" will not do this for you; you need to write a script.

– Ian D. Allen
Oct 14 '13 at 15:32













@IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

– phunehehe
Oct 15 '13 at 3:52





@IDAllen Yeah I have come to that same conclusion. Let me make the script an answer instead.

– phunehehe
Oct 15 '13 at 3:52










1 Answer
1






active

oldest

votes


















0














I have a feeling that there isn't, so I wrote this script which emulates the functionality:



#!/bin/bash

if [ -e "$target" ]
then
backup="$target.bak"
if [[ -e "$backup" ]]
then
count=0
while [[ -e "$backup.$count" ]]; do let "count += 1"; done
backup="$backup.$count"
fi
mv "$target" "$backup"
echo "backup file $backup created."
fi

# Normal cp or mv follows





share|improve this answer
























  • I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

    – Ian D. Allen
    Oct 17 '13 at 1:29











  • @IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

    – phunehehe
    Oct 17 '13 at 5:03











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%2f32397%2fcan-i-get-the-backup-option-of-cp-and-mv-to-work-with-directories%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









0














I have a feeling that there isn't, so I wrote this script which emulates the functionality:



#!/bin/bash

if [ -e "$target" ]
then
backup="$target.bak"
if [[ -e "$backup" ]]
then
count=0
while [[ -e "$backup.$count" ]]; do let "count += 1"; done
backup="$backup.$count"
fi
mv "$target" "$backup"
echo "backup file $backup created."
fi

# Normal cp or mv follows





share|improve this answer
























  • I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

    – Ian D. Allen
    Oct 17 '13 at 1:29











  • @IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

    – phunehehe
    Oct 17 '13 at 5:03
















0














I have a feeling that there isn't, so I wrote this script which emulates the functionality:



#!/bin/bash

if [ -e "$target" ]
then
backup="$target.bak"
if [[ -e "$backup" ]]
then
count=0
while [[ -e "$backup.$count" ]]; do let "count += 1"; done
backup="$backup.$count"
fi
mv "$target" "$backup"
echo "backup file $backup created."
fi

# Normal cp or mv follows





share|improve this answer
























  • I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

    – Ian D. Allen
    Oct 17 '13 at 1:29











  • @IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

    – phunehehe
    Oct 17 '13 at 5:03














0












0








0







I have a feeling that there isn't, so I wrote this script which emulates the functionality:



#!/bin/bash

if [ -e "$target" ]
then
backup="$target.bak"
if [[ -e "$backup" ]]
then
count=0
while [[ -e "$backup.$count" ]]; do let "count += 1"; done
backup="$backup.$count"
fi
mv "$target" "$backup"
echo "backup file $backup created."
fi

# Normal cp or mv follows





share|improve this answer













I have a feeling that there isn't, so I wrote this script which emulates the functionality:



#!/bin/bash

if [ -e "$target" ]
then
backup="$target.bak"
if [[ -e "$backup" ]]
then
count=0
while [[ -e "$backup.$count" ]]; do let "count += 1"; done
backup="$backup.$count"
fi
mv "$target" "$backup"
echo "backup file $backup created."
fi

# Normal cp or mv follows






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 15 '13 at 3:55









phunehehephunehehe

12.3k1882141




12.3k1882141













  • I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

    – Ian D. Allen
    Oct 17 '13 at 1:29











  • @IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

    – phunehehe
    Oct 17 '13 at 5:03



















  • I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

    – Ian D. Allen
    Oct 17 '13 at 1:29











  • @IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

    – phunehehe
    Oct 17 '13 at 5:03

















I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

– Ian D. Allen
Oct 17 '13 at 1:29





I think you've written an archive and delete script, not a back-up script, since your script deletes the original directory (by renaming it). Your script comment about "file created" will mislead if it isn't a file. Depending on how you call your script (i.e. if you use it with find), you may find that it archives all the files under a directory before it archives the directory itself, or find will complain that you've just moved away a directory that it was about to walk - this may not be what you want.

– Ian D. Allen
Oct 17 '13 at 1:29













@IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

– phunehehe
Oct 17 '13 at 5:03





@IDAllen You misunderstood, this script is to be used in place of cp or mv, to make the --backup behavior applicable to directories.

– phunehehe
Oct 17 '13 at 5:03


















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%2f32397%2fcan-i-get-the-backup-option-of-cp-and-mv-to-work-with-directories%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

Accessing regular linux commands in Huawei's Dopra Linux

Can't connect RFCOMM socket: Host is down

Kernel panic - not syncing: Fatal Exception in Interrupt