目录
一、知识补充
1.1 env
查看当前终端里的shell的环境变量(即全局变量)
查出来的内容中HISTSIZT=1000,即规定了历史命令的条数为1000
1.2 vim编辑器的使用
Linux下的文本编辑器,vi的升级版本
【安装vim编辑器】yum install vim -y(要求虚拟机必须一定要可以上网)
1.2.1 touch和vim创建文件的区别
touch a.txt 只是创建一个空文件,不往文件里写入内容
vim a.txt 不仅创建文件,同时可以往其中写入内容。如果a.txt文件已经存在,就是使用vim打开这个文件,如果不存在就是新建
1.2.2 操作步骤
- 按i进入输入(插入)模式
- 按Esc键返回到命令模式
- 输入:wq(必须是英文输入模式,小写)==>退出且保存
- 可通过cat接文件名查看文件内容
[root@192 lianxi]# vim lihua.txt
[root@192 lianxi]# cat lihua.txt
lihua
Chian
hunan
李华
中国
二、命令讲解
2.1 mv
1. 移动(Move)文件或目录==>剪切
[root@192 lianxi]# ls
hua.txt hunan lihua
[root@192 lianxi]# mv lihua hunan
[root@192 lianxi]# ls
hua.txt hunan
[root@192 lianxi]# ls hunan
lihua
可以同时移动多个文件夹,也可以同时移动文件和文件夹。此时,目的地必须是文件夹,否则无法容纳多个内容
[root@192 lianxi]# mkdir liming
[root@192 lianxi]# touch lihua.txt
[root@192 lianxi]# ls
hua.txt hunan lihua.txt liming
[root@192 lianxi]# mv lihua.txt liming/ hunan
[root@192 lianxi]# ls
hua.txt hunan
[root@192 lianxi]# ls hunan
lihua lihua.txt liming
2. 若目标位置与原位置相同或目标文件夹不存在,则相当为改名==>重命名
[root@192 lianxi]# ls
hua.txt hunan
[root@192 lianxi]# mv hunan xiang
[root@192 lianxi]# ls
hua.txt xiang
2.2 cat
显示文件的全部内容
【选项】-n 给输出的每一行进行编号
[root@192 lianxi]# file lihua.txt
lihua.txt: UTF-8 Unicode text
[root@192 lianxi]# cat -n lihua.txt
1 lihua
2 Chian
3 hunan
4 李华
5 中国
2.2.1 cat详细作用
cat - concatenate files and print on the standard output
连接文件和输出内容到屏幕
连接文件:同时可以将多个文件的内容同时输出,然后连接起来
[root@192 lianxi]# cat lihua.txt
lihua
Chian
hunan
李华
中国
[root@192 lianxi]# cat xiaoming.txt
Hello
xiaoming
[root@192 lianxi]# cat lihua.txt xiaoming.txt
lihua
Chian
hunan
李华
中国
Hello
xiaoming
2.2.2 重定向
把本来应该在屏幕上输出的内容,重定向到文件里,改变了他的输出方向
standard input 标准的输入==》键盘
standard output 标准的输出==》屏幕
重定向其实是改变了输出的方向,标准的输出是往屏幕输出,重定向可以输出到文件
[root@192 lianxi]# cat lihua.txt xiaoming.txt >lihua_xiaoming.txt
[root@192 lianxi]# ls
hua.txt lihua.txt lihua_xiaoming.txt passwd xiang xiaoming.txt
[root@192 lianxi]# cat lihua_xiaoming.txt
lihua
Chian
hunan
李华
中国
Hello
xiaoming
> 输出重定向:如果后面接的文件不存在就创建,如果存在就覆盖原来内容
[root@192 lianxi]# echo hello,world >hello.txt
[root@192 lianxi]# ls
hello.txt hua.txt lihua.txt lihua_xiaoming.txt passwd xiang xiaoming.txt
[root@192 lianxi]# cat hello.txt
hello,world
[root@192 lianxi]# echo HELLO,WORLD >hello.txt
[root@192 lianxi]# cat hello.txt
HELLO,WORLD
>> 追加输出重定向:如果后面接的文件不存在就新建,如果存在就不覆盖原来的内容,只是在末尾追加
[root@192 lianxi]# echo 123 >test.txt
[root@192 lianxi]# cat test.txt
123
[root@192 lianxi]# echo 456 >>test.txt
[root@192 lianxi]# cat test.txt
123
456
2.2.3 tac
颠倒读取文件内容
[root@192 lianxi]# cat test.txt
123
456
[root@192 lianxi]# tac test.txt
456
123
2.3 more
全屏方式显示文件内容
分页显示:一页一页的显示
2.3.1 交互操作方法
- 按Enter键向下逐行滚动
- 按空格键向下翻一屏,按b键向上翻一屏
- 按q键退出
[root@192 lianxi]# cp /var/log/messages .
[root@192 lianxi]# more messages
2.3.2 |管道符号
作用就是将前面一个命令的输出送给后面的命令作为输入
管道本质上是实现了进程与进程之间通信,是进程与进程之间通信的一种方式
[root@192 lianxi]# cat -n messages |more 即显示行号,有分页显示
2.3.3 ps aux
查看Linux系统里当前这个瞬间的进程的信息
- USER 指哪个用户启动的进程
- PID 为进程的编号 process id
- %CPU 为进程所消耗的cpu使用率
- %MEM 为进程所消耗的内存 memory
- COMMAND 为运行进程的名字
[root@192 lianxi]# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.6 128148 6756 ? Ss 2月14 0:05 /usr/lib/syst
root 2 0.0 0.0 0 0 ? S 2月14 0:00 [kthreadd]
root 4 0.0 0.0 0 0 ? S< 2月14 0:00 [kworker/0:0H
2.3.4 wc
统计一个文件里有多少行、单词、字节
- 第一个2 表示2行
- 第二个2 表示2个单词
- 8 表示八个字节
一个汉字占3个字节,一个英文字母占1个字节,一个空格、一个回车键都占一个字节
[root@192 lianxi]# cat test.txt
123
456
[root@192 lianxi]# wc test.txt
2 2 8 test.txt
[root@192 lianxi]# cat test.txt |wc -l 统计test.txt文件里有多少行
2
ps aux|wc -l 统计ps aux命令的输出有多少行
ps aux|more分页显示进程信息
【选项】
-l 统计行数
-w 统计单词数
-c 统计字节
2.3.5 $()
作用:优先执行里面的命令,然后将命令的执行结果返回
user_num=$(cat /etc/passwd |wc -l)先执行cat /etc/passwd |wc -l 得到结果,然后赋值给user_num这个变量
[root@192 lianxi]# cat /etc/passwd |wc -l
30
[root@192 lianxi]# user_num=$(cat /etc/passwd |wc -l)
[root@192 lianxi]# echo $user_num
30
2.3.6 less
与more命令相同,但扩展功能更多,操作方法基本类似,有个别出入
【page down】上翻页【page up】下翻页
【差异】more和less
- 内容显示完后,less不退出,more会自动退出
- less可以按【page down】【page up】,而more不行
2.4 head
用途:查看文件开头的一部分(默认是头10行)
格式:head -n numbuer 文件名
格式:head -数字 文件名
[root@192 lianxi]# head -3 messages 等于 [root@192 lianxi]# head -n 3 messages
[root@192 lianxi]# cat -n messages |head -3
1 Feb 13 17:27:01 192 rsyslogd: [origin software="rsyslogd" swVersion="8.24.0-55.el7" x-pid="1069" x-info="http://www.rsyslog.com"] rsyslogd was HUPed
2 Feb 13 17:37:33 192 dhclient[864]: DHCPREQUEST on ens33 to 192.168.255.254 port 67 (xid=0x7c8e74b0)
3 Feb 13 17:37:33 192 dhclient[864]: DHCPACK from 192.168.255.254 (xid=0x7c8e74b0)
2.5 tail
用途:查看文件结尾的一部分(默认是后10行)
格式:tail -n numbuer 文件名
格式:tail -数字 文件名
2.5.1 tail的高级用法
tail -n +number 用在不想显示前number行的内容,显示从第number行开始到末尾的内容
[root@192 lianxi]# df -Th
文件系统 类型 容量 已用 可用 已用% 挂载点
devtmpfs devtmpfs 475M 0 475M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17G 1.6G 16G 10% /
/dev/sda1 xfs 1014M 138M 877M 14% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
[root@192 lianxi]# df -Th|tail -n +2 从第二行开始到末尾
devtmpfs devtmpfs 475M 0 475M 0% /dev
tmpfs tmpfs 487M 0 487M 0% /dev/shm
tmpfs tmpfs 487M 7.7M 479M 2% /run
tmpfs tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17G 1.6G 16G 10% /
/dev/sda1 xfs 1014M 138M 877M 14% /boot
tmpfs tmpfs 98M 0 98M 0% /run/user/0
tail -f test.txt 动态监控test.txt文件里的内容变化:盯着文件末尾的内容,一旦有输入,马上输出给用户
一般用在我们看某个日志文件的变化
按Ctrl + C强行终止
[root@192 lianxi]# tail -f test.txt
123
456
HELLO
TEST
(复制标签)在另外一个终端里使用重定向功能追加
[root@192 ~]# cd lianxi
[root@192 lianxi]# echo HELLO >>test.txt
[root@192 lianxi]# echo TEST >>test.txt