Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently using this script line to find all the log files from a given directory structure and copy them to another directy where I can easily compress them.

find . -name "*.log" -exec cp \{\} /tmp/allLogs/ \;

The problem I have, is, the directory/subdirectory information gets lost because, I'm copying only the file.

For instance I have:


And I end up with:


And I would like to have:

share|improve this question
up vote 3 down vote accepted

What is the reason to copy them to another directory to compress them? The following will create a compressed tar file off all the log files while keeping the directory structure in one step (Assuming it is run from the root directory:

find . -iname '*.log' -print0 | xargs -0 tar zcf /tmp/test.tar.gz

For example:

kbrandt@alpine:~/scrap/tar$ find . *.log
kbrandt@alpine:~/scrap/tar$ find . -iname '*.log' -print0 | xargs -0 tar zcf /tmp/test.tar.gz
#List files in the archive with the -t switch
kbrandt@alpine:~/scrap/tar$ tar -tzf /tmp/test.tar.gz 
share|improve this answer
The reason is ... ignorance, definitely :) .) Thank you Kyle – OscarRyz Jan 3 '11 at 18:51

Try using cpio in pass-through mode

find . -name '*.log' | cpio -pdm /tmp/allLogs

share|improve this answer
find . -name "*.log" | tar -cz --files-from - -f /path/to/file.tgz
share|improve this answer

Your Answer


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.