When scripting in bash or any other shell in *NIX, while running a command that will take more than a few seconds, a progress bar is needed.
For example, copying a big file, opening a big tar file.
What ways do you recommend to add progress bars to shell scripts?
You may also be interested in how to do a spinner:
Can I do a spinner in Bash?
could create a function that draws this on a scale say 1-10 for the number of bars :
This lets you visualize that a command is still executing:
This will create an infinite while loop that executes in the background and echoes a "." every second. This will display
.
in the shell. Run thetar
command or any a command you want. When that command finishes executing then kill the last job running in the background - which is the infinite while loop.Many answers describe writing your own commands for printing out
'\r' + $some_sort_of_progress_msg
. The problem sometimes is that printing out hundreds of these updates per second will slow down the process.However, if any of your processes produce output (eg
7z a -r newZipFile myFolder
will output each filename as it compresses it) then a simpler, fast, painless and customisable solution exists.Install the python module
tqdm
.Help:
tqdm -h
. An example using more options:As a bonus you can also use
tqdm
to wrap iterables in python code.https://github.com/tqdm/tqdm/blob/master/README.rst#module
Here is how it might look
Uploading a file
Waiting for a job to complete
Simple function that implements it
You can just copy-paste it to your script. It does not require anything else to work.
Usage example
Here, we upload a file and redraw the progress bar at each iteration. It does not matter what job is actually performed as long as we can get 2 values: max value and current value.
In the example below the max value is
file_size
and the current value is supplied by some function and is calleduploaded_bytes
.GNU tar has a useful option which gives a functionality of a simple progress bar.
(...) Another available checkpoint action is ‘dot’ (or ‘.’). It instructs tar to print a single dot on the standard listing stream, e.g.:
The same effect may be obtained by: