Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to create a tar file suitable for extracting into /. I've created a work directory that represents the root of the file system, and it has all the stuff I want included in the tar underneath, like: etc/rc.d/init.d/glassfish3, opt/glassfish3/..., and other directories and files under opt/....

When I do tar zcvpf files.tgz * it includes the intermediate directories (like etc, etc/rc.d, and opt) in the tar file. Then when I extract it into / on another system, it has the undesirable behavior of mucking with ownership and permissions on those directories. I really want to leave those alone and not touch them.

So I thought I found a good way around this with:

find . -type f -print0 | tar zcvpf files.tgz --null --files-from -

which creates a tar that only includes files. This is great, it only gets the files I put in there... except I just discovered it's not picking up important leaf directories, like:

opt/glassfish3/glassfish/lib/asadmin

which is an empty directory (and needed by Glassfish3, or it will complain).

How can I create a tar file that skips intermediates but not final directories? I have a feeling some find+awk magic might do it. It doesn't need to be a one-liner though, readability is important. Thanks.


Edit: I found this. Other/better ideas welcome:

cat <(find . -type f -print0) <(find . -type d -empty -print0) | tar zcvpf files.tgz --null --files-from -
share|improve this question

1 Answer 1

up vote 4 down vote accepted

find can take -o for "or", so you can combine your two find commands like so:

find . \( -type f -o -type d -empty \) -print0 | tar ...

Or, if you can guarantee all the files can fit on one command line,

find . \( -type f -o -type d -empty \) -exec tar cvfz files.tgz {} +
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.