1

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:

./product/install/install.log
./product/execution/daily.log
./other/conf/blah.log

And I end up with:

/tmp/allLogs/install.log
/tmp/allLogs/daily.log
/tmp/allLogs/blah.log

And I would like to have:

/tmp/allLogs/product/install/install.log
/tmp/allLogs/product/execution/daily.log
/tmp/allLogs/other/conf/blah.log
sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
OscarRyz
  • 384
  • 1
  • 7
  • 15

4 Answers4

3

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
.
./foo
./bar
./bar/baz.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 
./bar/baz.log
Kyle Brandt
  • 83,619
  • 74
  • 305
  • 448
2

Try using cpio in pass-through mode

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

user9517
  • 115,471
  • 20
  • 215
  • 297
2
find . -name "*.log" | tar -cz --files-from - -f /path/to/file.tgz
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
0

What about filtering just the logs into the archive:

sudo tar -zcvf archive.tar.gz /path/**/*.log
Noam Manos
  • 307
  • 1
  • 2
  • 8