Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #this script copies non-empty directories (folders) to another directory
- #flag for executing the search loop, yes by default
- dome=1
- #counters to show stats at the end
- emptycount=0
- fullcount=0
- errcount=0
- #set the source and destination for copying
- if [[ $# = 0 || $1 = '-h' ]]
- then
- dome=0
- echo "this script copies non-empty directories (folders) to another directory"
- echo "it requires 1 or 2 argument:"
- echo " copy-non-empty.sh [source] destination"
- echo "if the source is not specified, the current directory is searched"
- echo
- elif [[ $# = 1 ]]
- then
- dest=$1
- src=`pwd`
- echo -n "copying from" $src "to" $dest "proceed [Y/n]? "
- read selection
- if [[ ${selection,,} = n ]]
- then
- dome=0
- fi
- elif [[ $# -gt 1 ]]
- then
- dest=$2
- src=$1
- echo -n "copying from" $src "to" $dest "proceed [Y/n]? "
- read selection
- if [[ ${selection,,} = n ]]
- then
- dome=0
- fi
- fi
- #the actual loop
- if [[ $dome = 1 ]]
- then
- for folder in `ls $src`
- do
- if [[ -d $src/$folder && `ls $src/$folder | wc -l` -gt 0 ]]
- then
- echo -n $src/$folder 'is not empty, copying... '
- cp -r $src/$folder $dest
- #check for success on copying
- if [[ $? = 0 ]]
- then
- fullcount=$(expr $fullcount + 1)
- echo 'copied'
- else
- errcount=$(expr $errcount + 1)
- echo 'failed'
- fi
- else
- echo $src/$folder 'is empty, skipping'
- emptycount=$(expr $emptycount + 1)
- fi
- done
- fi
- #print stats
- echo -n "done:" $fullcount "directories copied and" $emptycount "empty directories skipped"
- if [[ $errcount -gt 0 ]]
- then
- echo " and" $errcount "errors"
- else
- echo
- fi
Advertisement
Add Comment
Please, Sign In to add comment