Last active
August 29, 2015 14:27
-
-
Save brbsix/bb67b6050cd90018d67a to your computer and use it in GitHub Desktop.
Identify changes to a filesystem
This file contains 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 | |
# Identify changes to a filesystem | |
fsdiff(){ | |
( | |
defaultdirs=(.) | |
localdirs=("$HOME") | |
systemdirs=(/etc /home /opt /root /usr /var) | |
if (( $# == 0 )); then | |
watchdirs=("${defaultdirs[@]}") | |
elif (( $# == 1 )) && [[ $1 =~ ^(-h|--help)$ ]]; then | |
cat <<-EOF | |
Usage: ${FUNCNAME[0]} [OPTION] | |
Identify changes to a filesystem | |
-l, --local checks: ${localdirs[*]} | |
-s, --system checks: ${systemdirs[*]} | |
By default, check current directory for changes. | |
EOF | |
return 0 | |
elif (( $# == 1 )) && [[ $1 =~ ^(-l|--local)$ ]]; then | |
watchdirs=("${localdirs[@]}") | |
elif (( $# == 1 )) && [[ $1 =~ ^(-s|--system)$ ]]; then | |
watchdirs=("${systemdirs[@]}") | |
else | |
error "Invalid command" | |
return 1 | |
fi | |
hashall(){ | |
find "${watchdirs[@]}" ! -type d ! -path "$HOME/.cache/*" ! -path "$HOME/.lastpass/*" ! -path "$HOME/.local/share/Trash/*" ! -path "$HOME/.macromedia/*" ! -path "$HOME/.mozilla/*" ! -path "$HOME/.thumbnails/*" ! -path "$HOME/Downloads/*" -print0 2>/dev/null| sort -z | xargs -0r -- md5sum 2>/dev/null | |
} | |
before=$(hashall) | |
read -n 1 -p 'Continue? ' | |
echo | |
[[ $REPLY != [Yy] ]] && return 0 | |
after=$(hashall) | |
diff -c <(echo "$before") <(echo "$after") | sed 's/ [0-9a-z]\{32\} //' | sort -u | awk '/^+ / {sub("^+ ", ""); added[a++]=$0} /^- / {sub("^- ", ""); removed[r++]=$0} /^\! / {sub("^! ", ""); modified[m++]=$0} END{if (length(added)>0) {print "ADDED"; for (line in added) print added[line]; print ""} if (length(removed)>0) {print "REMOVED"; for (line in removed) print removed[line]; print ""} if (length(modified)>0) {print "MODIFIED"; for (line in modified) print modified[line]; print ""}}' | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment