Linux 命令 常用总结(二)
目录
(21):tree
命令简介
tree 命令的作用是以树形结构显示目录下的内容。
常用的Linux发行版系统中默认没有这个命令,你需要通过安装才可以使用:
#Centos
yum install tree -y
#Ubuntu
sudo apt-get install tree
安装完成之后就可以正常使用这个tree命令了。
语法格式
tree [选项] [目录]
选项说明
-a #显示所有文件
-d #只显示目录(名称)
-l #显示链接文件的原始文件
-f #显示所列出的文件或目录的完整目录路径
-i #不以阶梯的形式显示文件或目录名称
-q #将控制字符以?字符代替,显示文件和目录名称
-N #直接显示文件或目录的名称
-p #显示每个文件的权限信息
-u #显示文件所有者或者uid
-g #显示文件所属组或者gid
-s #显示每个文件的大小信息
-h #以可读的方式显示文件的大小信息
-D #显示最后修改日期
-v #按字母数字正序显示文件
-r #按字母数字倒序显示文件
-t #按最后时间排序显示文件
-C #在文件和目录列表上加上色彩,便于区分文件类型
-P pattern #只显示匹配正则表式的文件或目录名称
-I pattern #与上结果相反
应用举例
#树形显示当前目录及其子目录下的文件及目录名称
[root@centos7 testdir]# tree
.
├── dir
│ ├── test2.txt~
│ ├── test3.txt
│ └── test3.txt.bak
├── test2.txt
└── test2.txt~
1 directory, 5 files
#只显示目录名称
[root@centos7 testdir]# tree -d
.
└── dir
1 directory
#显示目录及文件的权限信息
[root@centos7 testdir]# tree -p
.
├── [drwxr-xr-x] dir
│ ├── [-rw-r--r--] test2.txt~
│ ├── [-rw-r--r--] test3.txt
│ └── [-rw-r--r--] test3.txt.bak
├── [-rw-r--r--] test2.txt
└── [-rw-r--r--] test2.txt~
1 directory, 5 files
#显示几层信息2代表2层(向下)
[root@centos7 testdir]# tree -L 2
.
├── dir
│ ├── test2.txt~
│ ├── test3.txt
│ └── test3.txt.bak
├── test2.txt
└── test2.txt~
1 directory, 5 files
[root@centos7 testdir]# tree -L 1
.
├── dir
├── test2.txt
└── test2.txt~
1 directory, 2 files
-C 显示各种文件类型,以颜色区分
#显示文件和目录的所有者
[root@centos7 testdir]# tree -u
.
├── [root ] cp -> /usr/bin/cp
├── [root ] dir
│?? ├── [root ] test2.txt~
│?? ├── [root ] test3.txt
│?? └── [root ] test3.txt.bak
├── [root ] test2.txt
└── [root ] test2.txt~
1 directory, 6 files
#显示文件和目录的所属组
[root@centos7 testdir]# tree -g
.
├── [root ] cp -> /usr/bin/cp
├── [root ] dir
│?? ├── [root ] test2.txt~
│?? ├── [root ] test3.txt
│?? └── [root ] test3.txt.bak
├── [root ] test2.txt
└── [root ] test2.txt~
1 directory, 6 files
#不按树形形式显示文件和目录
[root@centos7 testdir]# tree -i
.
cp -> /usr/bin/cp
dir
test2.txt~
test3.txt
test3.txt.bak
test2.txt
test2.txt~
1 directory, 6 files
[root@centos7 testdir]# tree
.
├── cp -> /usr/bin/cp
├── dir
│ ├── test2.txt~
│ ├── test3.txt
│ └── test3.txt.bak
├── test2.txt
└── test2.txt~
1 directory, 6 files
#显示文件和目录的完整路径
[root@centos7 testdir]# pwd
/root/testdir
[root@centos7 testdir]# tree -f
.
├── ./cp -> /usr/bin/cp
├── ./dir
│ ├── ./dir/test2.txt~
│ ├── ./dir/test3.txt
│ └── ./dir/test3.txt.bak
├── ./test2.txt
└── ./test2.txt~
1 directory, 6 files
[root@centos7 ~]# tree -f ./testdir/
./testdir
├── ./testdir/cp -> /usr/bin/cp
├── ./testdir/dir
│ ├── ./testdir/dir/test2.txt~
│ ├── ./testdir/dir/test3.txt
│ └── ./testdir/dir/test3.txt.bak
├── ./testdir/test2.txt
└── ./testdir/test2.txt~
1 directory, 6 files
tree 命令不难掌握,常用的选项不多,掌握上面的例子中的常见用法之后,在日常工作中应该是够用了。
(22):pwd
命令简介
pwd (print working directory)显示用户当前工作目录的绝对路径。
pwd 命令将当前用户的工作目录的全路径名称(从根目录)以绝对路径的方式标准输出在屏幕上。
语法格式
tree [选项] [.....]
pwd [OPTION]...
选项说明
-P #打印当前目录的完整路径信息
--help #显示帮助信息
--version #显示版本信息
应用举例
#打印当前工作目录路径
[root@centos7 dir]# pwd
/root/testdir/dir
#下面就是有链接文件
[root@centos7 var]# ll mail
lrwxrwxrwx. 1 root root 10 Aug 20 10:31 mail -> spool/mail
[root@centos7 mail]# pwd
/var/mail
[root@centos7 mail]# pwd -P
/var/spool/mail
pwd命令非常简单,基本上只会用到打印当前工作目录的功能,所以掌握起来很容易,同cd/ls/shutdown等一些命令一样,属于入门的最基础命令。
(23):file
命令简介
file 命令用于查看指定文件的类型。
在Linux系统中,一切皆文件。这里就不得不提一下Linux系统中的文件类型:
普通文件 #属性信息表示为 -
目录文件 #属性信息表示为 d
链接文件 #属性信息表示为 l
套接字文件 #属性信息表示为 s
字符设备文件 #属性信息表示为 b
块设备文件 #属性信息表示为 c
管道文件 #属性信息表示为 p
文件的属性信息在之前的文章: 每天学一个 Linux 命令(17):chmod 中有介绍,文件类型信息一般都是位于文件权限信息之首的位置。
[root@centos7 testdir]# ll
total 0
lrwxrwxrwx 1 root root 11 Jan 15 22:50 cp -> /usr/bin/cp
drwxr-xr-x 2 root root 62 Jan 2 09:15 dir
-rw-r--r-- 1 root root 0 Jan 2 09:03 test2.txt
-rw-r--r-- 1 root root 0 Jan 2 08:57 test2.txt~
语法格式
file [选项] [文件名或目录名]
选项说明
-b:#列出结果,但不显示文件名称
-c:#详细显示指令执行过程
-L:#显示链接文件的源文件类型
-m<魔法数字文件>:#指定魔法数字文件
-v:#打印出版本信息
-z:#查看压缩文件的内容
应用举例
#查看文件类型
[root@centos7 testdir]# file cp
cp: symbolic link to `/usr/bin/cp`
[root@centos7 testdir]# file dir
dir: directory
[root@centos7 testdir]# file test2.txt
test2.txt: empty
[root@centos7 testdir]# file test2.txt~
test2.txt~: empty
#直接显示结果,不显示文件名
[root@centos7 testdir]# file -b dir
directory
#解读压缩文件的内容
[root@centos7 ~]# file -z httpd-2.4.46.tar.gz
httpd-2.4.46.tar.gz: POSIX tar archive (GNU) (gzip compressed data, was "httpd-2.4.46.tar", from Unix, last modified: Sat Aug 1 10:12:01 2020, max compression)
(24):chattr/lsattr
命令简介
chattr 用来改变文件(扩展)属性。通常我们叫这个属性为特殊属性。
lsattr 查看文件扩展属性。
这种特殊属性有以下8种模式:
a #限定文件具有某种功能。这个对于日志文件非常有作用,比如,不允许删除,只允许向里面追加内容。
b #不更新或修改文件或目录的最后存储时间
c #将文件或目录压缩后存储
d #将文件或目录排除在倾倒操作之外
i #锁定文件或目录,不被更改
s #秘密删除文件或目录
S #动态实时更新文件或目录
u #防止文件或目录被意外删除
如果文件或目录被配置了上述8种之一的扩展属性,通过lsattr命令可以查看到:
[root@centos7 testdir]# lsattr
----i----------- ./dir
#结果说明这个目录被配置扩展属性
[root@centos7 testdir]# chattr -i dir
[root@centos7 testdir]# lsattr
---------------- ./dir
语法格式
chattr [选项] mode file
lsattr [选项] file
选项说明
chattr选项说明
-R #递归处理,将目录下的所有文件及子目录都加上此扩展属性
-v<版本编号> #设置文件或目录版本号
-V #显示指令执行过程;
+ <属性> #添加文件或目录的某个扩展属性
- <属性> #解除文件或目录的某个扩展属性
= <属性> #指定某个扩展性至指定的文件或目录
lsattr选项说明
-E #显示属性的当前值
-D #显示扩展属性的名称
-R #递归
-V #显示版本信息
-a #列出目录下所有文件,包括隐藏文件
#注意-E -D -R三个选项是不可以一起使用的,它们是相互排斥的。
应用举例
#防止误删除的作用
[root@centos7 testdir]# chattr +a test2.txt
[root@centos7 testdir]# lsattr test2.txt
-----a---------- test2.txt
#不允许直接替换,只允许追加内容
[root@centos7 testdir]# echo "12345">test2.txt
-bash: test2.txt: Operation not permitted
#查看文件或目录有没有扩展属性
[root@centos7 testdir]# echo "12345">>test2.txt
[root@centos7 testdir]# lsattr
---------------- ./dir
---------------- ./test2.txt~
-----a---------- ./test2.txt
lsattr: Operation not supported While reading flags on ./cp
这两个命令在日常工作也会常用到,锁定文件,或者当文件或系统出现故障时用于排查。
[root@centos7 testdir]# lsattr
---------------- ./dir
---------------- ./test2.txt~
----i----------- ./test2.txt
lsattr: Operation not supported While reading flags on ./cp
[root@centos7 testdir]# echo "12345">>test2.txt
-bash: test2.txt: Permission denied
比如上述情况,如果此文件是一个要实时存储数据的文件,被锁定了,那么就可能会引发应用运行故障,所以,这时就可以通过lsattr命令来查看与排查。
(25):more
命令简介
more 分页显示文件内容。
以全屏的方式来显示文件的内容,每次显示一页,可按键向下翻页。more 命令中自带了很多快捷键,H(获得帮助信息),Enter(向下翻一行),空格(向下滚一屏),Q(退出)。
当你打开一个文件时,可以使用下面的快捷键进行相关的操作:
Space(空格)键 #向下翻动一页
Enter(回车)键 #显示文本的下一行
| #输入一个模式,可以在当前文本内容查找一个相匹配的模式
H 键 #显示帮助信息
B 键 #向上翻一页
Q 键 #退出
命令格式
more [选项] [文件名]
more [option] [file ...]
选项说明
-<数字> #指定每页显示的行数
-c #不进行滚屏操作
-s #将多个空行压缩成一行显示
-u #去除下划线
+<数字> #从指定的行开始显示
应用举例
#查看文件mingongge的内容,在显示之前先清屏并且显示当前页数的百分比
more -dc mingongge
#显示文件mingongge的内容,每100行显示一次,显示之前清屏
more -c -100 mingongge
#从第十行开始显示mingongge.txt文件内容
more +10 mingongge.txt
#从包含字符串 "mingonggee" 行开始显示mingongge.txt文件内容
more +/"mingonggee" mingonggee.txt
#用ls打印当前目录列表,配合more分页显示
ls | more
常用的用法基本就这些,也属于比较常用且简单的命令。
(26):less
命令简介
less 命令分页上下翻页浏览文件内容。
less 命令与 more 命令相似,都可以用来查看文件内容,不同的是 less 命令可以向前或向后查看文件,而 more 命令只能向前查看。
用 less 命令显示文件时,按 PageUp 键向上翻页,按PageDown 键向下翻页,按 Q 键退出查看。
命令格式
less [选项] [文件名]
less [option] [file ...]
选项说明
-e #文件内容显示完成后,自动退出
-f #强制显示文件
-g #不加亮显示
-l #忽略大小写
-N #行首显示行号
-s #合并多个空行只显示一行
-S #不换行显示内容
-x<数字> #将TAB字符显示为指定个数的空格字符
应用举例
#分页显示mingongge.log文件内容
less /var/log/mingongge.log
#显示文件mingongge的内容,并在每行行首加上行号
less -N mingongge.txt
#下/上一页:
<Space> (down), b (up)
#less 命令转到文件的末尾/开始:
G (end), g (start)
#向前查找字符串(按 n N 跳转到下一个/上一个)
/mingongge
#向后查找字符串(按 n N 跳转到下一个/上一个)
?mingongge
#退出 less 命令
q
常用的用法基本就这些,与more命令一样,都属于比较常用且简单的命令。
(27):head
命令简介
head 显示文件内容的头部。在系统默认情况下,head 命令只显示文件的头10 行内容。
命令格式
head [选项] [文件名]
head [OPTION] [FILE]
选项说明
-n<数字> #按指定行数显示(从文件开头位置计算)
-c<字符数> #按指定的字符数显示(从文件的第一个字符开始计算)
-v #显示文件名信息
-q #不显示文件名信息
--help #打印帮助信息后退出
--version #打印版本信息后退出
应用举例
#显示文件mingongge.txt前150行内容(如果没有其它参数,可以不需要-n)
head -150 mingongge.txt
#显示文件mingongge1.txt 和mingongge2.txt的前100行内容
head -100 mingongge1.txt mingongge2.txt
[root@centos7 testdir]# head -100 mingongge1.txt mingongge2.txt
==> mingongge1.txt <==
11111111111111111111
22222222222222222222222
33333333333333333333333333
444444444444444444444444
....
==> mingongge2.txt <==
head1
head2
head3
head4
....
#注意:默认情况,一次显示多个文件的话,默认是显示文件名信息的,可以不加-v参数。
[root@centos7 testdir]# head -q -n 3 mingongge1.txt mingongge2.txt
11111111111111111111
22222222222222222222222
33333333333333333333333333
head1
head2
head3
常用的用法基本就这些,与more\less命令一样,都属于比较常用且简单的命令。
(28):ln
命令简介
ln 命令用于创建(软/硬)链接文件。
在linux系统中,有两种链接类型:硬链接与软件,默认创建的就是硬链接,创建软链接需要-s选项来配合完成。
硬链接文件是指通过索引节点来进行链接,在Linux系统中多个文件同时指向同一个索引节点,这种情况下的文件被称为硬链接文件。
软链接文件也称做符号链接(同Windows系统中快捷方式)。实际上它是一个文本文件,文本文件里存储着指向源文件链接的位置信息。
命令格式
ln [选项] [链接文件名]
ln [OPTION] [LINKNAME]
选项说明
-b #创建备份文件
-f #强行删除任何已存在的目标文件
-i #覆盖现有文件前先询问用户
-s #给一个文件创建软链接
-v #打印每个链接文件的名称
--help #打印帮助信息后退出
--version #打印版本信息后退出
应用举例
#在当前目录中创建硬链接
[root@centos7 testdir]# ln test2.txt test3
[root@centos7 testdir]# ls -li test*
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test2.txt
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test3
#从结果可以看出硬链接文件与源文件的inode号一样
#在当前目录中创建软链接
[root@centos7 testdir]# ln -s test2.txt test4
[root@centos7 testdir]# ls -li test*
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test2.txt
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test3
50342753 lrwxrwxrwx 1 root root 9 Jan 16 03:57 test4 -> test2.txt
#从结果可以看出,软链接与源文件的inode号不一样
#创建源文件test2.txt的软件链接文件名为test3,如果test3存,则将其改名为test3~
[root@centos7 testdir]# ln -s -b test2.txt test3
[root@centos7 testdir]# ls -li test*
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test2.txt
50342767 lrwxrwxrwx 1 root root 9 Jan 16 04:06 test3 -> test2.txt
50342754 -rw-r--r-- 2 root root 6 Jan 16 02:47 test3~
50342753 lrwxrwxrwx 1 root root 9 Jan 16 03:57 test4 -> test2.txt
基础入门必备命令之一,非常简单易学,掌握这些就够了。
(29):tail
命令简介
tail 显示文件内容的尾部,默认也是显示指定文件的末尾10行。
tail 命令还可以查看文件实时写入的数据,例如,我们常用它来查看应用运行的日志文件,可以动态实时显示应用运行所产生的日志,便于排错或查看应用运行是否正常。
命令格式
tail [选项] [链接文件名]
tail [OPTION] [LINKNAME]
选项说明
-f #显示文件最新追加的内容,实时动态展示一个文件的写入数据
-n<N> #输出文件的尾部N(N位数字)行内容。
--pid=<进程号> #与“-f”选项配合,如果指定进程号的进程终止后,自动退出
-v #显示文件名信息
-q #不显示文件名信息
--help #打印帮助信息
--version #打印版本信息
应用举例
#显示文件mingongge从第200行至文件末尾的内容
tail -n +200 mingongge
#显示文件mingongge的最后100个字符
[root@centos7 ~]# tail -c 100 mingongge
#显示mingongge.log最后的250行
[root@centos7 ~]# tail -250 mingongge.log
#实时展示mingongge.log文件的写入数据
[root@centos7 ~]# tail -f mingongge.log
#带文件名与不带文件名举例
[root@centos7 ~]# tail -v -n 10 /etc/passwd /etc/shadow
==> /etc/passwd <==
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
==> /etc/shadow <==
operator:*:17834:0:99999:7:::
games:*:17834:0:99999:7:::
ftp:*:17834:0:99999:7:::
nobody:*:17834:0:99999:7:::
systemd-network:!!:18494::::::
dbus:!!:18494::::::
polkitd:!!:18494::::::
sshd:!!:18494::::::
postfix:!!:18494::::::
chrony:!!:18494::::::
[root@centos7 ~]# tail -q -n 10 /etc/passwd /etc/shadow
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
operator:*:17834:0:99999:7:::
games:*:17834:0:99999:7:::
ftp:*:17834:0:99999:7:::
nobody:*:17834:0:99999:7:::
systemd-network:!!:18494::::::
dbus:!!:18494::::::
polkitd:!!:18494::::::
sshd:!!:18494::::::
postfix:!!:18494::::::
chrony:!!:18494::::::
基础入门必备命令之一,非常简单易学,掌握这些就够了。
(30):cut
命令简介
cut 将文件中行中内容按指定分隔符分割并输出。
cut命令还可以用于删除文件中指定行或段,然后打印输出更改后的内容。还可能用以拼接文件内容到一个新的文件中,功能和cat类似。
命令格式
cut [选项] [链接文件名]
cut [OPTION] [LINKNAME]
选项说明
-b #只显示行中指定(字节数)的内容
-c #只显示行中指定(字符数)的内容
-d #指定字段的分隔符,默认为“TAB”
-f #打印指定字段(列)的内容
-n #与“-b”选项连用,不分割多字节字符
-s #不打印不包含定界符的行的内容
--help #打印帮助信息
--version #打印版本信息
cut命令中指定字节或字符范围的说明如下:
N #从1字节、字符或字段开始到第N个字节、字符或字段
N- #从第N个字节、字符或字段到行的结尾
N-M #从第N个字节、字符或字段到第M个字节,字符或字段
-M #从第1个字节、字符或字段到第M个字节、字符或字段
注意:所有的范围取值需为整数,如:10,10-,10-20,-20。
应用举例
#打印指定字节数的内容
[root@centos7 testdir]# cat mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
[root@centos7 testdir]# cut -b 3 mingongge1.txt
1
2
3
4
#截取指定字段内容
[root@centos7 testdir]# cat cuttest.txt
1 2 3 4 5 6 8
9 8 7 6 5 4 3
2 1 9 8 7 6 5
#以空格为分隔,打印每一行的第一列
[root@centos7 testdir]# cut -f1 -d" " cuttest.txt
1
9
2
#以空格为分隔,打印每一行的第一列和第三列
[root@centos7 testdir]# cut -f1,3 -d" " cuttest.txt
1 3
9 7
2 9
#以空格为分隔,打印每一行的第三列到结尾
[root@centos7 testdir]# cut -f3- -d" " cuttest.txt
3 4 5 6 8
7 6 5 4 3
9 8 7 6 5
#截取每一行第2-5个字符
[root@centos7 testdir]# cut -c 2-5 cuttest.txt
2 3
8 7
1 9
#截取每一行第一个到第五个字符
[root@centos7 testdir]# cut -c -5 cuttest.txt
1 2 3
9 8 7
2 1 9
#截取每一行第五个到最后一个字符
[root@centos7 testdir]# cut -c 5- cuttest.txt
3 4 5 6 8
7 6 5 4 3
9 8 7 6 5
指定分隔符截取内容的用法非常实用,工作中也经常使用。
(31):md5sum
命令简介
md5sum 用于计算和校验文件的MD5值。
md5sum 常常被用来验证网络文件传输的完整性,防止文件被人篡改。在日常工作当中,我们可以用来监控系统中的重要文件是否被篡改。
还可以使用使用 md5sum 生成文件或用户的密码。
语法格式
md5sum [选项] [文件]
选项说明
-b #使用二进制模式对文件进行读取
-t #把输入的文件看作是文本文件
-c #从指定文件中读取MD5校验值,并进行校验
--status #校验成功时不输出任何信息
-w #当校验不正确时输出警告信息
应用举例
生成密码或随机数值
[root@centos7 ~]# date | md5sum
1b1f0ba711e7d4931c23fbbd2b328e40 -
检查一个文件的 md5 值
[root@centos7 testdir]# md5sum mingongge1.txt
c5cab5a45a72380ce456a4370bf40348 mingongge1.txt
检查一个文件是否被更改
#提取文件原md5值
[root@centos7 testdir]# md5sum mingongge1.txt >./mingongge.txt.md5
[root@centos7 testdir]# cat mingongge.txt.md5
c5cab5a45a72380ce456a4370bf40348 mingongge1.txt
#写入内容
[root@centos7 testdir]# cat >> mingongge1.txt << EOF
> 1
> 2
> 3
> 4
> EOF
[root@centos7 testdir]# md5sum mingongge1.txt -c mingongge.txt.md5
mingongge1.txt: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
(32):sort
命令简介
sort 对文件的文本内容排序。
系统默认情况下,排序规则如下:
-
以数字开头的行,将排在以字母开头的行前面
-
以小写字母开头的行,将排在以大写字母开头的行前面
-
按字母表的顺序排列以字母开头的行
语法格式
sort [选项] [文件]
sort [OPTION] [FILE]
选项说明
-b #排除开头的空白
-d #只考虑空白、字母、数字
-f #将小写字母视为大写字母考虑
-g #根据数字排序
-i #排除不可打印字符
-M #按非月份的顺序排序
-h #根据存储容量排序
-n #根据数字排序。
-R #随机排序
-r #倒序
--sort=WORD #根据指定的WORD排序
-V #按文本中(版本)数字的自然排序
-o #将排序结果写入一个文件
--help #显示帮助信息并退出
--version #显示版本信息并退出
应用举例
[root@centos7 testdir]# cat cuttest.txt
1 2 3 4 5 6 8
9 8 7 6 5 4 3
2 1 9 8 7 6 5
[root@centos7 testdir]# sort cuttest.txt
1 2 3 4 5 6 8
2 1 9 8 7 6 5
9 8 7 6 5 4 3
#将结果输出到文件
[root@centos7 testdir]# sort -o sort.cut.txt cuttest.txt
[root@centos7 testdir]# cat sort.cut.txt
1 2 3 4 5 6 8
2 1 9 8 7 6 5
9 8 7 6 5 4 3
#倒序排列
[root@centos7 testdir]# sort -r cuttest.txt
9 8 7 6 5 4 3
2 1 9 8 7 6 5
1 2 3 4 5 6 8
(33):uniq
命令简介
uniq 命令用于去除文件中重复行,一般与 sort 命令结合使用。
语法格式
uniq [选项] [标准输入 [输出]]
uniq [OPTION] [INPUT [OUTPUT]]
输入文件 #指定要去除的重复行文件。如果不指定该项,则从标准读入
输出文件 #指定要去除重复行后的内容要写入的输出文件。如果不指定此项,则将内容显示到标准输出设备(显示终端)。
选项说明
-c #在每列旁边显示该行重复出现的次数
-d #只显示重复出现的行与列
-f #忽略比较指定的字段
-s #忽略比较指定的字符
-i #不区分大小写的比较
-u #只显示出现过一次的行与列
-w #指定要比较的字符
-z #用0字节(NULL)代替换行符
--help #显示帮助信息并退出
--version #显示版本信息并退出
应用举例
#删除重复行
[root@centos7 ~]# cat test.txt
This is a test line
This is a test line
This is a test line
This is also a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# uniq test.txt
This is a test line
This is also a test line
This is also also a test line
[root@centos7 ~]# sort test.txt | uniq
This is also also a test line
This is also a test line
This is a test line
#只显示单一行
[root@centos7 ~]# uniq -u test.txt
This is also also a test line
[root@centos7 ~]# sort test.txt |uniq -u
This is also also a test line
#统计各行在文件中出现的次数
[root@centos7 ~]# sort test.txt |uniq -c
1 This is also also a test line
2 This is also a test line
3 This is a test line
#在文件中找出重复的行
[root@centos7 ~]# sort test.txt |uniq -d
This is also a test line
This is a test line
(34):wc
命令简介
wc 命令用来统计文件中的行数、单词数或字节数,然后将结果输出在终端上。我们可以使用 wc 命令来计算文件的Byte数、字数或是列数。
语法格式
wc [选项] [文件]
wc [OPTION] [FILE]
选项说明
-c #统计字节数
-l #统计行数
-m #统计字符数
-w #统计字数
-L #显示最长行的长度
-help #显示帮助信息
--version #显示版本信息
应用举例
查看文件的字节数、字数、行数
[root@centos7 ~]# wc test.txt
6 34 140 test.txt
[root@centos7 ~]# wc mingongge.file
0 0 0 mingongge.file
[root@centos7 ~]# wc -L test.txt
29 test.txt
[root@centos7 ~]# wc -m test.txt
140 test.txt
[root@centos7 ~]# wc -l test.txt
6 test.txt
[root@centos7 ~]# wc -c test.txt
140 test.txt
统计当前目录下的所有文件行数及总计行数
[root@centos7 ~]# wc -l *
48 anaconda-ks.cfg
wc: goinception: Is a directory
0 goinception
45222 goInception-linux-amd64-v1.2.3.tar.gz
wc: httpd-2.4.46: Is a directory
0 httpd-2.4.46
36246 httpd-2.4.46.tar.gz
0 mingongge.file
wc: testdir: Is a directory
0 testdir
6 test.txt
81522 total
配合命令输出结果来统计
[root@centos7 ~]# ls -l |wc -l
9
[root@centos7 ~]# ls -l
total 21888
-rw-------. 1 root root 1320 Aug 20 10:39 anaconda-ks.cfg
drwxr-xr-x 3 root root 39 Aug 30 03:48 goinception
-rw-r--r-- 1 root root 13034487 Aug 30 03:42 goInception-linux-amd64-v1.2.3.tar.gz
drwxr-sr-x 11 root 40 4096 Dec 24 22:35 httpd-2.4.46
-rw-r--r-- 1 root root 9363314 Aug 5 07:32 httpd-2.4.46.tar.gz
-rw-r--r-- 1 root root 0 Jan 2 08:43 mingongge.file
drwxr-xr-x 3 root root 210 Jan 16 10:19 testdir
-rw-r--r-- 1 root root 140 Jan 16 10:30 test.txt
(35):dos2unix
命令简介
dos2unix 命令用于将纯文本文件从 DOS 或 Mac 格式转换为 Unix。DOS 下的文本文件是以 \r\n 作为换行符,而 Unix 下的文本文件是以 \n 作为换行符。
默认系统是没有安装这个命令,需要用户自行安装:
[root@centos7 ~]# dos2unix test.txt
-bash: dos2unix: command not found
#CentOS/RHEL 安装
[root@centos7 ~]# yum install -y dos2unix
#Debian/Ubuntu 安装
[root@centos7 ~]# apt-get install dos2unix
语法格式
dos2unix [选项] [文件]
dos2unix [OPTION] [FILE]
选项说明
-k #输出文件的日期不变
-q #安静模式
-V #查看版本
-c #转换模式,模式有:ASCII, 7bit, ISO, Mac, 默认是:ASCII。
-o #写入到源文件
-n #写入到新文件
应用举例
最简单的用法
[root@centos7 ~]# dos2unix test.txt
dos2unix: converting file test.txt to Unix format ...
一次转换多个文件(注:也可以加上 -o 参数,也可以不加,效果一样)
[root@centos7 ~]# dos2unix test.txt mingongge.file
dos2unix: converting file test.txt to Unix format ...
dos2unix: converting file mingongge.file to Unix format ...
[root@centos7 ~]# dos2unix -o test.txt mingongge.file
dos2unix: converting file test.txt to Unix format ...
dos2unix: converting file mingongge.file to Unix format ...
转换时保持源文件不变(默认是直接修改源文件),可以使用 -n 参数
[root@centos7 ~]# ll test.txt
-rw-r--r-- 1 root root 140 Jan 16 11:32 test.txt
[root@centos7 ~]# dos2unix -n test.txt dos_test.txt
dos2unix: converting file test.txt to file dos_test.txt in Unix format ...
[root@centos7 ~]# ll test.txt
-rw-r--r-- 1 root root 140 Jan 16 11:32 test.txt
[root@centos7 ~]# ll dos_test.txt
-rw-r--r-- 1 root root 140 Jan 16 11:34 dos_test.txt
如果要保持文件时间戳不变,加上 -k 参数
[root@centos7 ~]# ll
total 21892
-rw-r--r-- 1 root root 140 Jan 16 11:34 dos_test.txt
-rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
drwxr-xr-x 3 root root 210 Jan 16 10:19 testdir
-rw-r--r-- 1 root root 140 Jan 16 11:32 test.txt
[root@centos7 ~]# dos2unix dos_test.txt
dos2unix: converting file dos_test.txt to Unix format ...
[root@centos7 ~]# ll
total 21892
-rw-r--r-- 1 root root 140 Jan 16 11:36 dos_test.txt
-rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
drwxr-xr-x 3 root root 210 Jan 16 10:19 testdir
-rw-r--r-- 1 root root 140 Jan 16 11:32 test.txt
#可以看出默认是改变了文件的时间戳信息
[root@centos7 ~]# dos2unix -k test.txt
dos2unix: converting file test.txt to Unix format ...
[root@centos7 ~]# dos2unix -k dos_test.txt mingongge.file
dos2unix: converting file dos_test.txt to Unix format ...
dos2unix: converting file mingongge.file to Unix format ...
[root@centos7 ~]# ll
total 21892
-rw-r--r-- 1 root root 140 Jan 16 11:36 dos_test.txt
-rw-r--r-- 1 root root 0 Jan 16 11:32 mingongge.file
drwxr-xr-x 3 root root 210 Jan 16 10:19 testdir
-rw-r--r-- 1 root root 140 Jan 16 11:32 test.txt
(36):diff
命令简介
diff 命令用于查找、分析两个文件中不同的行,并打印输出在屏幕上。
diff 命令是以逐行的方式,比较文本文件的不同之处。如果是对指定的目录进行比较,就是比较该指定目录下的同名文件,不会对该目录的子目录下的文件进行比较操作。
diff 命令最基础的用途就是用来比较指定的两个文件的不同。
语法格式
diff [选项] [文件]
diff [OPTION] [FILE]
选项说明
-<行数> #指定要显示的行数,必须与-c或-u参数一并使用
-a #逐行比较文件
-b #不对空格进行比较
-B #不对空行进行比较
-c #显示全部内容,并标出不同之处
-C<行数> #与执行“-c-<行数>”指令相同
-d #以小的单位来做比较
-H #加速比较大文件
-n #将比较结果以RCS的格式显示
-q #只需显示有无差异,不需要显示其它信息
-r #对子目录的文件进行比较
-s #如果两个文件没有差异,也给出相关的信息
-S #比较两个目录时,从指定的文件开始执行比较动作
-u #以合并的方式来显示文件内容的不同
-v #显示版本信息
-w #忽略所有空格字符
-y #以并列的方式显示两个文件的差异之处
--help #显示帮助
应用举例
比较两个文件的不同之处
[root@centos7 testdir]# diff mingongge1.txt mingongge2.txt
1,8c1,4
< 1111 11 111111 1 111 1 1 11
< 22222222222 222 2222 22 2 2 2
< 33333333333 333333 3333 333 33
< 444444444444 444 444444444
< 1
< 2
< 3
< 4
---
> head1
> head2
> head3
> head4
#a表示添加,c表示更改,d表示删除
#以<开头的行是第一个文件中的行
#以>开头的行是第二个文件中的行
[root@centos7 testdir]# diff -c mingongge1.txt mingongge2.txt
*** mingongge1.txt 2021-01-16 09:55:49.489792550 -0500
--- mingongge2.txt 2021-01-16 03:36:24.645284332 -0500
***************
*** 1,8 ****
! 1111 11 111111 1 111 1 1 11
! 22222222222 222 2222 22 2 2 2
! 33333333333 333333 3333 333 33
! 444444444444 444 444444444
! 1
! 2
! 3
! 4
--- 1,4 ----
! head1
! head2
! head3
! head4
diff 比对字符含义
! #表示此行是一个或多个需要更改的行的一部分
+ #表示第二个文件中需要添加到第一个文件中的一行
- #表示第一个文件中需要删除的一行
[root@centos7 testdir]# diff -c cuttest.txt cest.txt
*** cuttest.txt 2021-01-16 12:12:15.844549487 -0500
--- cest.txt 2021-01-16 12:15:08.775042428 -0500
***************
*** 1,6 ****
--- 1,7 ----
aaaaabbbbbbbb
cccccccccccccccccccc
ddddddddddd
+ 1111111111111111111111111111111111111111
fffffffffffffffffff
ggggggggggggggggg
wwwwwwwwwwwwwwww
#合并的方式来进行比较
[root@centos7 testdir]# diff -u cuttest.txt cest.txt
--- cuttest.txt 2021-01-16 12:12:15.844549487 -0500
+++ cest.txt 2021-01-16 12:15:08.775042428 -0500
@@ -1,6 +1,7 @@
aaaaabbbbbbbb
cccccccccccccccccccc
ddddddddddd
+1111111111111111111111111111111111111111
fffffffffffffffffff
ggggggggggggggggg
wwwwwwwwwwwwwwww
(37):paste
命令简介
paste 命令用于并排显示多个文件的相应行,将多个文件按列合并。
语法格式
paste [选项] [文件]
paste [OPTION] [FILE]
选项说明
-d #指定分隔符来取代默认分隔符(TAB分隔符)
-s #串列进行而非平行处理
--help #显示帮助信息
--version #显示版本信息
应用举例
并排显示两个文件的内容,
[root@centos7 testdir]# paste mingongge1.txt mingongge2.txt
1111 11 111111 1 111 1 1 11 head1
22222222222 222 2222 22 2 2 2 head2
33333333333 333333 3333 333 33 head3
444444444444 444 444444444 head4
1
2
3
4
paste 命令其他用法
[root@centos7 testdir]# paste mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
1
2
3
4
#连接文件中的所有行
[root@centos7 testdir]# paste -s mingongge1.txt
1111 11 111111 1 111 1 1 11 22222222222 222 2222 22 2 2 2 33333333333 333333 3333 333 33 444444444444 444 444444444 1 2 3 4
#使用指定分隔符连接所有行
[root@centos7 testdir]# paste -d, -s mingongge1.txt
1111 11 111111 1 111 1 1 11,22222222222 222 2222 22 2 2 2,33333333333 333333 3333 333 33,444444444444 444 444444444,1,2,3,4
[root@centos7 testdir]# paste -d[] -s mingongge1.txt
1111 11 111111 1 111 1 1 11[22222222222 222 2222 22 2 2 2]33333333333 333333 3333 333 33[444444444444 444 444444444]1[2]3[4
[root@centos7 testdir]# paste -d/ -s mingongge1.txt
1111 11 111111 1 111 1 1 11/22222222222 222 2222 22 2 2 2/33333333333 333333 3333 333 33/444444444444 444 444444444/1/2/3/4
#查看 paste 命令版本
[root@centos7 testdir]# paste --version
paste (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Written by David M. Ihnat and David MacKenzie.
paste 合并两个文件,或者添加行
[root@centos7 testdir]# cat mingongge1.txt
1111 11 111111 1 111 1 1 11
22222222222 222 2222 22 2 2 2
33333333333 333333 3333 333 33
444444444444 444 444444444
1
2
3
4
[root@centos7 testdir]# cat mingongge2.txt
head1
head2
head3
head4
[root@centos7 testdir]# paste -d '\n' mingongge1.txt mingongge2.txt
1111 11 111111 1 111 1 1 11
head1
22222222222 222 2222 22 2 2 2
head2
33333333333 333333 3333 333 33
head3
444444444444 444 444444444
head4
1
2
3
4
[root@centos7 testdir]# paste -d '\r' mingongge1.txt mingongge2.txt
head111 111111 1 111 1 1 11
head2222222 222 2222 22 2 2 2
head3333333 333333 3333 333 33
head44444444 444 444444444
1
2
3
4
(38):vi/vim
命令简介
vi/vim 命令是功能强大的纯文本编辑器。vim 是 vi 的加强版,比vi更容易使用。vim编辑器是Unix系统和Linux系统中最标准的编辑器,功能非常强大。它可以执行查找、删除、替换、输出多种文本操作方式。因此,学习vim编辑器也是学习Linux系统过程中比较重要的一个基础部分。
vim编辑器有三种模式,分别如下
#命令模式
此种模式下,可能通过移动光标,对字符或行进行删除操作。
#插入模式
在命令模式下,按键盘上字母“i”键即可进行插入模式,只有在此模式下才可以进行文字、字符的输入操作,按“ESC”键退出插入模式(返回命令模式)。
#底行模式
对文件保存或退出,以及设置编辑环境。
语法格式
vi/vim [选项] [文件]
vi/vim [OPTION] [FILE]
选项说明
+<行号> #从指定行号的行开始显示文本内容
-b #以二进制模式打开文件,用于编辑二进制文件和可执行文件
-c<指令> #多个文件时,先完成第一文件操作,然后再执行指定的指令动作
-d #以diff模式打开文件,当多个文件编辑时,显示文件差异部分
-l #使用lisp模式
-m #取消写文件功能
-M #关闭修改功能
-n #不使用缓存功能
-o<文件数目> #指定同时打开文件的数量
-R #以只读方式打开文件
-s #安静模式
vi/vim的基本操作
命令模式下光标的移动方法
Ctrl+f #屏幕向下移动一页,相当于Page Down
Ctrl+b #屏幕向上移动一页,相当于Page Up
0(数字) #移动到行首位置
$ #移动到行尾位置
gg #移动到第一行
G #移动到最后一行,与Shift+g功能相同
n<Enter> #光标向下移动n行(n为数字)
命令模式下搜索与查找
/word #向下查找匹配名为word的字符串
?word #向上查找匹配名为word的字符串
:n1,n2s/word1/word2/g #n1,n2为数字,在第n1与n2行之间查找匹配word1的字符串,并将word1全部替换成word2
:1,$s/word1/word2/g #在第一行与最后一行之间查找匹配word1的字符串,并将word1全部替换成word2
:1,$s/word1/word2/gc #在第一行与最后一行之间查找匹配word1的字符串,并将word1全部替换成word2,替换前进行提示确认是否需要替换
:%s/word1/word2/g #将匹配word1的内容全替换成word2
命令模式下删除、复制与粘贴方法
yy #复制光标当前所在的行
nyy #复制当前光标所在向下n行(n为数字)
dd #删除光标当前所在的行
ndd #删除当前光标所在向下n行(n为数字)
u #撤销上一次的操作
p #将复制的内容粘贴在光标所在的下一行
P #将复制的内容粘贴在光标所在的上一行
x #删除光标所在后一个字符
X #删除光标所有前一个字符
插入模式下保存与退出
:wq #保存并退出
:wq! #保存交强制退出
:q! #强制退出不保存
(39):tar
命令简介
tar 命令用于打包、压缩与解压压缩包文件。
tar 命令常常用于打包、压缩某些文件或目录,也可以添加新文件到归档文件中。Tar 代表的是磁带存档,是一种归档的文件格式,早期用于将文件归档到磁带备份存储。现可以用于收集、分发、归档文件,还可以保留文件原有的属性,如:用户和组权限,访问和修改日期以及目录结构。
语法格式
tar [OPTIONS] [FILE]
选项说明
-A #新增文件到已经存在的归档文件
-B #设置区块大小
-c #建立新的归档文件
-C #将压缩的文件解压到指定的目录下
-d #记录文件的差异
-x #解压或提取归档文件内容
-t #列出备份文件的内容
-z #通过gzip命令来压缩/解压缩文件,文件名一般为 xx.tar.gz
-Z #通过compress命令处理备份文件
-f #指定备份文件
-v #显示命令执行过程
-r #添加新文件到已经压缩的文件中
-u #添加改变了和现有的文件到已经存在的压缩文件
-j #通过bzip2命令来压缩/解压缩文件,文件名一般为xx.tar.bz2
-v #显示操作过程;
-k #保留原有文件不覆盖
-m #保留文件不被覆盖
-w #确认压缩文件的正确性
-p #保留原来的文件权限与属性
-P #使用文件名的绝对路径,不删除文件名称前的“/”号
-N #只将较指定日期更新的文件保存到备份文件中
--exclude=[范本样式] #排除符合范本样式的文件
--remove-files #归档/压缩之后删除源文件
应用举例
常见应用例子
tar -cf mingongge.tar *.jpg
#将所有.jpg的文件打包成一个名为mingongge.tar的文件
tar -rf mingongge.tar *.gif
#将所有.gif的文件增加到mingongge.tar的包里
tar -uf mingonggel.tar mingongge.gif
#更新mingongge.tar文件中的mingongge.gif文件
tar -tf mingongge.tar
#列出 all.tar 包中所有文件
tar -cfv mingongge.tar foo bar
#将文件foo和bar打包成mingongge.tar文件包,也可以理解成:从这两个文件中去创建这个mingongge.tar文件
tar -tvf mingongge.tar
#详细列出mingongge.tar中的所有文件
tar -xf mingongge.tar
#从mingongge.tar提取所有文件
将文件全部打包成tar包
tar -cvf mingongg.tar mingongg.log #仅打包,不压缩!
tar -zcvf mingongg.tar.gz mingongg.log #打包后,以gzip方式压缩
tar -jcvf mingongg.tar.bz2 mingongg.log #打包后,以bzip2方式压缩
解压目录
tar -xvf portal-web-v2.0.0.tar --strip-components 1 -C 指定目录
#排除目录--strip-components
将 tar包解压缩
tar -zxvf /opt/soft/test/log.tar.gz
打包或压缩文件时,排队指定的文件类型
tar -zcf mingongge.tar.gz /etc/ /var/ --exclude=*.txt
注意:如果在使用过程中遇到这类错误提示
tar: Removing leading `/’ from member names
原因是tar默认为相对路径,使用绝对路径的话就回报这个错,可以使用-P(大写)参数解决这个问题。
(40):gzip
命令简介
gzip 命令用来压缩文件,gzip,gunzip命令用于压缩或扩展 GNU GZIP 格式的文件。
如果压缩文件名对其文件系统而言太长,则gzip会将其截断。默认情况下,gzip 会将原始文件名和时间戳等信息保留在新产生的压缩文件中。
语法格式
gzip [ OPTIONS ] [ name ]
gunzip [ OPTIONS ] [ name ]
选项说明
-a #使用ASCII文字模式
-c #将输出写入到标准输出,保持原始文件不变。
-d #解压缩文件
-f #强制压缩文件
-h #显示在线帮助信息
-l #列出压缩文件的相关信息
-L #显示版本与版权信息
-n #压缩文件时,不保留原来的文件名称及时间戳等属性信息
-N #压缩文件时,保留原来的文件名称及时间戳属性信息
-q #不显示警告信息
-r #递归处理,将指定目录下的所有文件及子目录一并处理
-t #测试压缩文件是否正确无误
-v #显示命令执行过程信息
-V #显示版本信息
-<压缩效率> #压缩效率是一个介于1~9的数值,默认值为“6”,指定的值越高,压缩率就越高
应用举例
把当前目录所有的文件压缩成.gz包
[root@centos7 testdir]# gzip *
[root@centos7 testdir]# ll
total 24
-rw-r--r-- 1 root root 59 Jan 16 12:15 cest.txt.gz
-rw-r--r-- 1 root root 57 Jan 16 12:12 cuttest.txt.gz
-rw-r--r-- 1 root root 81 Jan 16 09:55 mingongge1.txt.gz
-rw-r--r-- 1 root root 51 Jan 16 03:36 mingongge2.txt.gz
-rw-r--r-- 1 root root 87 Jan 16 09:59 mingongge.txt.md5.gz
-rw-r--r-- 1 root root 65 Jan 16 10:19 sort.cut.txt.gz
把当前目录的每个压缩的文件解压,并列出详细的信息
[root@centos7 testdir]# gzip -dv *
cest.txt.gz: 77.8% -- replaced with cest.txt
cuttest.txt.gz: 73.5% -- replaced with cuttest.txt
mingongge1.txt.gz: 61.3% -- replaced with mingongge1.txt
mingongge2.txt.gz: 25.0% -- replaced with mingongge2.txt
mingongge.txt.md5.gz: -4.1% -- replaced with mingongge.txt.md5
sort.cut.txt.gz: 19.0% -- replaced with sort.cut.txt
[root@centos7 testdir]# ll
total 24
-rw-r--r-- 1 root root 144 Jan 16 12:15 cest.txt
-rw-r--r-- 1 root root 102 Jan 16 12:12 cuttest.txt
-rw-r--r-- 1 root root 124 Jan 16 09:55 mingongge1.txt
-rw-r--r-- 1 root root 24 Jan 16 03:36 mingongge2.txt
-rw-r--r-- 1 root root 49 Jan 16 09:59 mingongge.txt.md5
-rw-r--r-- 1 root root 42 Jan 16 10:19 sort.cut.txt
详细显当前目录每个压缩的文件的信息,但不将文件解压出来
[root@centos7 testdir]# gzip -l *
compressed uncompressed ratio uncompressed_name
59 144 77.8% cest.txt
57 102 73.5% cuttest.txt
81 124 61.3% mingongge1.txt
51 24 25.0% mingongge2.txt
87 49 -4.1% mingongge.txt.md5
65 42 19.0% sort.cut.txt
400 485 23.9% (totals)
和tar命令的常用用法基本一样,都属于文件压缩与解压命令。