Linux常用命令总结-----不断更新(二)

一、ln  创建软连接与硬链接        make links between files

  命令重要参数:

  -s    --symbolic          make symbolic links instead of hard links(创建一个软链接[也成为符号链接])

1.为文件testfile创建一个硬链接

[root@moban tmp]# ln testfile testfile_hardlink

2.位文件testfile创建一个软连接

[root@moban tmp]# ln -s testfile testfile_softlink

3.查看testfile文件的源文件、硬链接、软链接相关的信息

[root@moban tmp]# ls -lih testfile*
915936 -rw-r--r-- 2 root root 0 Jan 29 23:37 testfile
915936 -rw-r--r-- 2 root root 0 Jan 29 23:37 testfile_hardlink
915938 lrwxrwxrwx 1 root root 8 Jan 29 23:38 testfile_softlink -> testfile

4.关于硬链接的总结

  • 具有相同inode节点号的文件互为硬链接文件
  • 删除硬链接文件或者删除源文件任意之一,文件实体并未被删除
  • 只有删除了源文件同时删除了源文件所有对应的硬链接文件,文件的实体才会被删除
  • 当所有硬链接文件及源文件被删除以后,在存放新的数据时会占用这个文件的空间,或者磁盘fsck检查的时候,删除的数据会被系统回收
  • 硬链接文件就是文件的另外一个入口点
  • 可以给文件设置硬链接文件来防止重要文件被误删除
  • 执行"ln 源文件 硬链接文件"即可完成文件硬链接的创建
  • 硬链接文件可以使用rm命令删除
  • 对于静态文件(没有进程正在调用的文件)来讲,当对应硬链接数为0(i_link)时,文件就会被删除

5.关于软链接(类似于Windows中的快捷方式)的总结

  • 软链接类似于Windows的快捷方式(可以通过后面的readlink命令查看其指向)
  • 软链接类似于一个文本文件,里面存放的是源文件的路径,指向源文件实体
  • 即使删除了源文件,软链接依然可以存在,但是无法访问指向的源文件路径内容
  • 失效的时候一般是白字红底闪烁提示
  • 执行命令"ln -s  源文件   软链接文件",即可完成软链接的创建
  • 软链接和源文件是不同类型的文件,也是不同的文件,inode节点号也不相同
  • 删除软链接文件可以用rm命令

6.关于链接文件一句话的总结:当我们删除源文件的时候,硬链接的访问不收影响,而软链接文件失效。

7.给目录testdir创建一个软链接

[root@moban tmp]# ln testdir/ testdir_hardlink
ln: `testdir/': hard link not allowed for directory

注意:用户无法给目录创建硬链接
[root@moban tmp]#
[root@moban tmp]#
[root@moban tmp]# ln -s testdir/ testdir_sotflink
[root@moban tmp]#
[root@moban tmp]# ls -ldi testdir_sotflink
915940 lrwxrwxrwx 1 root root 8 Jan 30 00:05 testdir_sotflink -> testdir/

8.关于给目录创建链接文件的总结

  • 对于目录,用户无法手工创建硬链接,但是可以创建软连接文件
  • 给目录创建软链接是生产运维场景下常用的技巧(ln  -s /application/apache.2.2.17   /application/apache),方便查看系统安装的软件版本同时便于以后的软件升级维护操作
  • 目录的硬链接不能跨越文件系统(从硬链接的原理可以理解,因为硬链接需要具有相同的inode节点号)
  • 在每个目录下面都有一个硬链接"."号,和对应上级目录的硬链接".."
  • 在父目录中每创建一个子目录,父目录的i_link数量就会增加1,因为子目录下存在一个父目录的硬链接"..";但是在父目录下新建文件,父目录的i_link数量不会增加

 二、rename      批量修改文件名            Rename files

例子:

1.将以".txt"结尾的文件名全部取消

-rw-r--r-- 1 root root 0 Jan 30 17:58 foo96.txt
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo97.txt
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo98.txt
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo99.txt
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo9.txt
[root@moban t1]# rename ".txt" "" *
[root@moban t1]# ll
total 0
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo1
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo10
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo100
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo101
-rw-r--r-- 1 root root 0 Jan 30 17:58 foo102

2.将上述文件名统一宽度为fooXXX,不足的数字前面补零:

[root@moban t1]# rename foo foo0 foo?

[root@moban t1]# rename foo foo0 foo??


三、basename    显示文件名或者目录名(弹出文件的目录和后缀名)          strip directory and suffix from filenames 

使用例子:

1.显示文件/data/dir1/file1.txt的文件名

[root@moban t1]# basename /data/dir1/file1.txt
file1.txt

2.显示文件/data/dir1/file1.txt的文件名,但是不要显示后缀

[root@moban t1]# basename /data/dir1/file1.txt .txt
file1


四、dirname    显示文件或者目录的路径(注意当我们给出的文件是相对路径的是时候,返回的也是一个相对路径)         strip non-directory suffix from file name 

使用例子:

1.显示文件/data/dir1/file1.txt的目录

[root@moban t1]# dirname /data/dir1/file1.txt
/data/dir1

2.进入/data/dir1后,显示file.txt的目录

[root@moban t1]# cd /data/dir1/
[root@moban dir1]#
[root@moban dir1]#
[root@moban dir1]# dirname file1.txt
.


五、chattr    改变文件的扩展属性          change file attributes on a Linux file system

使用例子:

1.设置只能网文件/data/dir1/file1.txt追加内容,但不能删除文件

[root@moban tmp]# chattr +a /data/dir1/file1.txt
[root@moban tmp]# lsattr /data/dir1/file1.txt
-----a-------e- /data/dir1/file1.txt

[root@moban tmp]# echo 123 > /data/dir1/file1.txt
-bash: /data/dir1/file1.txt: Operation not permitted
[root@moban tmp]# echo 123 >> /data/dir1/file1.txt

2.将文件/etc/passwd,/etc/group /etc/shadow  /etc/gshadow锁定

[root@moban tmp]# chattr +i /etc/passwd /etc/shadow /etc/group /etc/gshadow
[root@moban tmp]# lsattr /etc/passwd /etc/shadow /etc/group /etc/gshadow
----i--------e- /etc/passwd
----i--------e- /etc/shadow
----i--------e- /etc/group
----i--------e- /etc/gshadow
[root@moban tmp]# useradd KKK
useradd: cannot open /etc/passwd


 六、lsattr    查看文件的扩展属性          list file attributes on a Linux second extended file system

  命令主要参数:

  -R        递归查看目录下所有文件的扩展属性

  -a        显示所有文件包括隐藏文件的扩展属性

  -d        显示目录的扩展属性

使用例子:

1.查看目录/data及其子目录下所有文件的扩展属性

[root@moban tmp]# lsattr -R /data
-------------e- /data/dir1

/data/dir1:
-----a-------e- /data/dir1/file1.txt

2.查看目录/data/dir1下所有文件的扩展属性

[root@moban tmp]# lsattr -a /data/dir1/
-------------e- /data/dir1/..
-----a-------e- /data/dir1/file1.txt
-------------e- /data/dir1/.

3.仅查看目录/data/dir1自身的扩展属性

[root@moban tmp]# lsattr -d /data/dir1/
-------------e- /data/dir1/


七、file    判断并显示文件的类型            determine file type 

  命令重要参数:

  -b    --brief        按照精简格式进行输出(不输出文件名)


 八、md5sum      计算和校验文件的MD5值          compute and check MD5 message digest

  命令重要参数:

  -b    --binary          二进制模式读取文件

  -c    --check          从指定文件读取MD5值并进行校验

  -t    --text           文本模式读取文件

         --quiet           如果文件校验成功不显示OK

         --status          校验文件不输出任何信息,依靠文件的返回值确认(0表示成功,1表示失败)

使用例子:

1.生成文件/data/dir1/file1.txt的MD5校验值

[root@moban tmp]# md5sum /data/dir1/file1.txt
ba1f2511fc30423bdbb183fe33f3dd0f /data/dir1/file1.txt

2.校验文件/data/dir1/file1.txt是否发生了变化

[root@moban tmp]# md5sum -c file1md5.log
/data/dir1/file1.txt: OK
[root@moban tmp]# echo "12312" >> /data/dir1/file1.txt
[root@moban tmp]# md5sum -c file1md5.log
/data/dir1/file1.txt: FAILED
md5sum: WARNING: 1 of 1 computed checksum did NOT match


九、chown    改变文件或者目录的用户和用户组           change file owner and group

  命令重要参数:

  -R      递归更改目录的用户和用户组

使用例子:

1.更改文件的所属用户属性

[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 root root 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]#
[root@moban tmp]# chown duanzl /data/dir1/file1.txt
[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 duanzl root 10 Jan 30 23:57 /data/dir1/file1.txt

2.更改文件所属的组(用户组)属性

[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 duanzl root 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]#
[root@moban tmp]# chown .duanzl /data/dir1/file1.txt
[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt

3.同时更改文件所属的用户和组属性

[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chown lab1.lab1 /data/dir1/file1.txt
[root@moban tmp]# ls -l /data/dir1/file1.txt
-rw-r--r-- 1 lab1 lab1 10 Jan 30 23:57 /data/dir1/file1.txt

4.递归更改目录及目录下的所有子目录及文件的用户和用户组的属性 

[root@moban tmp]# chown -R duanzl.duanzl  /data


十、chmod    修改文件或者目录的权限          change file mode bits

  命令重要参数:

  -R        递归处理指定目录及其子目录下的所有文件的权限

关于文件权限的总结

权限位 全称 含义 对应数字
r read 可读权限 4
w write 可写权限 2
x execute 可执行权限 1
-   没有任何权限 0
备注 一些特殊的权限位:t、T、s、S、x、X
用户类型

文件所属用户:u(Owner/User)

文件所属用户组:g(Group)

其他用户:o(Other)

所有:a(ALL),等价于ugo

操作字符

+:加入

-:取消

=:设置

 

使用例子:

1.权限字符和操作表达式

[root@moban tmp]# chmod a= /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
---------- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chmod u+x /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
---x------ 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chmod g+w /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
---x-w---- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chmod o+r /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
---x-w-r-- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chmod ug+r,o-r /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
-r-xrw---- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt

2.数字权限授权

[root@moban tmp]# chmod 000 /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
---------- 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt
[root@moban tmp]# chmod 753 /data/dir1/file1.txt
[root@moban tmp]# ll /data/dir1/file1.txt
-rwxr-x-wx 1 duanzl duanzl 10 Jan 30 23:57 /data/dir1/file1.txt

3.对于指定目录的递归授权

[root@moban tmp]# tree /data -p
/data
└── [drwxr-xr-x] dir1
├── [drwxr-xr-x] dir1
│   ├── [-rw-r--r--] 1.txt
│   ├── [-rw-r--r--] 2.txt
│   ├── [-rw-r--r--] 3.txt
│   ├── [-rw-r--r--] 4.txt
│   ├── [-rw-r--r--] 5.txt
│   ├── [-rw-r--r--] 6.txt
│   ├── [-rw-r--r--] 7.txt
│   ├── [-rw-r--r--] 8.txt
│   └── [-rw-r--r--] 9.txt
└── [-rwxr-x-wx] file1.txt

2 directories, 10 files
[root@moban tmp]# chmod -R 777 /data
[root@moban tmp]# tree /data -p
/data
└── [drwxrwxrwx] dir1
├── [drwxrwxrwx] dir1
│   ├── [-rwxrwxrwx] 1.txt
│   ├── [-rwxrwxrwx] 2.txt
│   ├── [-rwxrwxrwx] 3.txt
│   ├── [-rwxrwxrwx] 4.txt
│   ├── [-rwxrwxrwx] 5.txt
│   ├── [-rwxrwxrwx] 6.txt
│   ├── [-rwxrwxrwx] 7.txt
│   ├── [-rwxrwxrwx] 8.txt
│   └── [-rwxrwxrwx] 9.txt
└── [-rwxrwxrwx] file1.txt


十一、chgrp    更改文件用户组          change group ownership

   命令重要参数:

   -R      递归修改文件的用户组

使用例子:

1.递归修改/data目录及其下子目录和文件的用户组

[root@moban tmp]# ll /data
total 4
drwxrwxrwx 3 duanzl duanzl 4096 Jan 31 00:54 dir1
[root@moban tmp]# ll /data/dir1/
total 8
drwxrwxrwx 2 duanzl duanzl 4096 Jan 31 00:54 dir1
-rwxrwxrwx 1 duanzl duanzl 10 Jan 30 23:57 file1.txt
[root@moban tmp]#
[root@moban tmp]#
[root@moban tmp]# ll /data/dir1/dir1/
total 0
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 1.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 2.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 3.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 4.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 5.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 6.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 7.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 8.txt
-rwxrwxrwx 1 duanzl duanzl 0 Jan 31 00:54 9.txt
[root@moban tmp]# chgrp -R root /data
[root@moban tmp]# ll /data
total 4
drwxrwxrwx 3 duanzl root 4096 Jan 31 00:54 dir1
[root@moban tmp]# ll /data/dir1/
total 8
drwxrwxrwx 2 duanzl root 4096 Jan 31 00:54 dir1
-rwxrwxrwx 1 duanzl root 10 Jan 30 23:57 file1.txt
[root@moban tmp]# ll /data/dir1/dir1/
total 0
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 1.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 2.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 3.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 4.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 5.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 6.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 7.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 8.txt
-rwxrwxrwx 1 duanzl root 0 Jan 31 00:54 9.txt


十二、umask    显示或设置权限掩码           Display or set file mode mask

   -p      输出的权限掩码可直接作为命令来执行

   -S      以字符方式输出权限掩码

 umask计算总结:

1.文件的权限计算

系统创建文件的默认的最大权限为666(rw-rw-rw-)。

文件的权限=默认的最高权限666-umask的值,如果得到的文件权限全部是偶数,则得到了最终的结果;若果有若干位为奇数,则该位置上的奇数需要加1得到最终结果

例子:假设umask为022,则文件的权限为644;假设umask为045,则文件的权限为621+1=622;假设umask为055,则文件的的权限为611+11=622

2.目录的权限计算

系统创建目录默认的最大权限为777()rwxrwxrwx),X权限表示目录可以被进入。

目录的权限=默认的最高权限-umask的值

例子:假设umask为022,则目录的权限为755


十三、more      分页显示文件内容            file perusal filter for crt viewing

   命令重要参数:

   -num      指定屏幕显示大小为num行

   +num      从行号num开始显示

   -s        把连续的空行显示为一行

   -p        不滚屏,而是清楚整个屏幕,然后显示文本

   -c        不滚屏,而是从每一屏的顶部开始显示文本,每显示完一行就清除这一行剩余的部分 


十四、less      分页显示文件内容(more命令的升级版本)

   命令重要参数:

   -i         搜索的时候忽略大小写

   -m       显示类似于more命令的进度百分比

   -N       显示每行的行号

   -s        将连续的空行压缩为一行显示

   -e        当文件显示到结尾的时候自动退出文件,若不使用此选项需要在交互式命令q退出less

关于less在交互模式下的子命令及其说明

子    命     令 解    释     说    明 子    命    令 解     释     说    明
b 向前翻一页 /字符串 向下搜索字符串
空格键 向后翻一页 ?字符串 向上搜索字符串
u  向前翻半页 n 向后查找下一个匹配的文本
d 向后翻半页 N 向前查找下一个匹配的文本
y 向上滚动一行 v 进入vi编辑模式
回车键 向下滚动一行 !命令 执行shell命令
向上滚动一行 G 移动到最后一行
向下滚动一行 g 移动到第一行
[page up] 向前翻一页 h 显示帮助页面
[page down] 向后翻一页 q 退出less命令

十五、tailf    跟踪日志文件          follow the growth of a log file

   命令重要参数:

   -n      默认情况下命令显示文件的最后10行,设置后可以显示文件的最后n行

tailf与tail -f的不同点在于如果监控的文件没有增长,它不会去访问磁盘的文件,也不会修改文件的访问时间。


十六、cut      从文本中提取一段文字并输出            remove sections from each line of files

   命令重要参数:

   -b      按照字节为单位进行分割

   -n      取消分割多字节符,与-b选项一起使用

   -c      以字符为单位进行分割

   -d      自定义分隔符,默认以Tab为分隔符号

   -f       与-d选项同时使用,指定要显示那个区域

   N        第N个字节、字符或者字段

   N-      从第N个字节、字符或者字段开始到行尾

   N-M     从第N个到第M(包含M)个字节、字符或者字段

   -M      从第一个到第M(包含M)个字节、字符或者字段

1.按照字节分割文本

[root@moban ~]# cat duan.txt
I am Zhili Duan,my qq is 88888888
[root@moban ~]# cut -b 3 duan.txt
a
[root@moban ~]# cut -b 3-5,10 duan.txt
am i
[root@moban ~]# cut -b -3 duan.txt
I a
[root@moban ~]# cut -b 3- duan.txt
am Zhili Duan,my qq is 88888888
[root@moban ~]# cut -b -3,3- duan.txt
I am Zhili Duan,my qq is 88888888

2.以字符为单位分割文本

[root@moban ~]# cut -c 3-10 duan.txt
am Zhili
[root@moban ~]# cut -b 3-10 duan.txt
am Zhili
[root@moban ~]# echo "星期一上班" >> duan.txt
[root@moban ~]# cat duan.txt
I am Zhili Duan,my qq is 88888888
星期一上班
[root@moban ~]# cut -c 3-10duan.txt
am Zhili
一上班
[root@moban ~]# cut -b 3-10 duan.txt
am Zhili

[root@moban ~]# cut -nb 3-10 duan.txt
am Zhili
星期一

3.自定义分隔符号 

[root@moban ~]# head -3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@moban ~]# cut -d : -f 1 /etc/passwd | awk 'NR==1,NR==3'
root
bin
daemon
[root@moban ~]# cut -d : -f 1 /etc/passwd | sed -n 1,3p
root
bin
daemon
[root@moban ~]# cut -d : -f 3-5 /etc/passwd | awk 'NR==1,NR==3'
0:0:root
1:1:bin
2:2:daemon
[root@moban ~]# cut -d : -f 3-5 /etc/passwd | sed -n 1,3p
0:0:root
1:1:bin
2:2:daemon


十七、 split      将文件分割成若干个文件            split a file into pieces

   命令重要参数:

   -b      指定分割后文件的最大字节数

   -l       执行分割后文件的最大行数

   -a      指定后缀长度,默认为2位字母

   -d      使用数字后缀

使用例子:

1.按照行分割文件,以及指定后缀的形式

[root@moban ~]# wc -l inittab
26 inittab
[root@moban ~]# split -l 10 inittab new_
[root@moban ~]# ls new_*
new_aa new_ab new_ac
[root@moban ~]# wc -l new_*
10 new_aa
10 new_ab
6 new_ac
26 total
[root@moban ~]# split -l 10 -a 3 inittab new2_
[root@moban ~]# ls new2_*
new2_aaa new2_aab new2_aac
[root@moban ~]# split -l 10 -d inittab new3_
[root@moban ~]# ls new3_*
new3_00 new3_01 new3_02
[root@moban ~]# split -l 10 -d -a 3 inittab new4_
[root@moban ~]# ls new4_*
new4_000 new4_001 new4_002

2.按照大小进行文件分割

[root@moban ~]# ll -h lvm
-r-xr-xr-x 1 root root 1.5M Jan 31 23:35 lvm
[root@moban ~]# split -b 500K -d -a 3 lvm lvm_
[root@moban ~]# ls -lh lvm_*
-rw-r--r-- 1 root root 500K Jan 31 23:36 lvm_000
-rw-r--r-- 1 root root 500K Jan 31 23:36 lvm_001
-rw-r--r-- 1 root root 490K Jan 31 23:36 lvm_002


 十八、paste       合并文件(按照行与行进行合并,中间使用tab隔开)        merge lines of files

   命令重要参数:

   -d        指定合并文件后的分隔符号,默认使用tab

   -s        合并后每个文件占用一行

如果文件指定为"-",表示从标准输入中读取内容

使用例子:

1.将test1和test2两个文件进行合并

[root@moban ~]# cat test1
1
2
3
4
5
6
[root@moban ~]# cat test2
aaaaaaa
bbbbbbbbb
ccccccccccccc

eeeeeeeeeeeeeeeeeee
gggggg
[root@moban ~]# paste test1 test2
1 aaaaaaa
2 bbbbbbbbb
3 ccccccccccccc
4
5 eeeeeeeeeeeeeeeeeee
6 gggggg
[root@moban ~]# paste -d : test1 test2
1:aaaaaaa
2:bbbbbbbbb
3:ccccccccccccc
4:
5:eeeeeeeeeeeeeeeeeee
6:gggggg
[root@moban ~]# paste -s test1 test2
1 2 3 4 5 6
aaaaaaa bbbbbbbbb ccccccccccccc eeeeeeeeeeeeeeeeeee gggggg


十九、sort      文本排序            sort lines of text files

   命令重要参数:

   -b      忽略每行开头存在的空格字符

   -n      依照数字的大小进行排序

   -r      倒序排列

   -u      去除重复行

   -t      指定分割符

   -k      按照指定区间排序

使用例子:

1.默认以行尾单位进行比较

[root@moban ~]# cat duan.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@moban ~]# sort duan.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.8

2.按照数字从小到大排列

[root@moban ~]# sort -n duan.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.8

3.文件按照降序排列

[root@moban ~]# sort -r duan.txt
10.0.0.8
10.0.0.5
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.4

4.去除文件中的重复行

[root@moban ~]# sort -u duan.txt
10.0.0.4
10.0.0.5
10.0.0.8

5.通过指定参数进行排列 

[root@moban ~]# sort -t " " -k2 duan.txt
10.0.0.4 a
10.0.0.4 g
10.0.0.8 l
10.0.0.5 n
10.0.0.4 q
10.0.0.4 r

6.将ARP表按照ip地址的第3列排序,暂按照IP地址的第四列排序

[root@moban ~]# cat arp
10.255.157.245 d4b1-10b5-b864
10.255.157.246 7485-2a1e-1011
10.255.188.249 d4b1-10b5-b869
10.255.26.218 d4b1-10b5-b86f
10.255.26.217 001a-a97d-e3de
10.255.166.69 d4b1-10b5-b864
10.255.166.70 d4b1-10b5-bc24
[root@moban ~]#
[root@moban ~]#
[root@moban ~]# sort -n -t"." -k3.1,3.3 -k4.1,4.3 arp
10.255.26.217 001a-a97d-e3de
10.255.26.218 d4b1-10b5-b86f
10.255.157.245 d4b1-10b5-b864
10.255.157.246 7485-2a1e-1011
10.255.166.69 d4b1-10b5-b864
10.255.166.70 d4b1-10b5-bc24
10.255.188.249 d4b1-10b5-b869


 二十、join    按照两个文件的相同字段合并          join lines of two files on a common field

   命令重要参数:

   -a  文件号        输出文件中不匹配的行,文件号可选值为1或2,分别代表文件1和文件2

   -i            比较文件的时候忽略大小写

   -1  字段          以第一个文件的指定字段为基础进行合并

   -2  字段         以第二个文件的指定字段为基础进行合并

使用例子:

 1.合并文本

[root@moban ~]# cat a.txt
韩林海 22岁
韩海林 21岁
林海韩 24岁
海林韩 23岁
[root@moban ~]# cat b.txt
韩林海 男
海林韩 男
林海韩 男
韩海林 男
[root@moban ~]# sort a.txt > a.txtn
[root@moban ~]# sort b.txt > b.txtn
[root@moban ~]# join a.txt b.txt
韩林海 22岁 男
join: file 1 is not in sorted order
韩海林 21岁 男
[root@moban ~]# join a.txtn b.txtn
林海韩 24岁 男
海林韩 23岁 男
韩林海 22岁 男
韩海林 21岁 男


二十一、uniq      去除重复行          report or omit repeated lines

  命令重要参数:

  -c      去除重复行,并计算出每行出现的次数

  -d      只显示重复的行

  -u      只显示唯一的行

使用例子:

1.uniq的标准使用方式

[root@moban ~]# cat duan-q.txt
10.0.0.4
10.0.0.4
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@moban ~]# uniq duan-q.txt
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@moban ~]# uniq -c duan-q.txt
3 10.0.0.4
1 10.0.0.5
1 10.0.0.4
1 10.0.0.8
[root@moban ~]# uniq -u duan-q.txt        ========> uniq -u显示唯一行的时候只考虑有没有连续的重复,不考虑隔行的重复
10.0.0.5
10.0.0.4
10.0.0.8

2. 对于uniq -u参数的进一步说明

[root@moban ~]# cat duan-q.txt
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8
[root@moban ~]# uniq -u duan-q.txt
10.0.0.4
10.0.0.5
10.0.0.4
10.0.0.8


二十二、wc      统计文件的行数、单词数或者字节数            print newline, word, and byte counts for each file

  命令重要参数:

  -c        统计字节数

  -l         统计行数

  -m         统计字符数

  -w         统计单词数

  -L          打印最长一行的长度

使用例子:

1.计算/etc/inittab文件的行数、字符数和单词数

[root@moban ~]# wc /etc/inittab         =======>默认情况下wc显示文件的行数、单词数和字符数
26 149 884 /etc/inittab
[root@moban ~]# wc -c /etc/inittab
884 /etc/inittab
[root@moban ~]# wc -l /etc/inittab
26 /etc/inittab
[root@moban ~]# wc -m /etc/inittab
884 /etc/inittab
[root@moban ~]# wc -w /etc/inittab
149 /etc/inittab
[root@moban ~]# wc -L /etc/inittab
78 /etc/inittab

 2.打印“I am oldboy teacher welcome to oldboy training class.”中字母数不大于6的单词

[root@moban ~]# for word in I am oldboy teacher welcome to oldboy training class.;do [ `echo $word|wc -L` -le 6 ] && echo $word;done
I
am
oldboy
to
oldboy
class.


二十三、iconv      转换文件的编码格式          convert text from one character encoding to another

使用例子:

1、将文件从gb2312编码转化为utf8编码格式

[root@moban ~]# cat code2312
hello.
[root@moban ~]#
[root@moban ~]#
[root@moban ~]#
[root@moban ~]# iconv -f gb2312 -t utf8 code2312 -o code_utf8
[root@moban ~]# cat code_utf8
您好
hello.


 二十四、dos2unix      将DOS/MAC格式的文本转换为Unix模式          DOS/MAC to UNIX text file format converter

使用例子:

[root@moban ~]# dos2unix dos.txt unix.txt

与之相反的命令是unix2dos      将Unix的文本格式转换为DOS格式

[root@moban ~]# unix2dos unix.txt dos.txt


二十五、diff      比较两个文件的不同          compare files line by line

  命令重要参数:

  -y        以并列的方式显示文件的异同

  -W         在使用-y参数的时候指定屏幕的宽度

  -c        使用上下文的输出格式

  -u        使用同一的格式输出

使用例子:

1.使用默认的diff语句比对a和b两个文件的差异

[root@moban ~]# diff a b
2a3
> 444
4a6,8
> 555
>
>

2.为了便于查看用并列的方式展示文件a和b的差异

[root@moban ~]# diff -y a b
123 123
123123 123123
> 444
123123123 123123123
456 456
> 555
>
>
456 456
45666 45666

3.使用上下文的方式比较文件a和b的不同

[root@moban ~]# diff -c a b
*** a 2018-02-02 17:28:10.392620394 +0800
--- b 2018-02-02 17:20:09.579623783 +0800
***************
*** 1,6 ****
123
123123
456
45666
- 777
- 888
--- 1,10 ----
123
123123
+ 444
+ 123123123
+ 456
+ 555
+
+
456
45666

4.使用同一的格式比较文件a和b的异同

[root@moban ~]# diff -u a b
--- a 2018-02-02 17:28:10.392620394 +0800
+++ b 2018-02-02 17:20:09.579623783 +0800
@@ -1,6 +1,10 @@
123
123123
+444
+123123123
+456
+555
+
+
456
45666
-777
-888

5.比较/etc/rc3.d/和/etc/rc6.d/两个目录下文件的不同(由于目录下文件不同的比较多,输出部分做了截取)

[root@moban ~]# diff /etc/rc3.d/ /etc/rc6.d/
Only in /etc/rc6.d/: S01reboot
Only in /etc/rc3.d/: S01sysstat


二十六、 rev      反向输出文件内容(rev命令是这个字符的反转的)          reverse lines of a file or files

使用例子:

[root@moban ~]# seq -s " " 1 10
1 2 3 4 5 6 7 8 9 10
[root@moban ~]# seq -s " " 1 10 | rev
01 9 8 7 6 5 4 3 2 1


二十七、tr      替换或者删除字符            translate or delete characters

  命令重要参数:

  -d      删除字符

  -s      保留连续字符的第一个字符,删除后面的重复

  -c      使用第一个字符串(set1)的补集,取反

使用例子:

1.将文件中的字符abc转换为xyz

[root@moban ~]# cat duan.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq is 49000448.

not 4900000448.
my god,i am not oldbey,but OLDBOY!
[root@moban ~]# tr 'abc' 'xyz' < duan.txt            <==========>注意这里的转换方式是一个字符对应一个字符的
I xm oldyoy texzher!
I texzh linux.

I like yxdminton yxll,yillixrd yxll xnd zhinese zhess!
my ylog is http://oldyoy.ylog.51zto.zom
our site is http://www.etixntixn.org
my qq is 49000448.

not 4900000448.
my god,i xm not oldyey,yut OLDBOY!

2.使用tr命令将文件的大小写统一为小写

[root@moban ~]# tr '[A-Z]' '[a-z]' < duan.txt
i am oldboy teacher!
i teach linux.

i like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq is 49000448.

not 4900000448.
my god,i am not oldbey,but oldboy!

3.将文件中的0-9替换为a-j [当替换的字符长度比被替换的字符长度短的时候,多余的被替换字符会使用最后一个替换字符来代替;当被替换字符长度短的时候还是一一对应]

[root@moban ~]# tr '0-9' 'a-j' < duan.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.fbcto.com
our site is http://www.etiantian.org
my qq is ejaaaeei.

not ejaaaaaeei.
my god,i am not oldbey,but OLDBOY!

[root@moban ~]# echo 0123456789 | tr '0-9' 'a-d'
abcddddddd

[root@moban ~]# echo 0123456789 | tr '0-1' 'a-d'
ab23456789

4.删除文件中出现的oldboy中的每个字符

[root@moban ~]# cat duan.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com
our site is http://www.etiantian.org
my qq is 49000448.

not 4900000448.
my god,i am not oldbey,but OLDBOY!
[root@moban ~]# tr -d 'oldboy' < duan.txt                       <================> tr删除字符的时候也是按照每个字符去删除的,而不是做为字符串处理的
I am teacher!
I teach inux.

I ike amintn a,iiar a an chinese chess!
m g is http://.g.51ct.cm
ur site is http://www.etiantian.rg
m qq is 49000448.

nt 4900000448.
m g,i am nt e,ut OLDBOY!

5.删除文件中的换行符

[root@moban ~]# tr -d '\n' < duan.txt
I am oldboy teacher!I teach linux.I like badminton ball,billiard ball and chinese chess!my blog is http://oldboy.blog.51cto.comour site is http://www.etiantian.orgmy qq is 49000448.not 4900000448.my god,i am not oldbey,but OLDBOY![root@moban ~]#

 

6.删除连续字符

[root@moban ~]# echo "oooooooollllllldddddbbbooooooyyyyyyyyy" | tr -s oldboy
oldboy

7.替换文本中所有非数字的字符

[root@moban ~]# tr '0-9' '*' < duan.txt
I am oldboy teacher!
I teach linux.

I like badminton ball,billiard ball and chinese chess!
my blog is http://oldboy.blog.**cto.com
our site is http://www.etiantian.org
my qq is ********.

not **********.
my god,i am not oldbey,but OLDBOY!
[root@moban ~]# tr -c '0-9' '*' < duan.txt
**************************************************************************************************************************51******************************************************49000448*******4900000448*************************************[root@moban ~]#
[root@moban ~]# tr -c '0-9\n' '*' < duan.txt
********************
**************

******************************************************
******************************51*******
************************************
*********49000448*

****4900000448*
********************************** 


二十八、tee    多重定向(可以把数据同时重定向到屏幕和文件中)          read from standard input and write to standard output and files

  命令重要参数:

  -a      向文件追加内容,而不是覆盖

使用例子:

1.显示/tmp目录下的文件,并将结果写入tmp文件;单后显示当前目录下的文件,并将结果追加到tmp文件

[root@moban ~]# ls /tmp | tee tmp
[root@moban ~]# ls . | tee -a tmp


二十九、 

    

posted on 2018-01-30 17:52  吃胖了的包子  阅读(66)  评论(0)    收藏  举报