Interesting that this is marked as a duplicate of a question asked 3 years later?
– ChrisOct 10 '18 at 19:53
5 Answers5
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):
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.
– ChrisOct 28 '10 at 14:27
8
@Chris Well, you can always add a function for it. function cprate() {cat "$1" | pv --rate > "$2"}
– Michael MrozekOct 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.
– 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!
– OliverAug 13 '14 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.
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)
– Michael MrozekOct 30 '10 at 01:31
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)
– Mark JeronimusJul 13 '20 at 08: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
This is a neat hack. Although mine shows Every 2s. And I don't think it is accurate.
– GeneCodeMar 22 '18 at 05: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 :)
– SidJSep 26 '18 at 06:48
@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 :-)
– muellethSep 26 '18 at 07: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
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
}