Skip to content

Instantly share code, notes, and snippets.

@yangadam
Created October 15, 2015 15:16
Show Gist options
  • Save yangadam/e066732c32e6525b297f to your computer and use it in GitHub Desktop.
Save yangadam/e066732c32e6525b297f to your computer and use it in GitHub Desktop.
#!/bin/bash
# collect.sh
#Help infomation
function usage
{
echo "usage: collect.sh [[-l file] [FILE]... | [-h]] "
}
#Move action
function move
{
mv "$1" "$2"
readlink -f "$1" >> "$logfile"
readlink -f "$2" >> "$logfile"
}
#Init variables
bindir="$HOME/bin"
libdir="$HOME/lib"
srcdir="$HOME/src"
incdir="$HOME/inc"
logfile="$HOME/organize.log"
line="$(echo)"
#Process option parameter
if [ "$1" != "" ]; then
case $1 in
-f | --file ) shift
logfile=$1
shift
;;
-h | --help ) usage
exit
;;
esac
fi
# Set directories
if [ $# -eq 0 ]; then
read -t 10 -p "Please input directories where you collect files(separated by spaces in one line)." directories
else
directories=$@
fi
# Ask for target folders
choice="N"
read -t 10 -p "Would you like another directories to put objects?(Y/N)" choice
if [ $choice = 'Y' ] || [ $choice = 'y' ]; then
read -t 10 -p "Please input the target directory." dir
bindir="$dir/bin"
libdir="$dir/lib"
srcdir="$dir/src"
incdir="$dir/inc"
fi
start=`date +%s`
#Create directories if not exists
mkdir -p "$bindir"
mkdir -p "$libdir"
mkdir -p "$srcdir"
mkdir -p "$incdir"
if [ -f "$logfile" ]; then
echo -n > "$logfile"
else
touch "$logfile"
fi
dircnt=0
output=""
#Traversal all source folder
for curdir in $directories
do
((dircnt++))
filecnt=0
if [ -d $curdir ]; then
for curfile in $(ls -p "$curdir")
do
if [ -x "$curdir/$curfile" ]; then
move "$curdir/$curfile" "$bindir/$curfile"
((filecnt++))
fi
case $curfile in
*.c|*.cc|*.cpp|*.cxx ) move "$curdir/$curfile" "$srcdir/$curfile"
((filecnt++))
;;
*.h|*.hxx ) move "$curdir/$curfile" "$incdir/$curfile"
((filecnt++))
;;
lib*.* ) move "$curdir/$curfile" "$libdir/$curfile"
((filecnt++))
;;
esac
done
output="$output\n$filecnt files were moved from $curdir"
fi
done
end=`date +%s`
echo "The organization took $((end-start)) seconds."
echo "$dircnt directories wre proceeded."
echo -e "$output"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment