1

I am trying to 7zip all files inside a directory separately into their own archive (for each file) in Linux Centos 7.6. Been looking around for a while but never found a method.

1 Answers1

2

Assuming you'd like to do this to all regular files recursively in and below some top-level directory $topdir:

find "$topdir" -type f -exec 7za a {} {} \;

For a single directory containing only files that you'd like to compress, you would use

for pathname in "$topdir"/*; do
    7za a "$pathname" "$pathname"
done

The difference here is that hidden files would not be compressed. If you enable the dotglob shell option in bash with shopt -s dotglob, then the loop would include hidden names.

With find (which would amount to less typing), you could do the files in a single directory with

find "$topdir" -maxdepth 1 -type f -exec 7za a {} {} \;
Kusalananda
  • 333,661
  • Excuse my ignorance. Let's say the top directory's name was Test. How would I make this work? I'm not that proficient in writing scripts. – KnowledgeSeeker Mar 24 '19 at 16:49
  • I'd just modify the {} {} to {}.7z {} so something like image.png would archive to image.png.7z, otherwise 7zip threw error. And to also answer comment above, I've used full path such as: find "/home/myuser/test/batch" -maxdepth 1 -type f -exec 7za a {}.7z {} \; – LuxZg Jun 05 '23 at 08:40