forked from appotry/PTtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddirlink.sh
173 lines (140 loc) · 4.04 KB
/
ddirlink.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
#记录日志: ./dirlink.sh > dirlink.log
#使用说明: https://github.com/GTian28/PTtool#readme
#查找文件硬链接
#ls -ialh file.txt
#find . -inum 1234
#最后面不要加斜杠
SRC="/volume1/media/src"
DST="/volume1/media/dst"
FILEGIG=1000000c
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
######################################
function mklink() {
local THISSRC=$1
local THISDST=$2
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
function check_dir() {
local current_dir=$1
for file in "$current_dir"/*; do
if [ -f "$file" ]; then
local link_count=$(ls -l "$file" | awk '{print $2}')
if [ "$link_count" -eq 1 ]; then
# 找到一个没有硬链接的文件,不删除目录
return 1
fi
fi
done
# 没有找到没有硬链接的文件,删除目录
rm -r "$current_dir"
return 0
}
# 创建新的函数,用于递归遍历目录
function recursive_mklink() {
local current_dir=$1
local target_dir=$2
# 遍历当前目录下的所有文件和子目录
for item in "$current_dir"/*; do
# 如果是目录,就递归调用这个函数
if [ -d "$item" ]; then
recursive_mklink "$item" "$target_dir/$(basename "$item")"
fi
done
# 检查目录下是否有文件,如果有,就调用mklink函数
local dir_has_file=$(find "$current_dir" -maxdepth 1 -type f | head -1)
if [ -n "$dir_has_file" ]; then
if [ -e "$current_dir/islinked.lk" ]; then
check_dir "$current_dir"
else
mklink "$current_dir" "$target_dir"
touch "$current_dir/islinked.lk"
fi
fi
}
# 调用递归函数
recursive_mklink "$SRC" "$DST"
IFS=$SAVEIFS