Tuesday, July 28, 2009

Progress viewer for local cp

As I mentioned I have been moving around large files and I thought it would be cool to have a progress bar for local copies in a Bash shell. After a bit of digging around, I found out about a program called pv, which stands for Pipe Viewer. Basically you can stick pv in between commands to see how much and how fast data is passing through. For a local cp, it's as simple as this:

pv /path/to/file > /path/to/copied/file

For a little more complexity, you can make a function out of it, and place it in ~/.bashrc to be sourced automatically:


progcp() {

if [ -d "$1" ]; then
echo "Input is a directory. Exiting."
return
fi

if [ -d "$2" ]; then
OUTFILE="$2/"`basename "$1"`
else
OUTFILE="$2"
fi

pv "$1" > "$OUTFILE"
}


I'm interested to know how to add normal command-line switches to a Bash function, so you could add flags like -r (recursive), or some of pv's switches, but this function works well for a single file local copy, such as a movie or an iso. Just call it like this:

progcp /path/to/file /path/to/output/file/or/directory

No comments:

Post a Comment