Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Last active May 16, 2022 08:22
Show Gist options
  • Save thewh1teagle/331d0cf0e0ca7253fb748f2c440740e5 to your computer and use it in GitHub Desktop.
Save thewh1teagle/331d0cf0e0ca7253fb748f2c440740e5 to your computer and use it in GitHub Desktop.
watch changes under /dev directory in linux.
#!/usr/bin/bash
# will be ls /dev
# 2 files to compare before and
# after plug in/out the device
before="/tmp/before.txt"
after="/tmp/after.txt"
ls --color /dev > $before && clear
echo "############################"
echo "# Welcome to udev watcher! #"
echo "############################"
echo "1. Insert/remove device"
echo "2. Press any key to continue"
read -n 1 -r -s
clear && echo "Scanning..." && sleep 2
ls --color /dev > $after
result=$(diff $before $after)
clear && echo ""
if [ "$result" = "" ]; then
echo "Not found any device changes"
else
if [[ $result == *">"* ]]; then
echo "New devices added:"
else
echo "Devices removed:"
fi
echo $result
fi
# CLEAN
rm -f $before $after
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment