find and zip multiple file extensions while keeping the folder structure

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











up vote
1
down vote

favorite












Ubuntu 16.04



I would like to backup all .txt, .cfg, and .ini files, while keeping their folder structure to a zip file in the backups folder.



I know there is an easier way to do this entire operation but this is what I have.



#!/bin/bash

wdir="/home/files"
backup_dir="/home/files/backup"
allcfg=$(find suan -name '*.cfg')
alltxt=$(find suan -name '*.txt')
allini=$(find suan -name '*.ini')
timeStamp="$(date +%Y--%b-%d--%k:%M--%P)"
backupfilename="$backup_dir/Backup-$timeStamp.zip"

#-- set some arrays ...
backupfiles=( "$allcfg" "$alltxt" "$allini" )

cd "$wdir"
zip -r "$backupfilename" "$backupfiles"


So I can see a nice output of the files it files but when it gets to the zipping part it errors out.




zip error: Nothing to do! (try: zip -r /home/files/backup/Backup-2018--Jul-04--21:37--pm.zip . -i suan/cfg/360controller.cfg



But that doesn't work either.







share|improve this question















  • 2




    When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
    – Rakesh Sharma
    Jul 5 at 2:05














up vote
1
down vote

favorite












Ubuntu 16.04



I would like to backup all .txt, .cfg, and .ini files, while keeping their folder structure to a zip file in the backups folder.



I know there is an easier way to do this entire operation but this is what I have.



#!/bin/bash

wdir="/home/files"
backup_dir="/home/files/backup"
allcfg=$(find suan -name '*.cfg')
alltxt=$(find suan -name '*.txt')
allini=$(find suan -name '*.ini')
timeStamp="$(date +%Y--%b-%d--%k:%M--%P)"
backupfilename="$backup_dir/Backup-$timeStamp.zip"

#-- set some arrays ...
backupfiles=( "$allcfg" "$alltxt" "$allini" )

cd "$wdir"
zip -r "$backupfilename" "$backupfiles"


So I can see a nice output of the files it files but when it gets to the zipping part it errors out.




zip error: Nothing to do! (try: zip -r /home/files/backup/Backup-2018--Jul-04--21:37--pm.zip . -i suan/cfg/360controller.cfg



But that doesn't work either.







share|improve this question















  • 2




    When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
    – Rakesh Sharma
    Jul 5 at 2:05












up vote
1
down vote

favorite









up vote
1
down vote

favorite











Ubuntu 16.04



I would like to backup all .txt, .cfg, and .ini files, while keeping their folder structure to a zip file in the backups folder.



I know there is an easier way to do this entire operation but this is what I have.



#!/bin/bash

wdir="/home/files"
backup_dir="/home/files/backup"
allcfg=$(find suan -name '*.cfg')
alltxt=$(find suan -name '*.txt')
allini=$(find suan -name '*.ini')
timeStamp="$(date +%Y--%b-%d--%k:%M--%P)"
backupfilename="$backup_dir/Backup-$timeStamp.zip"

#-- set some arrays ...
backupfiles=( "$allcfg" "$alltxt" "$allini" )

cd "$wdir"
zip -r "$backupfilename" "$backupfiles"


So I can see a nice output of the files it files but when it gets to the zipping part it errors out.




zip error: Nothing to do! (try: zip -r /home/files/backup/Backup-2018--Jul-04--21:37--pm.zip . -i suan/cfg/360controller.cfg



But that doesn't work either.







share|improve this question











Ubuntu 16.04



I would like to backup all .txt, .cfg, and .ini files, while keeping their folder structure to a zip file in the backups folder.



I know there is an easier way to do this entire operation but this is what I have.



#!/bin/bash

wdir="/home/files"
backup_dir="/home/files/backup"
allcfg=$(find suan -name '*.cfg')
alltxt=$(find suan -name '*.txt')
allini=$(find suan -name '*.ini')
timeStamp="$(date +%Y--%b-%d--%k:%M--%P)"
backupfilename="$backup_dir/Backup-$timeStamp.zip"

#-- set some arrays ...
backupfiles=( "$allcfg" "$alltxt" "$allini" )

cd "$wdir"
zip -r "$backupfilename" "$backupfiles"


So I can see a nice output of the files it files but when it gets to the zipping part it errors out.




zip error: Nothing to do! (try: zip -r /home/files/backup/Backup-2018--Jul-04--21:37--pm.zip . -i suan/cfg/360controller.cfg



But that doesn't work either.









share|improve this question










share|improve this question




share|improve this question









asked Jul 5 at 1:45









needtoknow

1286




1286







  • 2




    When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
    – Rakesh Sharma
    Jul 5 at 2:05












  • 2




    When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
    – Rakesh Sharma
    Jul 5 at 2:05







2




2




When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
– Rakesh Sharma
Jul 5 at 2:05




When you quoted the variables, the filenames individuality is lost. Try the array option or do everything within find
– Rakesh Sharma
Jul 5 at 2:05










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










This is how the -r recursive option on 7z works, it's a recursive pattern search.



7z a -r "$backupfilename" *.ini *.cfg *.txt


7z creates zip files just fine.



You could also do the multi-arg option on find if you somehow don't have 7zip,



find suan ( -name *.cfg -o -name *.txt -o -name *.ini ) 
-exec zip -r "$backupfilename" +





share|improve this answer





















  • @thill Excellent!
    – needtoknow
    Jul 6 at 3:40

















up vote
0
down vote













Using find and zip:



find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' 
-exec zip -r "$backupfilename" +


You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.



This would find all relevant files and archive them in your backup file.



Alternatively with bash, if there's not thousands of files:



set -s globstar nullglob dotglob
cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt


The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.






share|improve this answer























    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    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%2f453511%2ffind-and-zip-multiple-file-extensions-while-keeping-the-folder-structure%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    This is how the -r recursive option on 7z works, it's a recursive pattern search.



    7z a -r "$backupfilename" *.ini *.cfg *.txt


    7z creates zip files just fine.



    You could also do the multi-arg option on find if you somehow don't have 7zip,



    find suan ( -name *.cfg -o -name *.txt -o -name *.ini ) 
    -exec zip -r "$backupfilename" +





    share|improve this answer





















    • @thill Excellent!
      – needtoknow
      Jul 6 at 3:40














    up vote
    2
    down vote



    accepted










    This is how the -r recursive option on 7z works, it's a recursive pattern search.



    7z a -r "$backupfilename" *.ini *.cfg *.txt


    7z creates zip files just fine.



    You could also do the multi-arg option on find if you somehow don't have 7zip,



    find suan ( -name *.cfg -o -name *.txt -o -name *.ini ) 
    -exec zip -r "$backupfilename" +





    share|improve this answer





















    • @thill Excellent!
      – needtoknow
      Jul 6 at 3:40












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    This is how the -r recursive option on 7z works, it's a recursive pattern search.



    7z a -r "$backupfilename" *.ini *.cfg *.txt


    7z creates zip files just fine.



    You could also do the multi-arg option on find if you somehow don't have 7zip,



    find suan ( -name *.cfg -o -name *.txt -o -name *.ini ) 
    -exec zip -r "$backupfilename" +





    share|improve this answer













    This is how the -r recursive option on 7z works, it's a recursive pattern search.



    7z a -r "$backupfilename" *.ini *.cfg *.txt


    7z creates zip files just fine.



    You could also do the multi-arg option on find if you somehow don't have 7zip,



    find suan ( -name *.cfg -o -name *.txt -o -name *.ini ) 
    -exec zip -r "$backupfilename" +






    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jul 5 at 2:56









    jthill

    2,283715




    2,283715











    • @thill Excellent!
      – needtoknow
      Jul 6 at 3:40
















    • @thill Excellent!
      – needtoknow
      Jul 6 at 3:40















    @thill Excellent!
    – needtoknow
    Jul 6 at 3:40




    @thill Excellent!
    – needtoknow
    Jul 6 at 3:40












    up vote
    0
    down vote













    Using find and zip:



    find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' 
    -exec zip -r "$backupfilename" +


    You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.



    This would find all relevant files and archive them in your backup file.



    Alternatively with bash, if there's not thousands of files:



    set -s globstar nullglob dotglob
    cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt


    The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.






    share|improve this answer



























      up vote
      0
      down vote













      Using find and zip:



      find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' 
      -exec zip -r "$backupfilename" +


      You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.



      This would find all relevant files and archive them in your backup file.



      Alternatively with bash, if there's not thousands of files:



      set -s globstar nullglob dotglob
      cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt


      The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using find and zip:



        find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' 
        -exec zip -r "$backupfilename" +


        You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.



        This would find all relevant files and archive them in your backup file.



        Alternatively with bash, if there's not thousands of files:



        set -s globstar nullglob dotglob
        cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt


        The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.






        share|improve this answer















        Using find and zip:



        find "$wdir" -type f '(' -name '*.cfg' -o -name '*.txt' -o -name '*.ini' ')' 
        -exec zip -r "$backupfilename" +


        You may alternatively want to let your script cd into "$wdir" first and then use . in the find command, depending on what path you'd like to have saved in the archive.



        This would find all relevant files and archive them in your backup file.



        Alternatively with bash, if there's not thousands of files:



        set -s globstar nullglob dotglob
        cd "$wdir" && zip -r "$backupfilename" **/*.ini **/*.cfg **/*.txt


        The shell options that are set here enables the ** glob (which globs pathnames), makes unexpanded globs expand to the empty string and makes globs match hidden files.







        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 13 at 9:53


























        answered Jul 13 at 9:47









        Kusalananda

        101k13199312




        101k13199312






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453511%2ffind-and-zip-multiple-file-extensions-while-keeping-the-folder-structure%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?