1

As the title suggests I need to .zip multiple files (thousands) in a folder and have each file zipped into their own archive with the original file name as the .zip filename.

If I try to do this with gzip, it is simple:

   $gzip *.fileextension

and it will .gz each file, delete the original and rename itself to the original filename

Example Input: file1.txt file2.txt file3.txt

Output: file1.gz file2.gz file3.gz

HOW DO I DO THIS WITH .ZIP?

1 Answer 1

3

You can create a small bash shellscript zipsep,

#!/bin/bash

for i in "$@"
do
 zip "$i".zip "$i"
done

Make it executable,

chmod +x zipsep

Move it to a directory in PATH,

sudo mv zipsep /usr/local/bin

Now you can use zipsep to zip each file separately, for example

zipsep *.txt

If you want to remove the original files, you can do that separately afterwards in the example above by

rm *.txt

or interactively if only a few files (safer)

rm -i *.txt

It is also possible to put the removal into the shellscript, but I would prefer to do it separately.


Edit: If there will be no problem with files with the same name with different extensions for example file1.txt and file1.doc and file2.pdf, you can use a bash parameter substitution to remove the original extension from the zip file name. See man bash and search for 'Parameter Expansion' and ${parameter%word} 'Remove matching suffix pattern'.

#!/bin/bash

for i in "$@"
do
 zip "${i%.*}".zip "$i"
done

If you already created zip files without removing the original extension, you can use the following command to check, that it works as intended 'dry run',

rename -n 's/\.[a-zA-Z0-9]*\././' *.zip

and then remove the option -n to do it,

rename 's/\.[a-zA-Z0-9]*\././' *.zip

With this method you will avoid overwriting if there were original files with the same name and different extensions. At least my version of rename will refuse to overwrite, 'perl rename': See man rename:

The original "rename" did not check for the existence of target filenames, so had to be used with care. I hope I've fixed that (Robin Barker).

7
  • 1
    zipsep is a tongue twister :-) … Probably extra info, but that for loop might work without the in "$@" part … it’s the default behavior … askubuntu.com/a/1419265
    – Raffa
    Aug 11, 2022 at 19:13
  • 1
    @SatoshiNakamoto, the old style is to create ~/bin and put your own shellscripts and other executables there, and after reboot ~/bin will be in PATH. Then only you have access to them. If you want general access to your own shellscripts and other executables, put them in /usr/local/bin. There is also /usr/local/sbin for executables that need sudo.
    – sudodus
    Aug 11, 2022 at 19:13
  • 1
    Thanks for that information @Raffa :-)
    – sudodus
    Aug 11, 2022 at 19:15
  • 1
    @sudodus thanks for the info!! Yes I prefer all the old school way of things Aug 11, 2022 at 19:19
  • 1
    I thought that is best to keep the extensions, because there might be files with the same name with different extensions for example file1.txt and file1.doc and file2.pdf. If this is never the case, you can use a bash parameter substitution, that I will append to the answer.
    – sudodus
    Aug 12, 2022 at 5:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .