1

I have the following folder structure

A
|-B
| |-C
|   |-1.log
|   |-2.log
|-10.log
|-D
  |-E
  |-F
    |-php.log
    |-H
      |-php.log

I want to collect all the log files and the php file present in the H subdirectory and make into a tar.

I tried tar -czf temp.tar.gz /A/**/*.log /A/D/F/H/php.log but it doesn't seem to search recursively in the A directory.

I am using Amazon Linux

2
  • The "**" looks wrong. Try tar -czf temp.tar.gz /A/*/*/*.log /A/D/F/H/php.log
    – steve
    Commented Jun 2, 2016 at 10:02
  • Is the / character before the A right? Maybe you should try just A/...
    – Luchostein
    Commented Aug 22, 2016 at 15:07

2 Answers 2

4

Don't rely on wildcard matching / shell globbing but instead use the find command to find and list the files you want and send that list to tar to be archived:

find /A/ -type f -print0 -name \*.log | tar -cvf /path/to/file.tar --null -T -
1
  • That works, but when untarring with tar -xvf test2.tar, it gives me a tar: A/B/2016-04-15.log: Cannot open: Not a directory error.
    – kosta
    Commented Jun 3, 2016 at 1:09
0

The following command will tar all .log files recursively across all your directory structure from the "." parent directory, if you want a specific file, just filter with greps:

tar -cvf file.tar `find . -type f -name *.log`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .