0

I'm trying to zip all the files within a directory which contains .py files individually. But after zipping the files the output that I'm seeing is .py.zip vs just .zip

Here's the one liner command that I'm trying to execute.

cd scripts/python/
for i in *; do zip $i.zip $i; done
shellter
  • 36,525
  • 7
  • 83
  • 90
Karthik
  • 339
  • 1
  • 7
  • 24
  • 1
    unclear if you want to zip all scripts into one file, or zip them individually. Please update your Q with sample list of files (1-3 should be enough) and what you want as a final result. I'll delete this comment when you do so. Good luck. – shellter Jun 08 '19 at 00:43
  • 3
    `I really hate this dumb machine, I wish that they would sell it; it never does quite what I want, but only what I tell it. ` Well, if you don't want the full name, strip the extension off before you decide to name it. – tink Jun 08 '19 at 00:44
  • Also, quote your variables to prevent unwanted globbing or word splitting. – Perplexabot Jun 08 '19 at 00:45
  • 2
    Is there any reason `.py.zip` is undesirable? If the files are later moved or copied elsewhere, it seems having the `".py.zip"` would be much more informative than just `".zip"` (and avoid the need to call `unzip -l` to find out what is in the .zip file) – David C. Rankin Jun 08 '19 at 01:00
  • Possible duplicate of [Extract filename and extension in Bash](https://stackoverflow.com/q/965053/608639), [Extract file basename without path and extension in bash](https://stackoverflow.com/q/2664740/608639), [How to automatically remove the file extension in a file name](https://stackoverflow.com/q/13337953/608639), etc. – jww Jun 08 '19 at 05:45

1 Answers1

4

This is what you are looking for:

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

Explanation

${i%.*}: This makes use of Bash's built in parameter expansion. Here it tries to match everything after %. If it does find a match, it uses everything before the match. https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion for more information.

Perplexabot
  • 1,852
  • 3
  • 19
  • 22