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.
Asked
Active
Viewed 1,632 times
1 Answers
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
{} {}
to{}.7z {}
so something likeimage.png
would archive toimage.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