-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhide
More file actions
executable file
·95 lines (79 loc) · 1.85 KB
/
hide
File metadata and controls
executable file
·95 lines (79 loc) · 1.85 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env bash
# hide -- move no longer useful files to the attic
. lib.bash || exit
usage() {
echo "Usage: $progname [-c] [-d <dir>] [-y <year>] <files>..."
echo ""
echo "Put away old files to ~/Attic/Misc/<year>."
echo ""
echo_opt "-c" "Copy instead of moving"
echo_opt "-d <dir>" "Put files in a subdirectory"
echo_opt "-y <year>" "Use specified year instead of modification time"
}
dir="$HOME/Attic/Misc"
if [[ ! -d $dir ]]; then
dir="/net/ember/home/grawity/Attic/Misc"
if [[ ! -d $dir ]]; then
vdie "misc directory not found and not accessible via NFS"
fi
vmsg "using NFS location ($dir)"
fi
arg_subdir=
arg_year=
arg_copy=0
while getopts ":cd:y:" OPT; do
case $OPT in
c) arg_copy=1;;
d) arg_subdir=$OPTARG;;
y) arg_year=$OPTARG;;
*) lib:die_getopts;;
esac
done; shift $((OPTIND-1))
if (( ! $# )); then
vdie "no files specified"
fi
if [[ $arg_year == @(now|today) ]]; then
arg_year=$(date +%Y)
fi
for arg; do
if [[ ! -e $arg ]]; then
vmsg "not found: \"$arg\"" >&2
let ++errors
continue
elif [[ -L $arg || ! ( -f $arg || -d $arg ) ]]; then
vmsg "not a regular file: \"$arg\"" >&2
let ++errors
continue
fi
src=$(readlink -f "$arg")
# separate name/extension for adding "-counter" if needed
ext=""
base=$(basename "$arg")
if [[ $base == *.* ]]; then
ext=${base##*.}
base=${base%.*}
fi
if [[ $arg_year ]]; then
dstdir=$dir/$arg_year
else
dstdir=$dir/$(date +%Y -d "@$(stat -c %Y "$arg")")
fi
if [[ $arg_subdir ]]; then
dstdir=$dstdir/$arg_subdir
fi
mkdir -p "$dstdir"
count=1
dst="$dstdir/$base${ext:+.}$ext"
while [[ -e $dst ]]; do
let ++count
dst="$dstdir/$base-$count${ext:+.}$ext"
done
if (( arg_copy )); then
vmsg "copying \"$src\" to \"${dst#$HOME/}\""
cp -b -p "$src" "$dst"
else
vmsg "moving \"$src\" to \"${dst#$HOME/}\""
mv -b "$src" "$dst"
fi || let ++errors
done
(( ! errors ))