-
Notifications
You must be signed in to change notification settings - Fork 48
/
dirlink.sh
executable file
·153 lines (120 loc) · 3.08 KB
/
dirlink.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
#auther: [email protected]
#记录日志: ./dirlink.sh > dirlink.log
#使用说明: https://github.com/appotry/PTtool#readme
#查找文件硬链接
#ls -ialh file.txt
#find . -inum 1234
#最后面不要加斜杠
SRC="/share/Download/tmp/src"
DST="/share/Download/tmp/dst"
FILEGIG=1000000c
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
######################################
function mklink ()
{
local THISSRC=$1
local THISDST=$2
echo "$*"
echo "mklink:"$THISSRC $THISDST
#查找大于1M的文件,硬链接
for i in `find $THISSRC -size +$FILEGIG`
do
echo "work:$i"
if [ -d $i ]; then
echo "跳过处理目录:$i"
echo "--"
continue
else if [ -e $i ]; then
echo "THISSRC file:$i"
fi
fi
#判断目录是否已经存在
tmppth=`dirname $i`
pth=${tmppth/"$THISSRC"/"$THISDST"}
if [ ! -d $pth ]; then
echo "mkdir -p $pth"
mkdir -p $pth
#else
# echo "跳过处理目录:$i"
# echo "--"
# continue
fi
dstfile=$pth/`basename $i`
echo "dst file:${dstfile}"
#判断文件是否已经存在
#不存在才复制
if [ ! -f $dstfile ]; then
echo "cp -l $i $dstfile"
cp -l $i $dstfile
fi
echo "--"
done
#查找小于1M的文件,复制小于1m的文件
for i in `find $THISSRC -size -$FILEGIG`
do
echo "work:$i"
if [ -d $i ]; then
echo "跳过处理目录:$i"
echo "--"
continue
else if [ -e $i ]; then
echo "src file:$i"
fi
fi
#判断目录是否已经存在
tmppth=`dirname $i`
pth=${tmppth/"$THISSRC"/"$THISDST"}
if [ ! -d $pth ]; then
echo "mkdir -p $pth"
mkdir -p $pth
fi
dstfile=$pth/`basename $i`
echo "dst file:${dstfile}"
#判断文件是否已经存在
#不存在才复制
if [ ! -f $dstfile ]; then
echo "cp $i $dstfile"
cp $i $dstfile
fi
echo "--"
done
return 0
}
function servicectl_usage(){
echo "Usage:dirlink.sh sourcedir dstdir"
return 1
}
function servicectl(){
[[ -z $1 || -z $2 ]] && servicectl_usage
}
if [ $# -eq 2 ]; then
SRC=$1
DST=$2
echo "User set:"
echo "src:$SRC"
echo "dst:$DST"
else
servicectl_usage
echo "use default set:"
echo "源目录src:$SRC"
echo "目的目录dst:$DST"
fi
for dir in $(ls $SRC)
do
echo "work dir:$dir"
dstdir=$DST/$dir
echo "当前硬链接目录"$dstdir
#if [ ! -d $dstdir ]; then
if [ ! -e $SRC/$dir/islinked.lk ]; then
mklink "$SRC/$dir" "$dstdir"
touch "$SRC/$dir"/islinked.lk
echo "=="
else
echo "$dir 已经硬链接过,跳过此目录"
echo "=="
fi
#fi
done
IFS=$SAVEIFS