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).
.bashrc
or.bash_aliases
and didn't know you can add your shellscripts to that location ~ however when should be advisable to no put your files there, and if is there any other special location maybe in/usr/
. Or just create your own folder in~/shellscripts
and add it to$PATH
– Satoshi Nakamoto Aug 11 '22 at 19:07zipsep
is a tongue twister :-) … Probably extra info, but that for loop might work without thein "$@"
part … it’s the default behavior … https://askubuntu.com/a/1419265 – Raffa Aug 11 '22 at 19:13~/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 '22 at 19:13file1.txt
andfile1.doc
andfile2.pdf
. If this is never the case, you can use a bash parameter substitution, that I will append to the answer. – sudodus Aug 12 '22 at 05:30