tar - exclude certain files

Posted by Alan on Super User See other posts from Super User or by Alan
Published on 2011-06-26T15:52:06Z Indexed on 2011/06/26 16:24 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

I wish to tar all files in a directory and its subdirectories that do NOT end in .jpg, .bmp, .gif, or png.

So, given the following folders and files:

foo/file.txt
foo/file.gif
foo/bar/file
foo/bar/image.jpg

I want to tar only the files file.txt and file. file.gif and image.jpg should be ignored. I would also like to maintain the folder structure.

My first thought was to pipe the results of the find command in conjunction with grep -v ".jpg|.gif|.bmp.png" to a text file, and then use the tar include argument to feed it that list of files.

However, the results of the grepped find command also contain directories (in the example above, it would be "foo" and "foo/bar"), and when a directory is fed to tar, it includes all files in that directory, so I would end up with a tar file containing all of the files--not what I want.

Is there any way to prevent find from outputting directories? Is there a far easier way to approach this?

© Super User or respective owner

Related posts about find

Related posts about grep