When I use zip, how can I display the overall progress without flooding the command line?
Solution 1
zip
can compress data to standard output. Hence, you can combine it with other tools like pv
:
zip -qr - [folder] | pv -bep -s $(du -bs [folder] | awk '{print $1}') > [file.zip]
Remove one of the -bep
options as your convenience.
Solution 2
If you're ok with using 7z:
7z a output.zip folder/
This will give you a progress bar like this:
Open archive: test.zip
--
Path = test.zip
Type = zip
Physical Size = 232039663
Scanning the drive:
3 folders, 2401 files, 238122225 bytes (228 MiB)
Updating archive: test.zip
Items to compress: 2404
16% 279 U folder/file.txt
Solution 3
I have successfully used the following:
zip -r [target_zip] [folder_to_zip] 2>&1 |
pv -lep -s $(ls -Rl1 [folder_to_zip] | egrep -c '^[-/]') > /dev/null
And this is explained below:
zip -r [target_zip] [folder_to_zip] 2>&1 |
zip recursively into [target_zip] file the [folder_to_zip] redirecting stderr to stdout. Note that stderr will contain one line for each file and directory being processed.
pv -lep -s $(ls -Ral1 [folder_to_zip] | egrep -c '^[-/]') > /dev/null
pipe into pv
the lines with the filenames as they are being output from zip. pv
is operated in line mode (counting progress based on lines and size is also in number of lines to expect – see PV man page -l option).
The total size of lines to expect is gathered by recursively listing (ls) the [folder_to_zip] and counting the lines starting with '-' or 'd', i.e., all the files and directories (remember directories are listed starting with '/' ).
The above provides accurate percentage of completion as the 100% is reached when all files and directories have been processed.
The problem with the answer of pedroapero is that progress is calculated on number of bytes processed (compressed) over the total number of bytes to process (uncompressed). As a result the process will complete at around 30% (depending on the compression rate).
Related videos on Youtube
bug
Updated on September 18, 2022Comments
-
bug over 1 year
A fixed-length progress bar, a file or byte count, or better yet a timer showing the estimated time remaining would be ideal.
zip
's standard behavior seems to be to print a line for every file processed, but I don't want that information overload when I zip thousands of files. I want a guesstimate how long it's going to take.I tried the
-q
(--quiet
) option in combination with-dg
(--display-globaldots
) but that just floods stdout with multiple lines of dots and gives no useful indication.I also tried
-qdgds 10m
as mentioned in the man page, but got the same result.I then tried
-db
(--display-bytes
) and-dc
(--display-counts
) but there doesn't seem to be a global option, so it again prints it for every filename.Lastly, I tried it together with
-q
like-qdbdc
, but that just outputs nothing.Funnily enough, I found a man page on the info-zip site that mentions a
-de
(--display-est-to-go
) option which should "Display an estimate of the time to finish the archiving operation."That sounds exactly like what I want, but the problem is that my version of
zip
does not have that feature. I'm using Ubuntu 14.04.1 64bit, bash-4.3.30(1) and zip-3.00. According to Wikipedia, this is zip's latest stable release.There are unreleased beta versions on the info-zip sourceforge page, but I'd rather not entrust my data to a beta release.
-
Admin about 9 yearsLog the output to a file and use it to provide high level info with
tee
. Before starting zip, make a total count of the files (withls
orfind -type f
) and while it is zipping, read the log file for the number of lines of processed files it already has (withgrep
for the right lines to look at, andwc -l
for the lines count), so your high level info will show something like "234/76438 files processed"; -
Admin about 9 yearsyou can work the timing by considering the total files size and checking the size of the ones that were already processed; but... even files with the same size take different time to be processed so it will always be a wild guess...
-
Admin almost 9 yearsI don't know if you can use stdin when making ZIP files, but if gzip is okay, you can do something like
pv /path/to/file | gzip > /path/to/file.gz
-
-
Jeff over 6 yearsThanks for this! I do this on my mac (brew install pv, brew install coreutils, and replace du with gdu).
-
pedroapero over 3 yearsThanks for your additional reply. That's correct, I guess it all depends of file size distribution, and content entropy.
-
Rodrigo over 3 yearsWhen I finally made zip's -rqdgds option work, it showed each dot on the next line, which is useless if you need more than a few dozen dots. So 7z was indeed the simplest way to go.
-
qwr over 3 years@Rodrigo sometimes the best answer is to just use a different tool.
-
Saddle Point over 3 yearsGreat explanations!
-
X Tian about 3 yearsDoes that show the progress?
-
qwr about 2 yearsdid you answer the wrong question?
-
Admin about 2 yearsYou are probably right. I've reiterated on one of the answers here. So I better find the exact usage of the zip I did and edit my post. Thanks.