Search code examples
bashcommand-linearchivedirectory-structure7zip

Archive files and dirs, some recursively, some non-recursively, using 7-Zip command line?


I want to archive a bunch of files and dirs with a 7-Zip command line, to be executed as a shell script (bash shell on a Ubuntu 19.04 server).

In my main dir, there are two subdirs: foo and bar.

Both the foo and bar dirs contain subdirs, e.g. the directory structure is like this:

foo
 foo/abc
 foo/pqrs
 foo/etc...
bar
 bar/xyz
 bar/temp
 bar/etc...

I want to create one single 7z archive with:

  • all contents of foo including its subdirs
  • all files in bar but NOT in its subdirs

I can do this separately, but is there a way to do this in one go, perhaps using a @filelist.txt construction?


Solution

  • perhaps using a @filelist.txt construction?

    Using a wildcard seemed easier to me. E.g:

    7z a -x'!bar/*/' my_archive.7z foo bar
    

    The first solution I proposed was:

    find foo bar \
         ! \( -type d -path 'bar/*' -prune \) \
         -type f \
         -exec 7z a 'my_archive.7z' {} +
    

    This will do almost the same except there won't be directory entries in the archive index.