Results 1 to 9 of 9

Thread: tar wildcard question

  1. #1
    Join Date
    Nov 2013
    Beans
    1

    tar wildcard question

    I'd like to create a new archive with files

    example1.txt
    example2.txt
    example3.txt

    How do I create the archive using a wildcard and leaving example3.txt out?

    Thanks

  2. #2
    Join Date
    Apr 2012
    Location
    Canada
    Beans
    75
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: tar wildcard question

    Hi I would do like this
    Code:
    tar cvf example.tar --exclude *3.txt *
    Try
    Code:
     man tar
    Cheers,Bruno
    Last edited by BrunoLotse; November 9th, 2013 at 05:23 PM.

  3. #3
    Join Date
    Apr 2012
    Beans
    7,256

    Re: tar wildcard question

    If you really want to use globbing for some reason, you can enable the shell's extended globbing (extglob) feature and use the !(3) ("not 3") pattern

    Code:
    $ tar cvf examples.tar example*.txt
    example1.txt
    example2.txt
    example3.txt
    $ 
    $ shopt -s extglob
    $ tar cvf examples.tar example!(3).txt
    example1.txt
    example2.txt

  4. #4
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: tar wildcard question

    Quote Originally Posted by steeldriver View Post
    If you really want to use globbing for some reason, you can enable the shell's extended globbing (extglob) feature and use the !(3) ("not 3") pattern

    Code:
    $ tar cvf examples.tar example*.txt
    example1.txt
    example2.txt
    example3.txt
    $ 
    $ shopt -s extglob
    $ tar cvf examples.tar example!(3).txt
    example1.txt
    example2.txt
    Question for you: when do you need to explicitly turn on extglob? I've been using the '!()' pattern for a while now (in fact, though about posting that very same thing to this thread earlier!), but I've never explicitly turned on extglob. I didn't even know I had to do that, but I had a quick look at Greg's Wiki, and according to that:

    In addition to the traditional globs (supported by all Bourne-family shells) that we've seen so far, Bash (and Korn Shell) offers extended globs, which have the expressive power of regular expressions. Korn shell enables these by default; in Bash, you must run the command:
    Code:
    shopt -s extglob
    Using the same idea, this works for me without the extglob option:

    Code:
    $ ls
    example1.txt  example2.txt  example3.txt
    
    
    [ dave@CygnusX1: ~/sandbox/tar ] $ tar cvf test.tar !(*3.txt)
    example1.txt
    example2.txt
    
    
    [ dave@CygnusX1: ~/sandbox/tar ] $ tar tvf test.tar 
    -rw-rw-r-- dave/dave         0 2013-11-09 13:11 example1.txt
    -rw-rw-r-- dave/dave         0 2013-11-09 13:11 example2.txt
    I had a quick search through .bashrc, .profile, etc., and I can't find any evidence it's been turned on by default in there, but I also don't know where to look to find the default options that are turned on. Do you know why this works for me without having to turn on extglob, and / or how to find out what options are turned on in the current session?

  5. #5
    Join Date
    Apr 2012
    Beans
    7,256

    Re: tar wildcard question

    Oops it seems you are correct, it does seem to be enabled by default. You can see the current settings using 'shopt' without arguments

    Code:
    $ shopt | grep ext
    extdebug        off
    extglob         on
    extquote        on
    and since not even /etc/bash.bashrc nor /etc/skel/.bashrc mention it, I guess it's compiled in ? I don't really know

    Code:
    $ grep shopt /etc/bash.bashrc
    shopt -s checkwinsize
    #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    $ 
    $ grep shopt /etc/skel/.bashrc
    shopt -s histappend
    shopt -s checkwinsize
    #shopt -s globstar

  6. #6
    Join Date
    Apr 2012
    Location
    Canada
    Beans
    75
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: tar wildcard question

    Hi
    Code:
    shopt  | grep extglob
    Cheers,Bruno

    Oops. It's already there. Racing....
    Last edited by BrunoLotse; November 9th, 2013 at 07:51 PM.

  7. #7
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: tar wildcard question

    Quote Originally Posted by steeldriver View Post
    Oops it seems you are correct, it does seem to be enabled by default. You can see the current settings using 'shopt' without arguments

    Code:
    $ shopt | grep ext
    extdebug        off
    extglob         on
    extquote        on
    and since not even /etc/bash.bashrc nor /etc/skel/.bashrc mention it, I guess it's compiled in ? I don't really know

    Code:
    $ grep shopt /etc/bash.bashrc
    shopt -s checkwinsize
    #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    $ 
    $ grep shopt /etc/skel/.bashrc
    shopt -s histappend
    shopt -s checkwinsize
    #shopt -s globstar
    That's very informative! It's also interesting because everything I look at says that extglob is not enabled by default in Bash. I can't seem to find what controls these options, but at least now I know that:

    1. It's enabled by default
    2. How to look to see what options are currently configured in my shell.


    Once again, I learned something very useful from you!

  8. #8
    Join Date
    Apr 2012
    Location
    Canada
    Beans
    75
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: tar wildcard question

    Hi
    Code:
    shopt | less
    Cheers,Bruno

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: tar wildcard question

    It seams that in GNU Bash 4.2 extglob is enabled by default in interactive shells. If I remember it correctly this wasn't the case in earlier versions.

    In non-interactive shells/scripts it's still disabled by default:
    Code:
    [sisco@acme ~]$ shopt extglob 
    extglob            on
    [sisco@acme ~]$ bash -c 'shopt extglob'
    extglob            off

Tags for this Thread

Bookmarks

Posting Permissions

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