首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 操作系统 > UNIXLINUX >

linux 惯用文本操作命令集

2013-03-28 
linux 常用文本操作命令集1.创建特定大小的文件dd[wang@localhost 桌面]$ dd if/dev/zero oftest.data b

linux 常用文本操作命令集
1.创建特定大小的文件dd[wang@localhost 桌面]$ dd if=/dev/zero of=test.data bs=1K count=22+0 records in2+0 records out2048 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 
abcdefgi第一列包含只在a.txt出现的行,第二列包含只在b.txt出现的行,第三列包含在a.txt和b.txt出现的行[wang@localhost 桌面]$ comm a.txt b.txt -1 -2acdf-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 patchpatching file b.txtReversed (or previously applied) patch detected!  Assume -R? [n] y[wang@localhost 桌面]$ cat b.txt
abcdefg现在b.txt和a.txt内容一样了。
6.打印前面或后面几行[wang@localhost 桌面]$ head -n 3 b.txt
ab[wang@localhost 桌面]$ tail -n -2 b.txtfg[wang@localhost 桌面]$ tail -f /var/log/messagestail: 无法打开 “/var/log/messages” 读取数据: 权限不够tail: no files remaining[root@localhost 桌面]# tail -f /var/log/messages    //跟踪日志Mar 26 13:57:13 localhost dhclient: bound to 192.168.126.148 -- renewal in 706 seconds.Mar 26 14:08:59 localhost dhclient: DHCPREQUEST on eth0 to 192.168.126.254 port 67Mar 26 14:08:59 localhost dhclient: DHCPACK from 192.168.126.254Mar 26 14:08:59 localhost dhclient: bound to 192.168.126.148 -- renewal in 876 seconds.也可以这样用:[root@localhost 桌面]# dmesg | tail -f
7.只列出目录[wang@localhost 桌面]$ ls -d */mm/  [wang@localhost 桌面]$ ls -l | grep "^d"drwxrwxr-x.  2 wang wang   4096 01-25 19:33 mm
8.最近的两个目录切换[wang@localhost 桌面]$ cd -/home/wang/project[wang@localhost project]$ cd -/home/wang/桌面[wang@localhost 桌面]$ 
9.查找匹配的文件[wang@localhost 桌面]$ grep -l "bin" . -r./mm/test.sh./shell.sh./bt.sh./test1.sh./startup.x
10.切分文件[wang@localhost 桌面]$ cut -f2,3 -d ":" /etc/passwdx:0x:1x:2
11.查找文件并操作find 路径 -name "文件" -exec "command" {} \;[wang@localhost 桌面]$ find . -name "a.txt" -exec cp {} /home/wang/project/ \;[wang@localhost 桌面]$ ls /home/wang/project/
12.替换sed[wang@localhost 桌面]$ echo WO WO WO | sed 's/WO/wo/2g'WO wo wo从第二个匹配的地方开始替换,不要2的话,就全部替换。[wang@localhost 桌面]$ echo WO WO WO | sed 's/WO/wo/g'wo wo wo[wang@localhost 桌面]$ echo WO WO WO | sed 's/WO/wo/'wo WO WO[wang@localhost 桌面]$ echo linux is 2 | sed 's/is \([0-9]\)/\1/' linux 2is 后面加个数字 替换成匹配的第一个字段。
13.删除tr[wang@localhost 桌面]$ echo linux is not windows ! | tr -d 'i'lnux s not wndows ![wang@localhost 桌面]$ echo linux is not windows ! | tr -s ' 'linux is not windows ![wang@localhost 桌面]$ echo linux is not windows ! | tr 'n' 'm'
limux is mot wimdows !

14.按列合并文件paste[wang@localhost 桌面]$ paste a.txt b.txta ab bc cd de ef fg g[wang@localhost 桌面]$ paste a.txt b.txt -d ";";a;ab;bc;cd;de;ef;fg;g


热点排行