How to check progress of running cp?

On recent versions of Mac OS X you can just hit CTRL+T to see progress. From the OSX 10.6 man page for cp(1):

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

Hitting CTRL+T is equivalent to signaling the current process with SIGINFO on BSD-ish machines, including OSX.

This works for dd(1) as well.

I don't think Linux has this SIGINFO mechanism, and don't see anything in the GNU man page for cp(1) about signals that can be used to report progress.


Yes, by running stat on target file and local file, and get a file size,

i.e stat -c "%s" /bin/ls

And you get the percentage of data copied by comparing the two value, that's it

In a very basic implementation that will look like this:

function cpstat()
{
  local pid="${1:-$(pgrep -xn cp)}" src dst
  [[ "$pid" ]] || return
  while [[ -f "/proc/$pid/fd/3" ]]; do
    read src dst < <(stat -L --printf '%s ' "/proc/$pid/fd/"{3,4})
    (( src )) || break
    printf 'cp %d%%\r' $((dst*100/src))
    sleep 1
  done
  echo
}

When you're copying a lot of files, du -s /path/to/destination or find /path/to/destination | wc -l gives you an idea of how much has already been done.

You can find out which file is being copied with lsof -p1234 where 1234 is the process ID of cp. Under many systems, pgrep -x cp reports the process IDs of all running processes named cp. This may not be very useful as the order in which the files inside a given directory are copied is essentially unpredictable (in a large directory under Linux, ls --sort=none will tell you; with a directory tree, try find).

lsof -p1234 also tells you how many bytes cp has already read and written for the current file, in the OFFSET column.

Under Linux, there are IO usage statistics in /proc/$pid/io (again, use the PID of the cp process for $pidf). The rchar value is the total number of bytes that the process has read, and wchar is the number of bytes that the process has written. This includes not only data in files but also metadata in directories. You can compare that figure with the approximate figure obtained with du /path/to/source (which only counts file data). read_bytes and write_bytes only include what has been read or written from storage, i.e. it excludes terminal diagnostics and data already in cache or still in buffers.

Tags:

Cp