Mikael's blog

A developers sixth time trying to maintain a blog

Progress at Last

Sometimes you need more than your operating system gives you.

That's when a text editor comes in handy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
 
EXPECTED_ARGS=2
E_BADARGS=65
E_BADPATH=66
 
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {source} {dest}"
  exit $E_BADARGS
fi
 
if [[ ! -f "$1" ]]; then
    echo "Source file does not exist or is not a regular file."
    exit $E_BADPATH
fi
 
DESTSIZE=`du -b "$1" | awk '{print \$1; }'`
 
DESTFILENAME=`basename "$1"`
 
if [[ -d "$2" ]]; then
    DESTPATH="$2/$DESTFILENAME"
else
    DESTDIR=`dirname "$2"`
    if [[ ! -d "$DESTDIR" ]]; then
        echo "Dest dir does not exist."
        exit $E_BADPATH
    fi
    DESTPATH="$2"
fi
 
 
cat "$1" | pv -s $DESTSIZE -p -e -r > "$DESTPATH"
 
exit 0

Copying large files to my NAS becomes so much more fun when I actually KNOW that it's doing what it should. Progress bars FTW!

UPDATE: Now it's actually working for more than one case. :)

by Mikael Lofjärd
Comments
by Mikael Lofjärd on
Feel free to insert quotation marks where appropriate if you want to handle file names with spaces in them. :)
by Mikael Lofjärd on
Now it should work with file names with spaces and copying to folders as well. Stupid of me not to test all cases before on real data before I uploaded it. :)

Name:

Home page:

Comment: