日期:2014-05-16  浏览次数:20605 次

linux 常用文本操作命令集
1.创建特定大小的文件dd
[wang@localhost 桌面]$ dd if=/dev/zero of=test.data bs=1K count=2
2+0 records in
2+0 records out
2048 bytes (2.0 kB) copied,0.00112496 秒,1.8 MB/秒
其中if表示输入文件,of表示输出文件,bs表示单位块大小,count表示需要复制的块数。
创建空白文件就用touch filename

2.文件的交集和差集comm
[wang@localhost 桌面]$ sort b.txt -o b.txt 
[wang@localhost 桌面]$ sort a.txt -o a.txt 
[wang@localhost 桌面]$ comm a.txt b.txt 

a
b
c
d
e
f
g
i
第一列包含只在a.txt出现的行,第二列包含只在b.txt出现的行,第三列包含在a.txt和b.txt出现的行
[wang@localhost 桌面]$ comm a.txt b.txt -1 -2
a
c
d
f
-1表示干掉第一列,-2表示干掉第二列,用于取交集。

3.创建长目录mkdir
[wang@localhost 桌面]$ mkdir -p ./william/wang/2013
此命令会依次创建william,wang,2013目录

4.查看文件信息file
[wang@localhost 桌面]$ file shell.sh 
shell.sh: Bourne-Again shell script text executable
[wang@localhost 桌面]$ file -b shell.sh 
Bourne-Again shell script text executable    //只列出文件类型

5.查找文件差异并进行修补diff
[wang@localhost 桌面]$ diff -u a.txt b.txt 
--- a.txt 2013-03-26 14:17:00.265890952 +0800
+++ b.txt 2013-03-26 14:16:55.439744576 +0800
@@ -1,8 +1,5 @@
-
 a
-b
 c
 d
-e
 f
-g
+i
[wang@localhost 桌面]$ diff -u a.txt b.txt > c.patch
[wang@localhost 桌面]$ patch -p1 b.txt < c.patch 
missing header for unified diff at line 3 of patch
patching file b.txt
Reversed (or previously applied) patch detected!  Assume -R? [n] y
[wang@localhost 桌面]$ cat b.txt

a
b
c
d
e
f
g
现在b.txt和a.txt内容一样了。

6.打印前面或后面几行
[wang@localhost 桌面]$ head -n 3 b.txt

a
b
[wang@localhost 桌面]$