Display transfer speed when performing "cp" from the command line?

  • Is this possible?

    Interesting that this is marked as a duplicate of a question asked 3 years later?

  • 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 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 Well, you can always add a function for it. `function cprate() {cat "$1" | pv --rate > "$2"}`

    `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.

    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!

  • 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
    

    `pv` is Pipe Viewer, and is pretty awesome. @Patrick, please expand your post with an explanation.

    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`)

  • 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.

    Thanks - if we have already started the command this really is the only way to avoid checkign the file size manually every few secs :)

    Also there's a -n option to specify a value larger than 2 secs

    @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 :-)

  • Hi Another way to show the transfer speed is to use scp on localhost like this:
    scp -rv src_folder [email protected]:/dest_folder

  • 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:

    [email protected]:~$ dd if=/dev/zero of=/tmp/zero bs=1 count=50000000 &
    [email protected]:~$ 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
    [email protected]:~$ killall dd; rm /tmp/zero
    

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM