Last active
November 27, 2015 17:28
-
-
Save florin-chelaru/2a6e8440cc9d0ea28a7a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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