7

is it possible to create a tar.gz file from contents of a folder but only put the files with certain name pattern in the archive and ignore the rest? for example just put the files which their names are log_?

0

2 Answers 2

12

Example Using zip(you can choose any other compression tool instead):

find /path-to-dir -name 'log_*' | zip archive.zip -@

example:

$ ls
file  log_1  log_2  log_3

Now zip all files log_* in zipped called archive.zip

$ find . -name 'log_*' | zip archive.zip -@
  adding: log_1 (stored 0%)
  adding: log_3 (stored 0%)
  adding: log_2 (stored 0%)

Now let's check output:

$ ls
archive.zip  file  log_1  log_2  log_3
1
  • 1
    Note that this search is recursive into subdirectories. The folder structure is kept in the compressed output file, but containing only files that matched the search query (eg. log_1, log_2, folder/log_21, folder/folder/log_31....)
    – rtribaldos
    Apr 21, 2021 at 15:35
10

You can use tar directly:

tar -zcvf file.tar.gz log_*
1
  • it seems to be simplest way
    – MoienGK
    Jun 23, 2015 at 13:12

You must log in to answer this question.

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