-
Notifications
You must be signed in to change notification settings - Fork 8
/
usbcp.sh
61 lines (50 loc) · 1.44 KB
/
usbcp.sh
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
function print_usage {
echo " Copies the files of [sourcedir] in alphabetical order"
echo " to a same-named directory created in [destdir]."
echo " Usage: $(basename "$0") [sourcedir] [destdir]"
}
# check for correct number of parameters
if [ "$#" != 2 ]; then
print_usage
exit 1
fi
# check if source is a directory
if [ ! -d "$1" ]; then
print_usage
echo " ERROR: [sourcedir] isn't a directory"
exit 1
fi
# check if dest is a directory
if [ ! -d "$2" ]; then
print_usage
echo " ERROR: [destdir] isn't a directory"
exit 1
fi
# short name of source folder, we'll make a new one of these later
foldername=$(basename "$1")
# full path of destination to make new folder in
fulldest=$(readlink -f "$2")
# change to dir with files to make globbing easier
cd "$1"
# if a glob is empty, don't make an array
shopt -s nullglob
# find all files in current dir and put them in an array
arr=( * )
# if the array is empty
if [ ${#arr[@]} == 0 ]; then
echo " ERROR: Source dir contains no files!"
exit 1
fi
# sort array - http://stackoverflow.com/a/11789688/1422582
IFS=$'\n' sorted=($(sort <<<"${arr[*]}"))
unset IFS
# create destination dir from source dir name
newdir="$fulldest/$foldername"
echo " Creating directory \"$newdir\""
mkdir -p "$newdir"
# copy sorted files
for ((i=0; i<${#sorted[@]}; i++)); do
echo " Copying \"$(pwd)/${sorted[$i]}\""
cp "${sorted[$i]}" "$newdir/${sorted[$i]}"
done