Find the answer to your Linux question:
Results 1 to 7 of 7
I have a directory that gets xml files downloaded into it, a large amount of files. I setup a script that will tar all .xml files daily, then remove the ...
  1. #1
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8

    find / tar sript

    I have a directory that gets xml files downloaded into it, a large amount of files. I setup a script that will tar all .xml files daily, then remove the xml files. The problem I'm having is that it will create a zip file even if no files exist in the directory. Is there a way to put a clause it that will say if no xml files then abort the tar creation? I want this since the xml files arent always downloaded to the directory everyday.

    Thx.

    Here's my script so far.

    NOWDATE=`date +%m%d%y`
    cd /home/filestore
    find . -name '*.xml' | xargs tar czvf $NOWDATE.tar.gz --files-from -
    find . -type f -name "*.xml" -exec rm -f {} \;

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    If the .xml files are going to be under /home/filestore only (that is, not under sub directories), you can change your script a bit to:

    Code:
    #!/bin/bash
    NOWDATE=`date +%m%d%y`
    cd /home/filestore
    [ -n "`ls *.xml 2>/dev/null`" ] && {
    find . -name '*.xml' | xargs tar czvf $NOWDATE.tar.gz --files-from -;
    find . -type f -name "*.xml" -exec rm -f {} \; ;
    }
    Change [ -n "`ls *.xml 2>/dev/null`" ] to
    [ -n "`find . -name '*.xml' -type f`" ] if .xml files are under sub-directories of /home/filestore.

  3. #3
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    they are all on one directory, I will test that new script. Thanks for the response.

  4. #4
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    also, can u explain what the part you added does, from what get from it does a list of .xmls them sends them to a temp file.

  5. #5
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    ls *.xml : lists all items ending with .xml

    -n "$some_string": tests if some_string is non-empty

    2>/dev/null: direct error output to /dev/null so it will not show on the standard output, which is the screen in most cases. this is needed because if there are no .xml files under the directory, ls *.xml (without the error redirecting) will report ls: cannot access *.xml: No such file or directory, which will lead to -n test to be true and empty folder to be tar'ed.

    [ some condition ] && {
    do something;
    do other_things;
    }

    is equivalent to

    if [ some_condition ]; then {
    do_something
    do other_things
    }

    So basically the script will test if the directory contains .xml files, if so, start packaging with tar.

  6. #6
    Just Joined!
    Join Date
    Sep 2009
    Posts
    8
    cool, got it, thanks so much for the response and explanation, I appreciate it.

  7. #7
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    No problem at all. In case you want to learn more, here's a site that dedicates to the ins and outs of bash scripting:
    Advanced Bash-Scripting Guide

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...