Find the answer to your Linux question:
Results 1 to 5 of 5
Hi. In a directory, I have files named a, b, c, d, e, f, g, h, i, and j. When I want ls to output only the file named e, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2013
    Posts
    69

    tar --exclude and grep -v


    Hi.

    In a directory, I have files named a, b, c, d, e, f, g, h, i, and j.
    When I want ls to output only the file named e, I use
    Code:
    ls | grep -e 'e'
    Now, if I want ls to output everything except e, I use
    Code:
    ls | grep -e 'e' -v
    Now, I want to create a .tar.gz file that contains only e, I use
    Code:
    tar --exclude=īls | grep -e 'e' -vī -c -z -f ./file.tar.gz ./
    But all files except e were tarred. Can someone please explain me what is wrong with that ?
    (Of course, this is a basic example, and the easiest/laziest answers would be : tar -c -z -f ./file.tar.gz ./e OR tar --exclude=īls | grep -e 'e'ī -c -z -f ./file.tar.gz ./e).

    Thanks in advance

  2. #2
    Linux Newbie
    Join Date
    Jun 2012
    Location
    SF Bay area
    Posts
    164
    If you can come up with a list of file you want included in the "tar" archive, you can just use the "-T" (also called "--files-from") option to tell "tar" like this:

    Code:
    ls | grep ...whatever... | tar -c -T - -f /path/name-of-output.tar
    I typically use "find" to generate the list of files but if you want to "grep" things out of an "ls" command that will work too. Just pipe the list of file to "tar" and use "-T -" to tell it that's where the list of files are.

  3. #3
    Just Joined!
    Join Date
    Apr 2013
    Posts
    69
    Quote Originally Posted by cnamejj View Post
    If you can come up with a list of file you want included in the "tar" archive, you can just use the "-T" (also called "--files-from") option to tell "tar" like this:

    Code:
    ls | grep ...whatever... | tar -c -T - -f /path/name-of-output.tar
    I typically use "find" to generate the list of files but if you want to "grep" things out of an "ls" command that will work too. Just pipe the list of file to "tar" and use "-T -" to tell it that's where the list of files are.
    It produce the desired tar file, but I do not understand why my method does not work.

  4. #4
    Just Joined!
    Join Date
    Nov 2006
    Location
    Bulgaria
    Posts
    7
    Quote Originally Posted by chris_inx View Post
    It produce the desired tar file, but I do not understand why my method does not work.
    Your method does not work because result of ls and grep is passed expanded to --exclude with all spaces, etc. In general, I expect the result of such invocation to be undefined.

    Anyway, I thought the task is to exclude a list of files and add to a tarball everything else, so, this is the invterted variant:
    Code:
    ls | grep -e e -v  | tar --exclude-from - -c -f ../file.tar ./
    This way tar reads list of patterns, one per line. You may wish to store exclusion list to a permanent file and not to pipe it.

  5. #5
    Just Joined!
    Join Date
    Apr 2013
    Posts
    69
    Quote Originally Posted by slonmron View Post
    Your method does not work because result of ls and grep is passed expanded to --exclude with all spaces, etc. In general, I expect the result of such invocation to be undefined.

    Anyway, I thought the task is to exclude a list of files and add to a tarball everything else, so, this is the invterted variant:
    Code:
    ls | grep -e e -v  | tar --exclude-from - -c -f ../file.tar ./
    This way tar reads list of patterns, one per line. You may wish to store exclusion list to a permanent file and not to pipe it.
    Thanks for the explanation. You thought right : the goal was to exclude a list of files and tar everything else.

    Thanks to both of you for providing a better (or at least one that works !) way to achieve that task.

Posting Permissions

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