-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp_and_replace.sh
executable file
·118 lines (101 loc) · 2.18 KB
/
cp_and_replace.sh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/bin/bash -eu
PROGNAME=$(basename "${0}")
VERSION='0.4'
SOURCE=''
DESTINATION=''
FINDOPTS='-mtime +30'
FINDMUSTOPTS='! -name .DS_Store ! -name ._*'
function usage(){
cat <<EOF
Description:
source 以下の検索条件に一致するファイルを destination に階層ごとコピーして元ファイルはシンボリックリンクに置き換える
Usage:
${PROGNAME} SOURCE-DIR DESTINATION-DIR [option] [-find <find-options>]
Options:
-h, --help
-v, --version
-d, --debug bash set -x
-find <find-options> find command options (Defult: ${FINDOPTS[@]})
EOF
}
for OPTS in "${@}"
do
case "${OPTS}" in
'-d'|'--debug')
shift 1
set -x
;;
'-h'|'--help')
usage
exit 0
;;
'-v'|'--version')
echo ${VERSION}
exit 0
;;
'-find')
if [ "${2-UNDEF}" = 'UNDEF' ]; then
FINDOPTS=''
else
FINDOPTS="${@:2}"
fi
break
;;
-*)
echo "[ERORR]: ${1}: Invalid option" 1>&2
exit 1
;;
*)
if [ ! "${1-UNDEF}" = 'UNDEF' ] && [ -d "${1}" ]; then
if [ -z "${SOURCE}" ]; then
SOURCE=$(cd "${1}" && pwd)
elif [ -z "${DESTINATION}" ]; then
DESTINATION=$(cd "${1}" && pwd)
fi
fi
shift 1
;;
esac
done
[ -z "${SOURCE}" ] && echo '[ERORR]: source dir: No such directory' 1>&2 && exit 1
[ -z "${DESTINATION}" ] && echo '[ERORR]: destination dir: No such directory' 1>&2 && exit 1
function cp_and_replace(){
TARGET="${1}"
echo "${TARGET}" | cpio -pdumv "${DESTINATION}"
if [ $? -eq 0 ]; then
ln -fs "${DESTINATION}/${TARGET}" "${TARGET}"
else
echo "Failed to copy file: ${TARGET}" 1>&2
exit 1
fi
}
cd "${SOURCE}"
FILES=()
while read f; do
FILES+=("${f}")
done < <(find * -type f ${FINDMUSTOPTS[@]} ${FINDOPTS[@]})
[ "${FILES-UNDEF}" = 'UNDEF' ] || [ -z "${FILES}" ] && exit 1
echo 'Timestamp Filepath'
for f in "${FILES[@]}"
do
echo $(stat -f %Sm -t %Y%m%d "${f}")" ${f}"
done
cat<<EOF
----
find command: find * -type f ${FINDMUSTOPTS[@]} ${FINDOPTS[@]}
source: ${SOURCE}
destination: ${DESTINATION}
----
copying and replacing files ? [y/n]
EOF
read ANS
if [ "${ANS}" = 'y' -o "${ANS}" = 'yes' ]; then
echo 'start'
for f in "${FILES[@]}"
do
cp_and_replace "${f}"
done
else
echo 'nya-n'
fi
exit 0