Thu 2010-01-07 ( En pr )

When cp -r takes a lot of time, I want to have an estimate how long it will take. My first approach was based on checking the size of the copied data repeatedly with du -sh.

This is obviously now very clean. Marco (a fellow student) told me that cp reacts to the SIGINFO signal by printing the progress to stdout. The manpage says:

If cp receives a SIGINFO (see the status argument for stty(1)) signal, the current input and output file and the percentage complete will be written to the standard output.

This is what the output looks like:

$ cp3 large.file large.file.copy 
large.file -> large.file.copy  42%
large.file -> large.file.copy  88%

Here’s the code:

#!/usr/bin/env ruby
puts <<USAGE unless ARGV.size == 2
cp progress printer (public domain)
Usage:
  cp3 FROM TO
USAGE
from, to = *ARGV
copy = IO.popen "cp -r #{from} #{to}", 'r'
Thread.new do
  loop do
    sleep 1
    Process.kill 'INFO', copy.pid
  end
end
Process.wait
puts

Say something! / Sag was!

No markup, just plain monospace text. / Kein Markup, nur Normschrift-Klartext.