52

Is this possible?

1
  • 2
    Interesting that this is marked as a duplicate of a question asked 3 years later?
    – Chris
    Commented Oct 10, 2018 at 19:53

5 Answers 5

53

The standard coreutils cp command doesn't support this. There's a Gentoo patch floating around that adds it for different versions, although it's not included in Gentoo anymore for some reason; the version for coreutils 6.10 is in their bugzilla, and I'm sure there are lots of others around.

If you don't want to patch cp, you need to use some other command. For example, rsync has a --progress flag, so you can do:

rsync --progress source destination

If instead of copying you cat the data and then redirect stdout to the destination (i.e. cat source > destination), then you can use a program that measures pipe throughput and insert it in the middle (cat source | SOME-PROGRAM > destination); there are a couple mentioned in this related question. The one I recommended there was pv (Pipe Viewer):

Screenshot of pv from the pv homepage

If you give it the --rate flag it will show the transfer rate

4
  • Very interesting, although not worth the effort. I merely would like cp <source> <dest> to provide a rate similar to how it would report transfer rate using the GUI. Not worth the effort to type that much text just to see the transfer speed. Thanks though.
    – Chris
    Commented Oct 28, 2010 at 14:27
  • 8
    @Chris Well, you can always add a function for it. function cprate() {cat "$1" | pv --rate > "$2"} Commented Oct 28, 2010 at 14:45
  • pv seems good, but I tried it for the same reason as the poster (progress/rate when copying to nfs), where the file gets crated in tmp and transferred afterwards. So, instead of nfs I have to use smb to see progess and rates.
    – user16487
    Commented Mar 11, 2012 at 15:45
  • 1
    Using "cat" is a very dangerous method, I have experienced that cat on some AIX Systems will cut out what they interpret as garbage. Depending on the character set you chose by default. I would agree totally with rsync!
    – Oliver
    Commented Aug 13, 2014 at 13:31
20

I find that using pv in this manner works well for that purpose

pv -p file1 > file2

The -p switch shows the file transfer progress. To see the transfer speed, add the -r switch. If you want to see the average transfer rate over time, you can use the -a switch.

pv -pra file1 > file2
5
  • 1
    pv? <filler text>
    – Chris
    Commented Oct 29, 2010 at 17:34
  • 3
    pv is Pipe Viewer, and is pretty awesome. @Patrick, please expand your post with an explanation. Commented Oct 29, 2010 at 18:55
  • 2
    This is the same command I mentioned in my answer, although I didn't realize it takes a filename argument (I did cat file | pv) Commented Oct 30, 2010 at 1:31
  • Good explanation: catonmat.net/blog/unix-utilities-pipe-viewer
    – Kijewski
    Commented Nov 25, 2011 at 1:40
  • It seems to measure only the read speed and not the write speed, as it writes only to cache (even for a several GB sized file) Commented Jul 13, 2020 at 8:21
10

I know this is rather old, but...

If you do not actually want to display the rate, but only want to watch if something is happening on copying of a large file, you can also just use the watch command (also works with mv):

cp /path/to/myfile /path/to/target/myfile

Then, in another shell, or pushing the copy-command to the background (e.g. with Ctrl + Z followed by bg), you can check the result with:

watch "ls -sh1 /path/to/target"

This will continuously update the output of the ls command update (by default every 2.0s), displaying something like:

Every 2.0s: ls -sh1 /path/to/target                                      
Tue Jan 12 15:02:45 2016

total 1.1G
4.0K data
130M tmp1.txt
137M tmp2.txt
151M tmp3.txt
168M tmp4.txt
162M myFile
4
  • 1
    This is a neat hack. Although mine shows Every 2s. And I don't think it is accurate.
    – GeneCode
    Commented Mar 22, 2018 at 5:54
  • Thanks - if we have already started the command this really is the only way to avoid checkign the file size manually every few secs :)
    – SidJ
    Commented Sep 26, 2018 at 6:48
  • Also there's a -n option to specify a value larger than 2 secs
    – SidJ
    Commented Sep 26, 2018 at 6:50
  • @GeneCode -n switch in the watch command allows to set the update interva in secondsl (also fractions like 0.5 are usually possible). the -h in the ls command makes output "human readable"... you can leave that out if you want byte counts :-)
    – muelleth
    Commented Sep 26, 2018 at 7:17
5

Hi Another way to show the transfer speed is to use scp on localhost like this:
scp -rv src_folder user@localhost:/dest_folder

1

Here is a script that uses du to monitor throughput. This is more application agnostic and is also referenced in https://unix.stackexchange.com/a/301490/183269. Execute the script on the destination host.

monitorio () {
# show write speed for file or directory
    interval="10"
    target="$1"
    size=$(du -ks "$target" | awk '{print $1}')
    firstrun="1"
    echo ""
    while [ 1 ]; do
        prevsize=$size
        size=$(du -ks "$target" | awk '{print $1}')
        #size=$(ls -l "$1"  | awk '{print $5/1024}')
        kb=$((${size} - ${prevsize}))
        kbmin=$((${kb}* (60/${interval}) ))
        kbhour=$((${kbmin}*60))
        # exit if this is not first loop & file size has not changed
        if [ $firstrun -ne 1 ] && [ $kb -eq 0 ]; then break; fi
        echo -e "\e[1A $target changed ${kb}KB ${kbmin}KB/min ${kbhour}KB/hour size: ${size}KB"
        firstrun=0
        sleep $interval
    done
}

example use:

user@host:~$ dd if=/dev/zero of=/tmp/zero bs=1 count=50000000 &
user@host:~$ monitorio /tmp/zero
/tmp/zero changed 4KB 24KB/min 1440KB/hour size: 4164KB
/tmp/zero changed 9168KB 55008KB/min 3300480KB/hour size: 13332KB
/tmp/zero changed 9276KB 55656KB/min 3339360KB/hour size: 22608KB
/tmp/zero changed 8856KB 53136KB/min 3188160KB/hour size: 31464KB
^C
user@host:~$ killall dd; rm /tmp/zero

Not the answer you're looking for? Browse other questions tagged .