CAVEAT: you can do the same thing with “lsof” and “fuser”

So you want to see the current files being copied or looked at by cp or rsync (or another program)? So you forgot to do verbose mode and your copy is already running?

You could always restart the copy and have it start back up where it left off… but we all know cp doesnt really do that smoothly, rsync probably does.

Anyhow here is a script that looks at the current open filedescriptors of the pids (of cp, or rsync if you wish) and nicely prints them on the screen. The result is the current files being copied.

Try this: identify the PID of your CP or RSYNC using ps ax . Then do this find /proc/PID/ -ls  Now look at all of the lines that have the words “/fd/” (file descriptors – or the processes currently opened files )and have an arrow pointing “->”. Everything to the right of that error is where the “/fd/”, file descriptor, is reading from or writing to.

My scripts below just automate that

(-CP-)

# Current Files being copied (looks at all cp)
for i in `ps ax | grep "c[p]" | awk '{print $1}'`; do
find /proc/$i/ -ls | grep "/fd/" 
done | cut -f2 -d\> | sort -u

# Current Files being copied (looks at only 1 cp)
find /proc/`ps ax | grep "c[p]" | awk '{print $1}'` -ls | grep "/fd/" | cut -f2 -d\> | sort -u

(- RSYNC-)

# Current Files being copied (looks at all rsync)
for i in `ps ax | grep "rsyn[c]" | awk '{print $1}'`; do
find /proc/$i/ -ls | grep "/fd/" 
done | cut -f2 -d\> | sort -u

# Current Files being copied (looks at only 1 rsync)
find /proc/`ps ax | grep "rsyn[c]" | awk '{print $1}'` -ls | grep "/fd/" | cut -f2 -d\> | sort -u

(-ANY PROGRAM-)

# Current Files being copied (looks at all PROGRAM)
THEPROGRAM="cp";
for i in `ps ax | grep "$THEPROGRAM" | grep -v grep | awk '{print $1}'`; do
find /proc/$i/ -ls | grep "/fd/" 
done | cut -f2 -d\> | sort -u

# Current Files being copied (looks at only 1 PROGRAM)
THEPROGRAM="cp";
find /proc/`ps ax | grep "$THEPROGRAM" | grep -v grep | awk '{print $1}'` -ls | grep "/fd/" | cut -f2 -d\> | sort -u

(-ANY PID-)

# Current Files being copied (looks at only 1 PID)
THEPID="1234";
find /proc/$THEPID -ls | grep "/fd/" | cut -f2 -d\> | sort -u

What’s happening in the scripts?

First do the “Try this” above the scripts.

First we look for the process with grep thru the list of “ps ax“. We dont want to find the entries of “grep” itself looking for the name of the program, so we avoid that using this trick GREP DONT FIND GREP. After that we look for all of the entries that have “/fd/” because that shows the file-descriptors (open files). Then we look for the stuff to the right of the arrow (the arrow has the unique character “>“, nothing else has that character. Everything to the left of the first “>” would be field 1, but we need field 2 which will give us everything to the right of the first arrow “>“)

Leave a Reply

Your email address will not be published. Required fields are marked *