[ale] Neat code clip: cp progress bar

Michael B. Trausch mike at trausch.us
Fri Mar 5 11:47:45 EST 2010


On 03/05/2010 09:04 AM, Brandon Wood wrote:
> I saw this post on Hacker News today and thought that it deserved to be
> shared:
>
>
>     Can you get cp to give a progress bar like wget?
>     <http://chris-lamb.co.uk/2008/01/24/can-you-get-cp-to-give-a-progress-bar-like-wget/>
>
> via Hacker News <http://news.ycombinator.com/> on 3/4/10
>
> <  zed0>  can you get cp to give a progress bar like wget?
>
> Damn right you can.
>
> #!/bin/sh
> cp_p()
> {
>     strace -q -ewrite cp --"${1}"  "${2}"  2>&1\
>        | awk'{
>          count += $NF
>              if (count % 10 == 0) {
>                 percent = count / total_size * 100
>                 printf"%3d%% [", percent
>                 for (i=0;i<=percent;i++)
>                    printf"="
>                 printf">"
>                 for (i=percent;i<100;i++)
>                    printf"  "
>                 printf"]\r"
>              }
>           }
>           END { print""  }'  total_size=$(stat -c'%s'  "${1}")  count=0
> }
>
> In action:
>
> % cp_p /mnt/raid/pub/iso/debian/debian-2.2r4potato-i386-netinst.iso /dev/null
> 76% [===========================================>                     ]
>

I've often wondered why there isn't an option in GNU's cp command to do 
that.  If one is copying a very large file to a very slow filesystem, 
such as a DVD-RAM or an old flash drive or a network filesystem mounted 
over the Internet, something like that is awfully handy.

Thanks for sharing this.  I'm _so_ adding it to my shell's start-time 
scripts!  It can't quite stand-in for cp directly, but I have to wonder 
if it would be useful to extend it a little bit and be able to pass 
arguments to cp, and show progress for each individual file.  Even if 
not, though, it's easy enough to modify for one-off use if options are 
required.

	--- Mike

-- 
Michael B. Trausch                                    ☎ (404) 492-6475


More information about the Ale mailing list