tar with nonexistant files...

Discussion in 'Computer Information' started by ldfishel@yahoo.com, Apr 25, 2014.

  1. Guest

    I'm back....

    Maybe someone here can help me with what seems like it should be a simple issue, and hopefully will be for someone.

    I find myself regularly (on some Linux-y system) wanting to tar/gz up all the source files in a directory to copy to another machine (or backup or whatever), so I wrote a VERY simple bash script to run in any directory with afew key strokes:

    tar xzf code.tar.gz makefile *.{c,cpp,h}

    This works perfectly unless the directory has, say no .c file. Then I get the following:

    tar: *.c: Cannot stat: No such file or directory
    tar: Exiting with failure status due to previous errors

    Does tar have a flag I'm missing or am I missing a way of expressing this glob that won't choke tar? Shell scripts are really not my thing, but yeah, I'm sure I could spend an hour writing a script to assemble a more specificcommand... The question is more about learning something new than saving an hour...
    , Apr 25, 2014
    #1
    1. Advertising

  2. Paul Guest

    wrote:
    > I'm back....
    >
    > Maybe someone here can help me with what seems like it should be a simple issue, and hopefully will be for someone.
    >
    > I find myself regularly (on some Linux-y system) wanting to tar/gz up all the source files in a directory to copy to another machine (or backup or whatever), so I wrote a VERY simple bash script to run in any directory with a few key strokes:
    >
    > tar xzf code.tar.gz makefile *.{c,cpp,h}
    >
    > This works perfectly unless the directory has, say no .c file. Then I get the following:
    >
    > tar: *.c: Cannot stat: No such file or directory
    > tar: Exiting with failure status due to previous errors
    >
    > Does tar have a flag I'm missing or am I missing a way of expressing this glob that won't choke tar? Shell scripts are really not my thing, but yeah, I'm sure I could spend an hour writing a script to assemble a more specific command... The question is more about learning something new than saving an hour...
    >
    >


    This article has an answer, of sorts.

    http://www.unix.com/man-page/Linux/7/glob/

    "Empty lists

    With bash one can force the classical behavior using this command:

    shopt -s nullglob
    "

    which would presumably make it work the way you want.

    Paul
    Paul, Apr 25, 2014
    #2
    1. Advertising

  3. Guest

    Thanks. I knew this would probably be a no-brainer for some script sense out there.

    I remember seeing, in some other context while trying to find an answer, the associated historical note of the POSIX ordained change in behavior and thinking how inconvenient it was, but I missed shopt. Since the shopt settings don't outlive the script where they are used, or affect concurrent shells, this works fine for my use, even if it does feel just slightly dirty (but then, I've survived writing Windows code for work. I can't quite seem to convince them to force all their users to switch to a real OS.)

    Moreover, while trying this out, I discovered something I had thought to check after posting my question, (while away from my computers), but hadn't gotten around to checking yet. When I run the script, as written before, it gripes and sounds very much like it didn't work, but it turns out that it actually does run and produces the output I want. So, I could have just ignored the error messages and been fine. Somehow that seems even dirtier... :)

    So, my script now looks like this, in case anyone else needs to do something similar:

    shopt -s nullglob
    tar czf code.tar.gz makefile *.{c,cpp,h} makefile

    Note: The first line MUST be in the script if invoked normally, because invoking a new bash shell clears this flag (unless, of course, you include it in a profile. Speaking of dirty.)

    Thanks again Paul. Are you the only other person who remembers Usenet (except when they have a question they can't answer elsewhere)? I didn't really feel that old until I realized that I even remember when there was no web interface to Usenet (maybe even no Google). Gad! At least I can say I never used punch-cards (for their intended purpose.at least...) Though I did use a hard-copy terminal, probably the last year they were in use at my college, and took COBOL the very last semester it was required. I better stop now....get off my lawn!
    , Apr 28, 2014
    #3
  4. Paul Guest

    wrote:
    > Thanks. I knew this would probably be a no-brainer for some script sense out there.
    >
    > I remember seeing, in some other context while trying to find an answer, the associated historical note of the POSIX ordained change in behavior and thinking how inconvenient it was, but I missed shopt. Since the shopt settings don't outlive the script where they are used, or affect concurrent shells, this works fine for my use, even if it does feel just slightly dirty (but then, I've survived writing Windows code for work. I can't quite seem to convince them to force all their users to switch to a real OS.)
    >
    > Moreover, while trying this out, I discovered something I had thought to check after posting my question, (while away from my computers), but hadn't gotten around to checking yet. When I run the script, as written before, it gripes and sounds very much like it didn't work, but it turns out that it actually does run and produces the output I want. So, I could have just ignored the error messages and been fine. Somehow that seems even dirtier... :)
    >
    > So, my script now looks like this, in case anyone else needs to do something similar:
    >
    > shopt -s nullglob
    > tar czf code.tar.gz makefile *.{c,cpp,h} makefile
    >
    > Note: The first line MUST be in the script if invoked normally, because invoking a new bash shell clears this flag (unless, of course, you include it in a profile. Speaking of dirty.)
    >
    > Thanks again Paul. Are you the only other person who remembers Usenet (except when they have a question they can't answer elsewhere)? I didn't really feel that old until I realized that I even remember when there was no web interface to Usenet (maybe even no Google). Gad! At least I can say I never used punch-cards (for their intended purpose.at least...) Though I did use a hard-copy terminal, probably the last year they were in use at my college, and took COBOL the very last semester it was required. I better stop now...get off my lawn!


    I used to use the Sun Managers group as a source of info,
    before there was a Google/dejanews to archive it with. I didn't post
    any questions there, just read the topics as they came up.
    Came in handy for adjusting stuff on SunOS and Solaris machines
    at work. My newsreader was crude enough, it wasn't even threaded
    (sitting on the Sun box, I didn't have a lot of software choices,
    except what I could find and compile for myself - my first
    web browser on that box, was compiled from source. Took a week.)

    My problem with computing stuff, is many things I'll do, I forget
    the details before the next time I go to do it again.

    Paul
    Paul, Apr 28, 2014
    #4
  5. Guest

    See, you're just trying to make me feel better about my recent for-fun project refreshing on my low-level X11 skills, because Gtk was looking less portable (except for Windows, which it looks like I can now use Gtk on when I get around to setting it up). When did X11 start including Freeype? Years ago? Ack! Ppppppppttt! (Damn, Bill the Cat's been gone for 30 years?) <Hugshis 30 year old Bill the Cat doll> Wait, what were we talking about?
    , Apr 28, 2014
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bobby Janow

    extract tar files?

    Bobby Janow, Jul 15, 2003, in forum: Cisco
    Replies:
    2
    Views:
    12,369
    Bobby Janow
    Jul 15, 2003
  2. Rene Poelman

    QDM.tar

    Rene Poelman, Jul 1, 2004, in forum: Cisco
    Replies:
    0
    Views:
    1,293
    Rene Poelman
    Jul 1, 2004
  3. the niner nation

    .tar files

    the niner nation, Nov 29, 2003, in forum: Computer Support
    Replies:
    15
    Views:
    2,310
  4. david

    tar files???

    david, Apr 8, 2004, in forum: Computer Support
    Replies:
    7
    Views:
    549
  5. Jeremy Watts

    unzipping .tar files

    Jeremy Watts, Nov 14, 2006, in forum: Computer Information
    Replies:
    2
    Views:
    429
    Sjouke Burry
    Nov 14, 2006
Loading...

Share This Page