#!/bin/bash sep='' resep='@' safe_types_regexp=('text' '(GIF|JPEG|PNG) image data' 'MS Windows icon') safe_types_string=('empty') safe_files_regexp=('/\.(git|svn)/') safe_files_string=() normalize_filename() { local cwd="`pwd`" readlink -m -- "$1"|sed "s|^$cwd/|./|" } matches_string() { local needle=$1 shift for straw in "$@"; do if [[ "$needle" = "$straw" ]]; then return 0 fi done return 1 } matches_regexp() { local needle=$1 shift for straw in "$@"; do if [[ "$needle" =~ $straw ]]; then return 0 fi done return 1 } main() { # Parse arguments for file in "$@"; do safe_files_string+=("$(normalize_filename "$file")"); done # Init unsafe_files="$(mktemp)" # Heavy lifting find . -type f -exec file -F"$sep" {} + | while read line; do file="$(echo "$line"|sed "s${resep}${sep}.*${resep}${resep}")" type="$(echo "$line"|sed "s${resep}.*${sep}\s*${resep}${resep}")" file="$(normalize_filename "$file")" if \ matches_string "$file" "${safe_files_string[@]}" || \ matches_string "$type" "${safe_types_string[@]}" || \ matches_regexp "$file" "${safe_files_regexp[@]}" || \ matches_regexp "$type" "${safe_types_regexp[@]}" ; then : # do nothing else printf "%s\n" "$file" fi done > "$unsafe_files" if [[ -n "$(cat "$unsafe_files")" ]]; then echo "==> WARNING: The source directory `pwd` contains binary files:" <"$unsafe_files" sort | sed 's/./ -> &/' rm -f "$unsafe_files" exit 1 else rm -f "$unsafe_files" fi } main "$@"