Created
April 28, 2016 15:52
-
-
Save Rudde/e27e2acd64ed1649873722dc52c5b155 to your computer and use it in GitHub Desktop.
Bash copy tool giving speed and progress bar, working for files and directories built of pv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
hash pv | |
if [ $? -ne 0 ]; then | |
echo "ERROR: Command pv is not available but required, please run: sudo apt-get install pv" | |
exit 1 | |
fi | |
WORKDIR=$(pwd) | |
FROM=$1 | |
TO=$2 | |
TOPATH=$(dirname $(realpath "$TO")) | |
FILENAME=$(basename "$FROM") | |
if [[ ! -d "$FROM" ]] && [[ ! -f "$FROM" ]]; then | |
echo "ERROR: Input: $FROM is neither a file or directory, nothing to do" | |
exit 1 | |
fi | |
if [[ ! -d "$TOPATH" ]]; then | |
if [[ -f "$TO" ]]; then | |
echo "ERROR: Destination: $TO is already a file" | |
else | |
echo "ERROR: Destination: $TO does not exist!" | |
fi | |
exit 1 | |
fi | |
START=$(($(date +%s%N)/1000000)) | |
BYTES=$(du -bs "$FROM" | awk '{print $1}') | |
END=$(($(date +%s%N)/1000000)) | |
echo "Gathering size took $(($END-$START)) milliseconds" | |
if [[ -d "$FROM" ]] && [[ -d "$TO" ]]; then | |
CHILD=$(dirname "$FROM") | |
DIR=$(basename "$FROM") | |
cd "$CHILD" | |
tar -cf - "$DIR" | pv -s $BYTES | tar -C "$TO" -x | |
exit $? | |
fi | |
if [[ -f "$FROM" ]] && [[ -d "$TO" ]]; then | |
pv -s $BYTES "$FROM" > "$TOPATH/$FILENAME" | |
exit $? | |
elif [[ -f "$FROM" ]] && [[ ! -d "$TO" ]] && [[ ! -f "$TO" ]] && [[ -d "$TOPATH" ]]; then | |
pv -s $BYTES "$FROM" > "$TO" | |
exit $? | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment