-
Notifications
You must be signed in to change notification settings - Fork 0
/
文件操作
208 lines (177 loc) · 6.08 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
###########文件的差集与交集#########
whwu@master:~/script> cat A.txt
apple
gold
iron
orange
silver
steel
whwu@master:~/script> cat B.txt
carrot
cookies
gold
orange
whwu@master:~/script> comm A.txt B.txt
apple
orange
comm: file 1 is not in sorted order
comm: file 2 is not in sorted order
gold
cookies
carrot
silver
steel
iron
whwu@master:~/script> sort A.txt -o A.txt; sort B.txt -o B.txt
whwu@master:~/script> comm A.txt B.txt
apple
carrot
cookies
gold
iron
orange
silver
steel
文件备份以及压缩
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt
whwu@master:~/script/test> tar -cf output.tar 1.txt 2.txt #将两个文件打包成一个文件 -c创建 -f指定文件名
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt output.tar
whwu@master:~/script/test> tar -tf output.tar #-t查看文件
1.txt
2.txt
whwu@master:~/script/test> tar -rvf output.tar 3.txt #-r追加 -v压缩的过程中显示文件
3.txt
whwu@master:~/script/test> tar -tf output.tar
1.txt
2.txt
3.txt
whwu@master:~/script/test> ls
4.txt fortar output.tar
whwu@master:~/script/test> tar -xf output.tar #-x表示提取extract
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt fortar output.tar
whwu@master:~/script/test/fortar> ls
whwu@master:~/script/test/fortar>
whwu@master:~/script/test/fortar> tar -xf ../output.tar -C . #-C必须是大写 指定文件提取至哪个文件夹
whwu@master:~/script/test/fortar> ls
1.txt 2.txt 3.txt
whwu@master:~/script/test/fortar> tar -xvf ../output.tar 1.txt -C . #只提取指定文件至文件夹
1.txt
whwu@master:~/script/test/fortar> ls
1.txt
whwu@master:~/script/test> tar -Af first.tar output.tar #合并两个.tar文件
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt first.tar fortar output.tar
whwu@master:~/script/test> tar -tvf first.tar
-rw-rw-r-- whwu/whwu 53 2016-04-22 00:06 1.txt
-rw-rw-r-- whwu/whwu 29 2016-04-22 00:06 2.txt
-rw-rw-r-- whwu/whwu 53 2016-04-22 00:06 1.txt
-rw-rw-r-- whwu/whwu 29 2016-04-22 00:06 2.txt
-rw-rw-r-- whwu/whwu 111 2016-04-22 00:06 3.txt
whwu@master:~/script/test> tar -tf first.tar
1.txt
2.txt
1.txt
2.txt
3.txt
whwu@master:~/script/test> tar --delete --file first.tar 3.txt #从归档文件中删除文件
whwu@master:~/script/test> tar -tf first.tar
1.txt
2.txt
1.txt
2.txt
whwu@master:~/script/test> tar --delete --file first.tar 1.txt
whwu@master:~/script/test> tar -tf first.tar
2.txt
2.txt
tar命令只是将文件归档
whwu@master:~/script/test> tar -tf output.tar
1.txt
2.txt
3.txt
4.txt
whwu@master:~/script/test> tar -cf output.tar * --exclude "1.txt" #从归档中排除某些文件
whwu@master:~/script/test> tar -tf output.tar
2.txt
3.txt
4.txt
whwu@master:~/script/test> tar -cf output.tar * --exclude "1.txt" --totals #--total显示有多少个字节
tar: output.tar: file is the archive; not dumped
Total bytes written: 10240 (10KiB, 23MiB/s)
whwu@master:~/script/test> tar -tf output.tar
2.txt
3.txt
4.txt
bunzip以及bzip命令
用bunzip或bzip压缩
bunzip2是另一种与gzip类似的压缩技术。bzip2通常能够生成比gzip更小(压缩比更高)的文件。
所有Linux发型版都包含了这个工具。
whwu@master:~/script/test> bzip2 1.txt 2.txt 3.txt
whwu@master:~/script/test> ls
1.txt.bz2 2.txt.bz2 3.txt.bz2 4.txt file1 file2 file3 #分别对文件进行压缩,没有归档
whwu@master:~/script/test> bunzip2 1.txt.bz2 #解压缩时删除.bz2文件,然后生成1.txt的未压缩形式
whwu@master:~/script/test> ls
1.txt 2.txt.bz2 3.txt.bz2 4.txt file1 file2 file3
whwu@master:~/script/test> tar -cjvvf test.tar.bz2 1.txt 2.txt 3.txt #创建经由bzip2压缩过的归档文件
-rw-rw-r-- whwu/whwu 58 2016-04-23 11:13 1.txt
-rw-rw-r-- whwu/whwu 29 2016-04-22 00:06 2.txt
-rw-rw-r-- whwu/whwu 111 2016-04-22 00:06 3.txt
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test.tar.bz2
whwu@master:~/script/test> tar -xavvf test.tar.bz2 -C test #将文件解压到指定文件
-rw-rw-r-- whwu/whwu 58 2016-04-23 11:13 1.txt
-rw-rw-r-- whwu/whwu 29 2016-04-22 00:06 2.txt
-rw-rw-r-- whwu/whwu 111 2016-04-22 00:06 3.txt
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test.tar.bz2
whwu@master:~/script/test> cd test
whwu@master:~/script/test/test> ls
1.txt 2.txt 3.txt
raync命令
用raync备份系统快照
rsync -av source_path destination_path #-a表示要进行归档 -v表示在stdout上打印出细节信息或进度
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test1 test.tar.bz2
whwu@master:~/script/test> rsync -av ./test ./test1 #将文件夹test 复制到test1中
sending incremental file list
test/
test/1.txt
test/2.txt
test/3.txt
sent 426 bytes received 73 bytes 998.00 bytes/sec
total size is 198 speedup is 0.40
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test1 test.tar.bz2
whwu@master:~/script/test> cd test1
whwu@master:~/script/test/test1> ls
test
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test.tar.bz2
whwu@master:~/script/test> rsync -av ./test ./test1 #可以创建文件夹test1
sending incremental file list
created directory ./test1
test/
test/1.txt
test/2.txt
test/3.txt
sent 426 bytes received 73 bytes 998.00 bytes/sec
total size is 198 speedup is 0.40
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test1 test.tar.bz2
whwu@master:~/script/test> rsync -av ./test ./test1 --exclude "1.txt" #传递过程中,排除1.txt
sending incremental file list
created directory ./test1
test/
test/2.txt
test/3.txt
sent 310 bytes received 54 bytes 728.00 bytes/sec
total size is 140 speedup is 0.38
whwu@master:~/script/test> ls
1.txt 2.txt 3.txt 4.txt file1 file2 file3 test test1 test.tar.bz2
whwu@master:~/script/test> cd test1
whwu@master:~/script/test/test1> ls
test
whwu@master:~/script/test/test1> ls test
2.txt 3.txt