How to create a zip file from contents of a folder with certain name pattern?

10,250

Solution 1

You can use tar directly:

tar -zcvf file.tar.gz log_*

Solution 2

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
Share:
10,250

Related videos on Youtube

MoienGK
Author by

MoienGK

Updated on September 18, 2022

Comments

  • MoienGK
    MoienGK over 1 year

    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_?

  • MoienGK
    MoienGK almost 9 years
    it seems to be simplest way
  • rtribaldos
    rtribaldos about 3 years
    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....)