Skip to content

Commit d2b86df

Browse files
Add rm-clone
1 parent 3ab176c commit d2b86df

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

Diff for: rm-clone

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/sh
2+
3+
##
4+
# rm-clone:
5+
# if two file are equal, then delete the second file.
6+
#
7+
# Syntax:
8+
#
9+
# rm-clone <file1> <file2>
10+
#
11+
# Example:
12+
#
13+
# $ rm-clone foo.txt bar.txt
14+
#
15+
# How the script compares the files:
16+
#
17+
# * Is each file a real file and not directory, symlink, etc.?
18+
# * Are the two files the same size, and do they have the same content?
19+
# * If the files are equal, then delete the second file.
20+
#
21+
#
22+
# ## Customization
23+
#
24+
# To calculate file size we try to use the command `du` then try others.
25+
#
26+
# To do file comparisons, we use the command `cmp`.
27+
#
28+
# To remove a file, we use the command `rm`.
29+
#
30+
# You can customize the commands by using environment variables:
31+
#
32+
# DU=/foo/du CMP=/goo/cmp RM=/bin/rmsafe ...
33+
#
34+
#
35+
# ## Tracking
36+
#
37+
# * Program: rm-clone
38+
# * Version: 2.0.0
39+
# * Created: 2014-12-02
40+
# * Updated: 2017-09-06
41+
# * License: GPL
42+
# * Contact: Joel Parker Henderson ([email protected])
43+
##
44+
set -euf
45+
46+
arg_file1="$1"
47+
arg_file2="$2"
48+
49+
out () { printf %s\\n "$*" ; }
50+
51+
file_size() {
52+
file="$1"
53+
size=$(
54+
${DU:-du} --apparent-size --block-size=1 "$1" 2>/dev/null ||
55+
${GDU:-gdu} --apparent-size --block-size=1 "$1" 2>/dev/null ||
56+
${FIND:-find} "$1" -printf "%s" 2>/dev/null ||
57+
${GFIND:-gfind} "$1" -printf "%s" 2>/dev/null ||
58+
${STAT:-stat} --printf="%s" "$1" 2>/dev/null ||
59+
${STAT:-stat} -f%z "$1" 2>/dev/null ||
60+
${WC:-wc} -c <"$1" 2>/dev/null
61+
)
62+
q=$?; [ $q -eq 0 ] || exit $q
63+
out "$size" | awk '{print $1}'
64+
}
65+
66+
file_eq() {
67+
file1="$1"; file2="$2"
68+
[ -f "$file1" ] && [ ! -L "$file1" ] &&
69+
[ -f "$file2" ] && [ ! -L "$file2" ] &&
70+
[ $(file_size "$file1") == $(file_size "$file2") ] &&
71+
${CMP:-cmp} -s "$file1" "$file2"
72+
return
73+
}
74+
75+
file_eq "$arg_file1" "$arg_file2" && ${RM:-rm} "$arg_file_2"

0 commit comments

Comments
 (0)