Backup all contents of current directory to a subdirectory inside the current directory, which will be created if not exists

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1














Right now I have following command to copy all contents of the current directory to sub-directory, provided if the subdirectory is created in advance:



cp -p !($PWD/bakfiles2) bakfiles2/


But I have to some times visit those folders which I have never visited before, so sub-directory "bakfiles2" may not exist there, can I somehow create that backup directory with current timestamp(as to avoid conflictions with any existing directory), on the fly when with single copy command or bash script ?



It would be great if the script can ignore any sub-directory starting with a particular pattern which could then be reserved for backup directory names like _bak_* (Note: * means any number of any characters).










share|improve this question























  • I don’t understand; is your question just “How do I create a directory?”
    – Scott
    Jul 28 '17 at 7:31










  • How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
    – VST
    Jul 28 '17 at 7:33







  • 1




    How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
    – Scott
    Jul 28 '17 at 7:39















1














Right now I have following command to copy all contents of the current directory to sub-directory, provided if the subdirectory is created in advance:



cp -p !($PWD/bakfiles2) bakfiles2/


But I have to some times visit those folders which I have never visited before, so sub-directory "bakfiles2" may not exist there, can I somehow create that backup directory with current timestamp(as to avoid conflictions with any existing directory), on the fly when with single copy command or bash script ?



It would be great if the script can ignore any sub-directory starting with a particular pattern which could then be reserved for backup directory names like _bak_* (Note: * means any number of any characters).










share|improve this question























  • I don’t understand; is your question just “How do I create a directory?”
    – Scott
    Jul 28 '17 at 7:31










  • How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
    – VST
    Jul 28 '17 at 7:33







  • 1




    How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
    – Scott
    Jul 28 '17 at 7:39













1












1








1


2





Right now I have following command to copy all contents of the current directory to sub-directory, provided if the subdirectory is created in advance:



cp -p !($PWD/bakfiles2) bakfiles2/


But I have to some times visit those folders which I have never visited before, so sub-directory "bakfiles2" may not exist there, can I somehow create that backup directory with current timestamp(as to avoid conflictions with any existing directory), on the fly when with single copy command or bash script ?



It would be great if the script can ignore any sub-directory starting with a particular pattern which could then be reserved for backup directory names like _bak_* (Note: * means any number of any characters).










share|improve this question















Right now I have following command to copy all contents of the current directory to sub-directory, provided if the subdirectory is created in advance:



cp -p !($PWD/bakfiles2) bakfiles2/


But I have to some times visit those folders which I have never visited before, so sub-directory "bakfiles2" may not exist there, can I somehow create that backup directory with current timestamp(as to avoid conflictions with any existing directory), on the fly when with single copy command or bash script ?



It would be great if the script can ignore any sub-directory starting with a particular pattern which could then be reserved for backup directory names like _bak_* (Note: * means any number of any characters).







command-line file-copy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 20 '18 at 7:09









Rui F Ribeiro

39k1479130




39k1479130










asked Jul 28 '17 at 7:22









VST

83




83











  • I don’t understand; is your question just “How do I create a directory?”
    – Scott
    Jul 28 '17 at 7:31










  • How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
    – VST
    Jul 28 '17 at 7:33







  • 1




    How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
    – Scott
    Jul 28 '17 at 7:39
















  • I don’t understand; is your question just “How do I create a directory?”
    – Scott
    Jul 28 '17 at 7:31










  • How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
    – VST
    Jul 28 '17 at 7:33







  • 1




    How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
    – Scott
    Jul 28 '17 at 7:39















I don’t understand; is your question just “How do I create a directory?”
– Scott
Jul 28 '17 at 7:31




I don’t understand; is your question just “How do I create a directory?”
– Scott
Jul 28 '17 at 7:31












How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
– VST
Jul 28 '17 at 7:33





How do I create sub-directory inside my current directory on the fly when I am copying the contents of current directory to that sub-directory ?
– VST
Jul 28 '17 at 7:33





1




1




How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
– Scott
Jul 28 '17 at 7:39




How do I eat a chicken sandwich while standing on one foot in an airport? The same way I eat any sandwich, anywhere, standing or sitting down. You seem to know of the concept of a bash script, so I don’t understand what you’re asking.
– Scott
Jul 28 '17 at 7:39










2 Answers
2






active

oldest

votes


















0














cp command doesn't have an option to create destination directory if doesn't exist while coping, but you can achieve with scripting.



or simply use rsync command which can create destination directory if doesn't exist only on last level.



rsync -rv --exclude='_bak_*/' /path/in/source/ /path/to/destination


  • note that leading / in /path/in/source/ will prevent coping source directory itself and adding --exclude option to don't sync directories with matched name.





share|improve this answer






























    0














    You can get a current timestamp with the date command



    date +'%Y%m%d_%H%M%S'


    You can copy the contents of a directory only to a directory that already exists, unless you're using a recursive copy. However, you can't use a recursive copy unless you want the previous backup directories also copied to the new backup directory.



    So this has to be done in a couple of steps



    backup_dir="_bak_$(date +'%Y%m%d_%H%M%S')"
    mkdir "$backup_dir"
    cp -p !(_bak_*) "$backup_dir/"


    Bear in mind that as written this will not copy dot files (files beginning with a . character), since these are usually treated specially by the shell. (You might want to consider naming your backup directory .bak_* instead of _bak_* to benefit from this feature. You can then cp -p * "$backup_dir/")



    There's no reason why this couldn't created as a little function or script, so it's then still only one command.






    share|improve this answer




















    • Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
      – VST
      Jul 28 '17 at 11:26










    • @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
      – roaima
      Jul 28 '17 at 11:49











    • Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
      – VST
      Jul 28 '17 at 11:51










    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%2f382313%2fbackup-all-contents-of-current-directory-to-a-subdirectory-inside-the-current-di%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    cp command doesn't have an option to create destination directory if doesn't exist while coping, but you can achieve with scripting.



    or simply use rsync command which can create destination directory if doesn't exist only on last level.



    rsync -rv --exclude='_bak_*/' /path/in/source/ /path/to/destination


    • note that leading / in /path/in/source/ will prevent coping source directory itself and adding --exclude option to don't sync directories with matched name.





    share|improve this answer



























      0














      cp command doesn't have an option to create destination directory if doesn't exist while coping, but you can achieve with scripting.



      or simply use rsync command which can create destination directory if doesn't exist only on last level.



      rsync -rv --exclude='_bak_*/' /path/in/source/ /path/to/destination


      • note that leading / in /path/in/source/ will prevent coping source directory itself and adding --exclude option to don't sync directories with matched name.





      share|improve this answer

























        0












        0








        0






        cp command doesn't have an option to create destination directory if doesn't exist while coping, but you can achieve with scripting.



        or simply use rsync command which can create destination directory if doesn't exist only on last level.



        rsync -rv --exclude='_bak_*/' /path/in/source/ /path/to/destination


        • note that leading / in /path/in/source/ will prevent coping source directory itself and adding --exclude option to don't sync directories with matched name.





        share|improve this answer














        cp command doesn't have an option to create destination directory if doesn't exist while coping, but you can achieve with scripting.



        or simply use rsync command which can create destination directory if doesn't exist only on last level.



        rsync -rv --exclude='_bak_*/' /path/in/source/ /path/to/destination


        • note that leading / in /path/in/source/ will prevent coping source directory itself and adding --exclude option to don't sync directories with matched name.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jul 28 '17 at 16:39

























        answered Jul 28 '17 at 7:41









        αғsнιη

        16.5k102865




        16.5k102865























            0














            You can get a current timestamp with the date command



            date +'%Y%m%d_%H%M%S'


            You can copy the contents of a directory only to a directory that already exists, unless you're using a recursive copy. However, you can't use a recursive copy unless you want the previous backup directories also copied to the new backup directory.



            So this has to be done in a couple of steps



            backup_dir="_bak_$(date +'%Y%m%d_%H%M%S')"
            mkdir "$backup_dir"
            cp -p !(_bak_*) "$backup_dir/"


            Bear in mind that as written this will not copy dot files (files beginning with a . character), since these are usually treated specially by the shell. (You might want to consider naming your backup directory .bak_* instead of _bak_* to benefit from this feature. You can then cp -p * "$backup_dir/")



            There's no reason why this couldn't created as a little function or script, so it's then still only one command.






            share|improve this answer




















            • Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
              – VST
              Jul 28 '17 at 11:26










            • @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
              – roaima
              Jul 28 '17 at 11:49











            • Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
              – VST
              Jul 28 '17 at 11:51















            0














            You can get a current timestamp with the date command



            date +'%Y%m%d_%H%M%S'


            You can copy the contents of a directory only to a directory that already exists, unless you're using a recursive copy. However, you can't use a recursive copy unless you want the previous backup directories also copied to the new backup directory.



            So this has to be done in a couple of steps



            backup_dir="_bak_$(date +'%Y%m%d_%H%M%S')"
            mkdir "$backup_dir"
            cp -p !(_bak_*) "$backup_dir/"


            Bear in mind that as written this will not copy dot files (files beginning with a . character), since these are usually treated specially by the shell. (You might want to consider naming your backup directory .bak_* instead of _bak_* to benefit from this feature. You can then cp -p * "$backup_dir/")



            There's no reason why this couldn't created as a little function or script, so it's then still only one command.






            share|improve this answer




















            • Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
              – VST
              Jul 28 '17 at 11:26










            • @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
              – roaima
              Jul 28 '17 at 11:49











            • Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
              – VST
              Jul 28 '17 at 11:51













            0












            0








            0






            You can get a current timestamp with the date command



            date +'%Y%m%d_%H%M%S'


            You can copy the contents of a directory only to a directory that already exists, unless you're using a recursive copy. However, you can't use a recursive copy unless you want the previous backup directories also copied to the new backup directory.



            So this has to be done in a couple of steps



            backup_dir="_bak_$(date +'%Y%m%d_%H%M%S')"
            mkdir "$backup_dir"
            cp -p !(_bak_*) "$backup_dir/"


            Bear in mind that as written this will not copy dot files (files beginning with a . character), since these are usually treated specially by the shell. (You might want to consider naming your backup directory .bak_* instead of _bak_* to benefit from this feature. You can then cp -p * "$backup_dir/")



            There's no reason why this couldn't created as a little function or script, so it's then still only one command.






            share|improve this answer












            You can get a current timestamp with the date command



            date +'%Y%m%d_%H%M%S'


            You can copy the contents of a directory only to a directory that already exists, unless you're using a recursive copy. However, you can't use a recursive copy unless you want the previous backup directories also copied to the new backup directory.



            So this has to be done in a couple of steps



            backup_dir="_bak_$(date +'%Y%m%d_%H%M%S')"
            mkdir "$backup_dir"
            cp -p !(_bak_*) "$backup_dir/"


            Bear in mind that as written this will not copy dot files (files beginning with a . character), since these are usually treated specially by the shell. (You might want to consider naming your backup directory .bak_* instead of _bak_* to benefit from this feature. You can then cp -p * "$backup_dir/")



            There's no reason why this couldn't created as a little function or script, so it's then still only one command.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 28 '17 at 9:57









            roaima

            42.8k551116




            42.8k551116











            • Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
              – VST
              Jul 28 '17 at 11:26










            • @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
              – roaima
              Jul 28 '17 at 11:49











            • Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
              – VST
              Jul 28 '17 at 11:51
















            • Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
              – VST
              Jul 28 '17 at 11:26










            • @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
              – roaima
              Jul 28 '17 at 11:49











            • Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
              – VST
              Jul 28 '17 at 11:51















            Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
            – VST
            Jul 28 '17 at 11:26




            Your last command says - bash: !: event not found, do I need to add any other lines, or this is just a glitch ?
            – VST
            Jul 28 '17 at 11:26












            @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
            – roaima
            Jul 28 '17 at 11:49





            @VST you must already have extglob enabled because otherwise your own example in your question couldn't have worked.
            – roaima
            Jul 28 '17 at 11:49













            Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
            – VST
            Jul 28 '17 at 11:51




            Ok, thanks for that, I didn't know maybe someone else has enabled that already in the system.
            – VST
            Jul 28 '17 at 11:51

















            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%2f382313%2fbackup-all-contents-of-current-directory-to-a-subdirectory-inside-the-current-di%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

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay