tar with nonexistant files...

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

  1. ldfishel

    ldfishel 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...
    ldfishel, Apr 25, 2014
    1. Advertisements

  2. ldfishel

    Paul Guest

    This article has an answer, of sorts.


    "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, Apr 25, 2014
    1. Advertisements

  3. ldfishel

    ldfishel 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!
    ldfishel, Apr 28, 2014
  4. ldfishel

    Paul Guest

    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, Apr 28, 2014
  5. ldfishel

    ldfishel 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?
    ldfishel, Apr 28, 2014
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.