-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcondense
executable file
·60 lines (53 loc) · 1.39 KB
/
condense
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
#!/bin/bash
# INSTALL: symlink or copy this file to /usr/local/bin
#
# Move all files in subdirectories to the current level and delete duplicates
#
# Usage:
# $ condense [-c] [-o output] [source]
#
# Positional Arguments:
# * [source]: Root directory to start recursively condensing files from
#
# Options:
# * [-c]: Copy files instead of moving
# * [-o output]: Output directory to transport files to (defaults to source)
#
# Notes:
# The root directory will be searched for duplicate files using fdupes. If any
# changes are made, they are printed to stdout with [+/-] notation
# Parse CLI optional and positional arguments --------------------------------
while getopts "co:" opt; do
case $opt in
c)
copy=true;;
o)
output=$OPTARG;;
\?)
exit 1;;
:)
exit 1;;
esac
done
copy=${copy:-false}
shift $((OPTIND - 1))
source=${1:-$(pwd)}
if [ -z "$output" ]; then
output=$source
keep_source=true
fi
# Execute command ------------------------------------------------------------
fdupes -drqN $source
search="find $source -mindepth 2 -type f"
if [[ "$copy" = true ]]; then
transport="xargs -I {} cp {} $output"
else
transport="xargs -I {} mv {} $output"
fi
cmd="$search | $transport"
eval $cmd
if [[ "$keep_source" = true ]]; then
find $source -mindepth 1 -type d -delete
else
find $source -type d -delete
fi