Info: A text-based progress bar for shell scripts and commands in Linux
Purpose: If you are into writing shell scripts and you wish you could display a nice little text progress bar as your shell scripts execute different commands, then your worries are over. In this blog entry we will see how we can implement a text progress bar in our shell scripts using a very handy utility called ‘clpbar‘. Some good reasons to have a progress bar are:
- 1. Let user know how much more they have to wait before a particular task/command gets completed.
- 2. Visually appealing and professional
and there are many more…
Step 1: Download or obtain the source
This utility (as far as I know) is not a part of any standard Linux distribution like Ubuntu, Fedora, Debian, etc. So we will download the utility for the sourceforge website. You will notice that the author has uploaded various formats of binary installation package: “.deb” package for Debian/Ubuntu based systems, “.rpm” for Fedora based systems and also the source tarball if you need to compile it. We will download the “.deb” package and will install on a Debian Lenny system. Either you can manually download the file from your Internet browser or you can simply type this command on your Linux system:
# apt-get update
# apt-get wget
# wget http://downloads.sourceforge.net/clpbar/bar_1.10.9_i386.deb?modtime=1183036896&big_mirror=0
This will download the file into the directory from where you gave the above command.
Step 2: Install the “.deb” file
Once you have downloaded the “.deb” file on your Linux system, install it using dpkg command:
# dpkg -i <file-you-downloaded-from-step1>
Example:
# dpkg -i bar_1.10.9_i386.deb
Step 3: Use it within your shell script or even with standalone along with any command
Once you install finish installation from Step 2, you will now have a binary called “bar”. For example simply type:
# bar
# t 0.0B/s elapsed: 0:00:01
This shows that “bar” is installed properly. Now let’s see this command in some action. Suppose you want to untar a 500+ MB file (say image.tar.bz2) and you would like to see a progress bar and an estimated time of completion. Just give the following command:
#bar image.tar.bz2 | tar xjf -
and you will be able to see something like this:
# bar image.tar.bz2 | tar xjf -
44.2MB at 383.1KB/s eta: 0:05:20 26% [========= ]
The above output tells us the following:
ETA (estimated time of arrival): 5:20
How much is the transfer rate: 383.1KB/s
How much of the file is untar’d: 44.2MB
Note: Do not ignore the “-” sign at the end of command above, otherwise you will get an error message.
Step 4: Use in shell script
Using “bar” utility in shell script is very simple. Here is an example of a bash shell script:
#!/bin/bash
echo "Starting untaring image..."
bar image.tar.bz2 | tar xjf -
echo "Untarring successfully completed"
The above example that we saw above is just a tip of the iceberg. I highly encourage you to read the man page for bar to explore it’s true potential:
# man bar
You can change the color of the foreground and background, change the labels, etc.
Additional Examples
Following are some examples straight from bar’s man page:
Example 1: Using bar to copy a 2.4gb file from a device (in this case a tape drive) to a file, using a 64k buffer.
# bar --in-file /dev/rmt/1cbn --out-file tape-restore.tar --size 2.4g --buffer-size 64k
Example 2: Using bar to copy a 37tb file across the network using SSH.
# ssh remote `dd if=file` | bar --size 37t > file
Example 3: Using bar inside a tar-pipe command:
Normal tar-pipe command might be:
# (cd /some/dir/somewhere && tar -cf - *) | (cd /some/other/dir && tar -xBpf -)
3a: Using bar within the tar-pipe:
# cd /some/dir/somewhere && tar -cf - *) | bar | (cd /some/other/dir && tar -xBpf -)
3b: Using bar with the –size option in a tar-pipe:
# du -sk /some/dir/somewhere
6281954 /some/dir/somewhere
# (cd /some/dir/somewhere && tar -cf - *) | bar --size 6281954k | (cd /some/other/dir && tar -xBpf -)
Example 4: Using bar on a regular file. (Note that the –size option is not needed here, as bar will retrieve the file size itself.)
# bar --in-file ./file | ssh remote ` cd /some/dir && dd of=file`
Example 5: Generating a 512k file of random data
# dd if=/dev/random bs=1024 count=512 | bar -s 512k -of ./random
As you can see from the above examples, that bar is a very powerful and useful tool.
Enjoy Bar’ing….:)
As usual, please leave a comment/feedback if you have any. Comments encourages bloggers to post more and keep their spirits high.
Also don’t forget to rate this post below.

Free Email Subscription









January 29th, 2009 at 2:18 pm
Great-great, but you won’t be able to send your script to a friend, since he doesn’t necessarily has that bar application.
Reply to this comment
Kushal Reply:
January 30th, 2009 at 10:10 pm
Hi kblcuk,
Thanks. Yes you are right your friend needs to have the bar utility. One thing you can do is you can insert a check in your shell script which will first check if the system (say your friend’s machine) has bar utility installed or not. If it does not then provide an explicit message saying that “you need ‘bar’ utility to execute this script. Do you want to download (y/n)?” After that you can just do “wget” and dpkg -i command and it will install it on your friends computer.
Hope that helps.
Reply to this comment
February 12th, 2009 at 1:41 pm
Hi Kushal,
a good utility. I have a script that is more like a build script that compiles and installs multiple sources on after the other. It is something like running a make. How do you make use of bar for this ?. I know that the entire script takes 2 hours in a normla cpu load to run.
Reply to this comment
February 12th, 2009 at 1:50 pm
Is it possible to use for tracking build progress when you generally give a make for a big project ?
Reply to this comment
Kushal Reply:
February 12th, 2009 at 7:23 pm
Hi Srinivas,
Your question made me wondered if it is really possible to use a make command. I think it might not be possible because in order for bar to work it needs to know the estimated size of the operation. Based on that it gives the estimated size. With make command (let’s say Kernel make) it is really hard to know the the “size” or even define what the “size” is.
However, I think there might be a way to trick bar to work with make. I suggest you read the man page because it has some good examples and extensive documentation. Please let us know in case if you find a solution for it. I will update the post.
Thank you.
Reply to this comment
April 15th, 2009 at 8:32 am
Your comment has not been approved by the moderator because of possible spam detection by our spam filtering software. If you think your comment got mis-categorized as spam, please reply to this e-mail
or
contact the moderator at: http://blogs.koolwal.net/contact-me/
Reply to this comment
November 17th, 2009 at 1:48 am
Hi,
how can i cross compile this tool to ppc-linux using ./configure?
Reply to this comment