Find the answer to your Linux question:
Results 1 to 6 of 6
Ok, here's a toughie... Code: tar --after-date="$START_TIME" -cf "my-archive.tar" my-folder/*?[!*.reg] Because of the --after-date thing, this ommits a lot of files, but it still includes the directories they were in, ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6

    shell script... exclude empty directories

    Ok, here's a toughie...

    Code:
    tar --after-date="$START_TIME" -cf "my-archive.tar" my-folder/*?[!*.reg]
    Because of the --after-date thing, this ommits a lot of files, but it still includes the directories they were in, which means I'm left with lots of empty directories in my archive. Any ideas how to get rid of them? I don't mind breaking this statement up into more than one.

    -Nathan

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    First, would you please explain what you mean by this?
    Code:
    *?[!*.reg]
    In the famous words of Inigo Montoya, I do not think it means what you think it means.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6
    Quote Originally Posted by wje_lf View Post
    First, would you please explain what you mean by this?
    Code:
    *?[!*.reg]
    In the famous words of Inigo Montoya, I do not think it means what you think it means.
    Well it seems to match the files I want it to. It misses out all the files ending in .reg in the root directory. So the file my-folder/blah.reg is excluded, but my-folder/sub-folder/moo.reg is included.

  4. #4
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6
    Oh, and I might get shot down for admitting this, but I don't quite understand why it works this way. Although I'm very happy that it does. And I'll make sure I understand it before it becomes important.

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by njmacinnes View Post
    Well it seems to match the files I want it to. It misses out all the files ending in .reg in the root directory. So the file my-folder/blah.reg is excluded, but my-folder/sub-folder/moo.reg is included.
    No.

    Use the bash man page when in doubt. Concretely, for this case, search for "Pathname Expansion". There, you can read this (amongst many other useful things):

    Quote Originally Posted by bash man page
    Pathname Expansion
    After word splitting, unless the -f option has been set, bash scans each word for
    the characters *, ?, and [. If one of these characters appears, then the word is
    regarded as a pattern, and replaced with an alphabetically sorted list of file
    names matching the pattern.

    [non relevant contents trimmed down to shorten the post]

    [...] Matches any one of the enclosed characters.
    Look at the bolds.

    So, as a sample, this command:

    Code:
    ls -d *?[!*.reg]
    Would list all the files whose name has at least one character (because of the ?, which doesn't match the null string), and doesn't end on one of these characters (the commas are just separators): *, . (dot), r, e OR g.

    The reason why .reg files are not listed, is just because they end in "g", which is a side effect. But that is not the intended effect, and might render your backups completely useless if it left out some other important files ended in one of those characters.

    So, no, your regexp doesn't work.

    Here you have some examples to illustrate what I said:

    $ ls
    dir flix.reg kiss* p pepe pepe. p_output_10 p_output_4 p_output_7 r reg
    $ ls *?[!*.reg]
    p_output_10 p_output_4 p_output_7
    ls *.?(reg)
    flix.reg pepe.
    $ ls *.!(reg)
    pepe.
    $ ls *!(reg)
    flix.reg kiss* p pepe pepe. p_output_10 p_output_4 p_output_7

    dir:

    r:

    reg:
    $ ls !(*.reg)
    kiss* p pepe pepe. p_output_10 p_output_4 p_output_7

    dir:

    r:

    reg:
    Note however, that this is an extended syntax, and as the man page says, it only works if you have the bash shell option extglob turned on.

    You can turn it on by doing "shopt -s extglob". Its not persistent, so, put it into your bashrc and bash_profile files if you intent to use this regularly (or just include it in your script).

  6. #6
    Just Joined!
    Join Date
    Mar 2008
    Posts
    6
    Ah, ok thanks. I just got that off the internet somewhere. Can't remember where. Looks like I need to rewrite it.

    I knew that [] matches any of the characters, but I guess I just assumed something about the syntax made this different.

    I don't suppose I needed to make my second post! You already knew that I didn't understand. I'll have a go myself, then come back if I get stuck... unless writing it all for me is your idea of fun

Posting Permissions

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