I need to recursively zip the individual files itself in the directory path and its sub folders without zipping the folders or directory. I also need to exclude certain file types (more then one).

How can I do this in Linux using ZIP?

Here is the modified script (thanks Ravexina).

echo "hello world"
#cd "${dir}"
#cd $(echo $dir | tr -d '\r')
cd "$dir"
find -not -iname '*.txt' -type f | while read -r file_name; \
do zip "$(dir "$file_name")".zip "$file_name" ; done

Output: line 8 zip command not found.

  • Do you want one *.zip file with all the files below a certain root directory or several *.gz files (one for each file)? // What should happen when two files in different directories have the same name? – PerlDuck Sep 1 '18 at 15:48
  • No. I need a zip file for each file in the directory or sub folders but not zip the directory and sub folder. – Femina Jaffer Sep 1 '18 at 22:22
  • Thank you Ravexina. I'm sorry if this is a silly question, would you mind clarifying what the "realpath" or $file_name" would be in an example. I tried this syntax and I get the error "line 8 zip not found". I'll edit my original question and place the script in there. – Femina Jaffer Sep 3 '18 at 13:44
  • @FeminaJaffer seems you don't have zip installed, install it using: sudo apt install zip – Ravexina Sep 3 '18 at 15:42

You can do it with something like this:

find -not -iname '*.mp3' -type f | while read -r file_name; \
do zip "$(realpath "$file_name")".zip "$file_name" ; done

It will zips all files in their directory each separately other that of "mp3" files.

You can exclude other type of files too, for example, zip and mp3 files:

find -not -iname '*.mp3' -not -iname '*.zip' -type f

So this structure:

└── a
    ├── 1
    │   └── 1.mp3
    └── 2
        ├── 1.jpg
        ├── 1.mp3
        └── 1.mpv

would become:

└── a
    ├── 1
    │   └── 1.mp3
    └── 2
        ├── 1.jpg
        ├── 1.jpg.zip
        ├── 1.mp3
        ├── 1.mpv
        └── 1.mpv.zip
  • Thank you so much I will give this a try. If it is all files than for filename I can use the wildcard option, such as . correct? How would I add a counter...lets say I want the number of total directories and just zip files for 10 directories/subfolders files at a time? – Femina Jaffer Sep 1 '18 at 22:26
  • It's not that smart of script, you can change the while with a for or add a counter as you already said your self and break the while after it reaches a number. check this out: stackoverflow.com/questions/13638670/… – Ravexina Sep 2 '18 at 15:35
  • 1
    @FeminaJaffer You can also accept the answer by clicking on the gray tick if you think it was helpful to you. – Ravexina Sep 2 '18 at 15:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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