Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to create individual zip files for each .txt file it finds and move them after

Tags:

shell

unix

I need a shell script that basically does this:

  • Searches in a folder for all the txt files, and for each one it finds, creates a individual zip file with the same of the txt file it found + .zip.
  • After that moves the created zip file to the txt file.

Basically its a script to substitute a list of txt files for its zip equivalent but keeping the same name.

I've have used find to find the files that I want to zip:

find . -name '.txt.' -print

The Results are:

./InstructionManager.txt.0
./InstructionManager.txt.1

Those are the files I want to zip individually (of course they will be a lot more), but don't know how to use them as arguments individually for make commans like:

zip ./InstructionManager.txt.0.zip ./InstructionManager.txt.0
mv ./InstructionManager.txt.0.zip ./InstructionManager.txt.0
zip ./InstructionManager.txt.1.zip ./InstructionManager.txt.1
mv ./InstructionManager.txt.1.zip ./InstructionManager.txt.1

Any Ideas? And no, i don't want a zip with all the files :S Thanks

like image 331
catteneo Avatar asked Sep 07 '12 15:09

catteneo


People also ask

How do I zip multiple files individually?

Right-click on the file or folder. Select “Compressed (zipped) folder”. To place multiple files into a zip folder, select all of the files while hitting the Ctrl button. Then, right-click on one of the files, move your cursor over the “Send to” option and select “Compressed (zipped) folder”.

How do I zip a file in shell script?

Syntax : $zip –m filename.zip file.txt 4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.

How do I make a ZIP file automatically?

Find and select the files you want to compress in File Explorer, then right-click and choose Send to > Compressed (zipped) folder. A new folder appears next to the original files with a big zipper on it, indicating that it's zipped. The new ZIP file automatically uses the name of the last file you zipped.

How do you make a batch file that uses a zip folder?

Simply select All file types, add . bat after filename, and click the Save button. Now, go to the created batch file and double-click on it to execute the zipping script. It will create an archive folder in the specified location.


2 Answers

find . -name '*.txt.*' -print -exec zip '{}'.zip '{}' \; -exec mv '{}'.zip '{}' \;
  • Find the .txt files
  • The first -exec zips the files
  • The second -exec renames the zipped files to the original names

Note that this procedure overwrites the original files. To be sure that the new files are actual zip files, you can do:

file InstructionManager.txt.*

Which should return:

InstructionManager.txt.0: Zip archive data, at least v1.0 to extract
InstructionManager.txt.1: Zip archive data, at least v1.0 to extract
like image 81
Sicco Avatar answered Sep 23 '22 13:09

Sicco


Using find and zip:

find . -name '*.txt.*' -exec zip '{}.zip' '{}' \;
like image 27
perreal Avatar answered Sep 24 '22 13:09

perreal