-
Notifications
You must be signed in to change notification settings - Fork 0
/
mv_calibre_junk
executable file
·53 lines (44 loc) · 2.28 KB
/
mv_calibre_junk
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
#!/usr/bin/env bash
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" )
# what/why: ebooks often come in archive-file sets which (I've taken awhile to
# realize) often include calibre-format-translated versions of the ebook in
# formats I don't care about (azw* and mobi); these are just deadweight
# consumers of disk space: if I need the format, I'm perfectly able to use
# calibre to generate a version of the file in the needed format; otherwise,
# I want to delete them before they start consuming server (especially
# backup) storage.
#
# Rather than delete such files, this script moves these files to a
# _calibre_junk subdir; higher level scripts are free to rm -rf this dir
#
die() { printf %s "${@+$@$'\n'}" 1>&2 ; exit 1 ; }
see() ( { set -x; } 2>/dev/null ; "$@" )
dieifnotinpath() { command -v "$1" >/dev/null || die "$1 not in PATH"; }
dieifnotinpath pdfinfo
dieifnotinpath find_calibre_binary
ebookmeta="$(find_calibre_binary ebook-meta)" || die
fn_exists() { declare -F "$1" > /dev/null; } # https://stackoverflow.com/a/85932
calibre_produced() { "$ebookmeta" "$1" | grep -q '^Book Producer.\+calibre' ; } # `ebook-meta` does not display "Book Producer ..." metadata for PDF files (though calibre WRITES it to produced PDFs!)
calibre_produced_pdf() { pdfinfo "$1" | grep -qP '^Producer:\s+calibre\s' ; }
mkdir -p _calibre_junk || die "mkdir failed"
# https://stackoverflow.com/a/26349346 variant of https://stackoverflow.com/a/8489394
azwmobi() (
find . -maxdepth 1 -type f \( -iname '*.azw*' -o -iname '*.mobi' \) -print0 | while IFS= read -r -d '' fnm; do
if calibre_produced "$fnm" ; then
see mv -- "$fnm" _calibre_junk
fi
done
)
all() ( # note that we exclude .medtype.pdf files from consideration
find . -maxdepth 1 -type f \( -iname '*.azw*' -o -iname '*.mobi' -o -iname '*.pdf' \) ! -iname '*.medtype.pdf' -print0 | while IFS= read -r -d '' fnm; do
ext="${fnm##*.}"
cpfx=calibre_produced ; if fn_exists "${cpfx}_$ext" ; then cpfx="${cpfx}_$ext" ; fi
# echo "$ext $cpfx $fnm"
# if mt="${fnm/%pdf/medtype.pdf}" && [[ -f "$mt" ]] && "$cpfx" "$fnm" ; then
if "$cpfx" "$fnm" ; then
see mv -- "$fnm" _calibre_junk
fi
done
)
"${1:-all}" # run scan