How to Copy a File and Get a Progress Bar, in Linux

Today I had to copy a really big file (in this case, a Ghost image) from a Linux box to a Windows box, using Samba. My first instinct was to use the cp command. The problem with cp is that it does not show you any sort of progress (I couldn’t find any relevant option in the cp man page). What I wanted was a progress bar display like the one wget displays.

I did some googling and found a few decent candidates:

While all of them might work, the only one I tried was pv and I loved it. It did exactly what I wanted. The result was sort of like “cp with a progress bar”.

Under Debian Linux (sarge version) pv was easy enough to install: apt-get install pv.

After reading the pv man page, I was still a little unclear on exactly how to use it to copy files (yes, I guess I’m dumb). However, after a little bit of experimentation, I figured it out:

me@mycomputer:/home/ghost$ pv bigfile > /mnt/ghost_on_windows_box/bigfile

The source file is bigfile, in the current directory. The target file is bigfile, on a share called /mnt/ghost_on_windows_box. Obviously, the share is a SMB share on a Windows box. On the Linux box, I mounted that share with the CIFS file system (via an entry in /etc/fstab).

Here is what the output looked like, at three different stages:

205MB 0:00:17 [8.03MB/s] [=======>                           ] 25% ETA 0:00:49
436MB 0:00:31 [40.7MB/s] [=================>                 ] 54% ETA 0:00:25
801MB 0:00:44 [  18MB/s] [=================================>] 100% ETA 0:00:00

Just confirm that the copy worked, I ran md5sum on both the source file and the target file. It indeed worked.

One minor problem I noticed, however, is that it took several seconds to get my command prompt back after pv showed the copy was 100% complete. After some investigation, it seems that it wasn’t pv’s fault. Rather, it had something to do with Samba. I’m not even sure if it was the Linux Samba client’s fault or the Windows SMB server’s fault.

I confirmed that it was not a pv problem by copying the file to a local folder on the Linux box. In that case, pv returned control to the shell immediately after the progress bar went to 100%.

As a workaround for this Samba copying problem, I added an extra command to the original command:

me@mycomputer:/home/ghost$ pv bigfile > /mnt/ghost_on_windows_box/bigfile; echo ***DONE***

Now, several seconds (or even a minute or so) after the progress bar reaches 100%, I get the message ***DONE*** in the console. Then I know it is really done.

For my needs, pv works great.

If this tip helped you, please leave me a comment!




3 comments to How to Copy a File and Get a Progress Bar, in Linux

  • Jay

    Ah, that is exactly what I was looking for while transferring a 5GB file! Thanks for that!

  • Jonathan Ng

    Exactly what I was trying to figure out. Thanks! :)

  • Sebastian

    Sadly, the more complex a filename is, the less fun is this command, because it forces you to type it twice.
    Although this is not an issue as long as files are named a, b or bigfile, it can get nasty with spaces and all the escaping needed.
    Is there any way to change the behaviour towards, say pv bigfile > /path/to/target/, so target/ will contain bigfile?

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>