-
Notifications
You must be signed in to change notification settings - Fork 25
/
file-eq
executable file
·73 lines (67 loc) · 1.62 KB
/
file-eq
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/sh
##
# file-eq:
# return true iff two files are equal.
#
# Syntax:
#
# file-eq <file1> <file2>
#
# Example:
#
# $ file-eq foo.txt bar.txt
#
# How the script compares the files:
#
# * Is each file a real file and not directory, symlink, etc.?
# * Are the two files the same size, and do they have the same content?
# * If the files are equal, then exit 0, else nonzero.
#
#
# ## Customization
#
# To calculate file size we try to use the `du` command, then try others.
#
# To do file comparisons, we use the `cmp` command.
#
# You can customize the commands by using environment variables:
#
# DU=/foo/du CMP=/goo/cmp
#
#
# ## Tracking
#
# * Program: file-eq
# * Version: 2.0.0
# * Created: 2014-12-02
# * Updated: 2017-09-06
# * License: GPL
# * Contact: Joel Parker Henderson ([email protected])
##
set -euf
arg_file1="$1"
arg_file2="$2"
out () { printf %s\\n "$*" ; }
file_size() {
file="$1"
size=$(
${DU:-du} --apparent-size --block-size=1 "$1" 2>/dev/null ||
${GDU:-gdu} --apparent-size --block-size=1 "$1" 2>/dev/null ||
${FIND:-find} "$1" -printf "%s" 2>/dev/null ||
${GFIND:-gfind} "$1" -printf "%s" 2>/dev/null ||
${STAT:-stat} --printf="%s" "$1" 2>/dev/null ||
${STAT:-stat} -f%z "$1" 2>/dev/null ||
${WC:-wc} -c <"$1" 2>/dev/null
)
q=$?; [ $q -eq 0 ] || exit $q
out "$size" | awk '{print $1}'
}
file_eq() {
file1="$1"; file2="$2"
[ -f "$file1" ] && [ ! -L "$file1" ] &&
[ -f "$file2" ] && [ ! -L "$file2" ] &&
[ $(file_size "$file1") == $(file_size "$file2") ] &&
${CMP:-cmp} -s "$file1" "$file2"
return
}
file_eq "$arg_file1" "$arg_file2"