0

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.

0

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 {} {} \;
  • 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 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.