Skip to content

Instantly share code, notes, and snippets.

@florin-chelaru
Last active November 27, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florin-chelaru/2a6e8440cc9d0ea28a7a to your computer and use it in GitHub Desktop.
Save florin-chelaru/2a6e8440cc9d0ea28a7a to your computer and use it in GitHub Desktop.
#!/bin/bash
function showHelp {
printf "\nUsage: $1 [OPTION]... [IGNORES]...\n"
printf "Valid options:\n"
printf " -h\tThis help message\n"
printf " -d\tTarget directory\n"
printf " -o\tOutput file\n"
printf " -i=[list of dirs to ignore] (must be the last option in the command)\n"
printf "\n"
}
# joins the given arguments using one character separator
function join { local IFS="$1"; shift; echo "$*"; }
# joins the given arguments using a string separator
function multijoin {
separator=$1 # e.g. constructing regex, pray it does not contain %s
shift
items=( "$@" )
ret="$( printf "${separator}%s" "${items[@]}" )"
ret="${ret:${#separator}}" # remove leading separator
echo "${ret}"
}
currentFile=$0
hasIgnores=false
targetDir=.
out=""
while getopts "h?id:o:" opt; do
case "$opt" in
h|\?)
showHelp $currentFile
;;
i) hasIgnores=true
;;
d) targetDir=$OPTARG
;;
o) out=$OPTARG
;;
esac
done
shift $((OPTIND-1))
ignore=""
if [ "$hasIgnores" = true ] ; then
ignores=( "$@" )
ignore=" \( "$( multijoin " -or " "${ignores[@]/#/-wholename }" )" \) -prune -o "
fi
cmd="find $targetDir $ignore-type f -not \( -wholename $currentFile -or -name \"*.xz\" -or -name \"*.gz\" -or -name \"*.bz2\" -or -name \"*.zip\" -or -name \"*.tgz\" \) -print"
echo $cmd
echo $targetDir
if [ -z "$out" ] ; then
echo $cmd | bash -
else
echo $cmd | bash - > $out
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment