-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
samefile
executable file
·36 lines (31 loc) · 1.07 KB
/
samefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env sh
# samefile
# ⚖️ a portable, POSIX-compliant implementation of Bash’s `-ef` test
# check if two files are hardlinked by device and inode
#
# author: Lucas Larson
# URL: https://github.com/LucasLarson/samefile
# license: GNU Affero General Public License v3.0 or later
main() {
# find if two files refer to the device and inode numbers
# usage: samefile [-v|--verbose] file1 file2
# return 0 if they match
if [ "${1}" = -v ] || [ "${1}" = --verbose ]; then
set -x
shift
fi
# stat
# `-L` to dereference symbolic links # https://superuser.com/a/196655
# `-c` for GNU to display %d for device and
# %i for inode
# `-f` for BSD/macOS to display %d for device and
# %i for inode
if stat -L -c '%d:%i' -- . >/dev/null 2>&1; then
argument='-c'
else
argument='-f'
fi
[ "$(stat -L "${argument-}" %d:%i "${1}" 2>/dev/null)" = "$(stat -L "${argument-}" %d:%i "${2}")" ] && return 0 || return 1
{ set +x; } 2>/dev/null
}
main "${@}"