Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@StefanHamminga
Last active January 26, 2016 21:52
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 StefanHamminga/a668ce90af594e373278 to your computer and use it in GitHub Desktop.
Save StefanHamminga/a668ce90af594e373278 to your computer and use it in GitHub Desktop.
Bash script to detect whether two paths/files are in the same file system
#!/bin/bash
MOUNTS=`cat /proc/mounts | cut -d ' ' -f 2 | sort | uniq | tr '/' '\\/'`
IFS=$'\n'
what_fs() {
for MOUNT in ${MOUNTS}; do
readlink -f $1 | grep -o "^${MOUNT}"
# echo -e "$1"
# echo -e "${MOUNT}"
done
}
if [ $# -ne 2 ]; then
echo "Please provide exactly 2 arguments"
exit 2
fi
if [ ! -e "$1" ]; then
echo "File not found: $1"
exit 2
fi
if [ ! -e "$2" ]; then
echo "File not found: $2"
exit 2
fi
FS1=$(what_fs $1 | tail -n 1)
FS2=$(what_fs $2 | tail -n 1)
# echo -e "FS for '${1}': ${FS1}"
# echo -e "FS for '${2}': ${FS2}"
if [ "${FS1}" = "${FS2}" ]; then
echo "Same file system"
exit 0
else
echo -e "Different file system on ${FS2}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment