think i wrote the most useless script i’ve ever produced today. the embarrassing thing was i didn’t realize what the hell i was doing til it was almost done.. to make some use of it i figured i’d comment it and put it here.
#!/bin/bash
# check if number of arguments ($#) is less than (-lt) 2
if [ $# -lt 2 ]; then
echo "usage: $0 dst src [src1 src2 ...]"
# wrong usage means non zero exit code
exit 1
fi
# make sure first argument ($1) is a directory
if [ ! -d $1 ]; then
echo "target dir must exist!"
exit 1
fi
# store arguments to script in args (only reason is i dunno
# how to do the following line with $@).
args=$@
# loop through args-array from element 2 to <last>
for a in ${args[@]:2}; do
if [ ! -d $a ]; then
echo "\"$a\" is not a directory, skipping.."
# if it's not a dir skip this iteration
continue
fi
echo "creating dir \"$1/$a\""
mkdir "$1/$a"
# check exit code of last call (mkdir), if it's not equal (-ne)
# to 0 we've got an error.
if [ $? -ne 0 ]; then
echo "could not create \"$1/$a\", skipping.."
continue
fi
# list all files (-A), including symbolic links (-P) in $a, one per line (-1), with trailing / on dirs (-p) |
# select the ones with without trailing / |
# make xargs replace {} in the cp-call with result
ls -1APp $a | grep '[^/]$' | xargs -I '{}' cp "$a/{}" "$1/$a/{}"
# list files in $a as above but recursively (-R) |
# select the lines containing a : |
# remove the : and loop thru the result
for b in `ls -ARPp $a | grep ":" | sed -e 's/://'`
do
mkdir "$1/$b"
if [ $? -ne 0 ]; then
echo "could not create \"$1/$b\", skipping.."
continue
fi
ls -1APp $b | grep '[^/]$' | xargs -I '{}' cp "$b/{}" "$1/$b/{}"
done
done
exit 0
notice how lines 23-46 can be replaced by
cp -R "$a" "$1/."
// sluggo