LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linux script Limiting Zip archive size (https://www.linuxquestions.org/questions/programming-9/linux-script-limiting-zip-archive-size-911631/)

Nominal Animal 11-09-2011 11:59 AM

Quote:

Originally Posted by giantanakim (Post 4519697)
It split this up into three different zip files. -1 was 1.3G, -2 was 800MB, and -3 was 2.39GB.

Like michaelk said, audio files tend not to compress much.

The script does not group the files based on their compressed sizes, but on their uncompressed size. It will not split a file between two archives, nor will it reorder the files to get better distribution between archives. (Like I said, those options are possible, but result in a much more complex and often very much slower script.)

I suggest you try with a maximum size limit about 400M smaller.

Quote:

Originally Posted by giantanakim (Post 4519697)
Can I run this for another directory? For instance, if I put in ./nominalanimal.sh /home/foo/bar/recordings

You can. The zip files will be created in the current working directory. The zip contents will have a home folder, a foo folder inside that, then a bar folder, and a recordings folder inside that, and then you get the files and subfolders under the recordings folder and its subfolders.

If you want to create a zip file in /home/foo/bar/recordings, containing the files in that directory or its subdirectories, use
Code:

( cd /home/foo/bar/recordings ; /path/to/nominalanimal.sh . )
This way you only get the files and folders under recordings.

The parentheses tell your shell to use a subshell, and the cd will then change the working directory only for the subshell, not for the script or shell you run the above from. Effectively, the working directory is only changed for the nominalanimal.sh script.
Quote:

Originally Posted by giantanakim (Post 4519697)
Can this script be modified so that once a file is added to the zip, the original can be moved to a different directory?

Of course. Look at the zip commands in the script. The "${FILES[@]}" will expand to the list of files contained in the zip archive.

Because the files may reside in subdirectories, a normal copy often does not work, since it will not create the intermediate directories. However, you can use tar. For example, if you insert the following line after each line containing a zip command in the script,
Code:

tar -cf - "${FILES[@]}" | tar -xf - -C where-to-move || exit $?
the files are copied to under where-to-move, keeping the same relative directory structure. You can add
Code:

rm -f "${FILES[@]}"
after that to remove files that were successfully zipped and copied.

Since the above will not remove empty directories, you can replace the last two lines of the script to remove (only) empty directories. It is safe: it will not remove a directory with files (or subdirectories with files), even if the files are "hidden" (not listed in a standard directory listing). The two lines would be changed to
Code:

) || exit $?
find "$@" -depth -type d -print0 | xargs -r0 rmdir &>/dev/null
exit 0

It is safe to change the end of the script, even if you did not do the other changes above. Then it would not move the files, just remove empty directories after zipping the files.


All times are GMT -5. The time now is 06:20 PM.