50

This question already has an answer here:

Is this possible?

marked as duplicate by roaima, GAD3R, sam, lgeorget, Anthon Sep 15 '16 at 11:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Interesting that this is marked as a duplicate of a question asked 3 years later? – Chris Oct 10 '18 at 19:53
49

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

  • 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 Oct 28 '10 at 14:27
  • 8
    @Chris Well, you can always add a function for it. function cprate() {cat "$1" | pv --rate > "$2"} – Michael Mrozek Oct 28 '10 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 Mar 11 '12 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 Aug 13 '14 at 13:31
16

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
9

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
  • This is a neat hack. Although mine shows Every 2s. And I don't think it is accurate. – GeneCode Mar 22 '18 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 Sep 26 '18 at 6:48
  • Also there's a -n option to specify a value larger than 2 secs – SidJ Sep 26 '18 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 Sep 26 '18 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 or ask your own question.