基本命令
- 增:
- mkdir
- echo
- 删:
- rm
- rmdir
- 改:
- cd
- mv
- cp
- rm
- 查:
- cat
- less
- more
- head
- tail
- nl
- uname
- df
- free
- who
- uptime
| 命令 | 解析 |
| Tab键 | 自动补齐文件名,命令等;按两次tab键可以输出可用的所有名称列表 |
| \反斜杠 | 强制换行 |
| ctrl+u | 快速删除光标之前的所有字符(可当作剪切) |
| ctrl+k | 快速删除光标之后的所有字符(可当作剪切) |
| ctrl+y | 粘贴刚才所删除(剪切)的字符 |
| ctrl+l | 清屏 |
| ctrl+c | 结束当前执行的命令 |
| ctrl+d | 断开shell远程连接,类似于输入exit |
| ctrl+a | 把光标移动到首行,类似于home键 |
| ctrl+e | 把光标移动到行尾,类似于end键 |
| ctrl+z | 转入后台运行 |
| ctrl+r | 在历史命令中查找(常用且好用) |
背景为黄色的是常用快捷键
mv:移动文件或目录及改名操作
#命令格式:mv [选项] 文件
#把 a.txt 移动到 /usr/local 下
mv a.txt /usr/local
#把 a.txt 移动到 /usr/local 下,并改名为:b.txt
mv a.txt /usr/local/b.txt
cp:复制文件或者目录
#命令格式:cat [选项] 文件
-r #递归复制文件和目录
-p #保留源文件或目录的所有属性
-i #若目标文件已存在,则会询问是否覆盖
#覆盖文件或者目录
cp -rp a /usr/local
rm:删除文件或目录(它直接删除,不进回收站,删除后难以恢复,使用时需谨慎)
rm 文件名 # 删除单个文件
rm 文件1 文件2 # 删除多个文件
rm -r 目录名 # 递归删除目录及其内容
rm -f 文件 # 强制删除,不询问、忽略不存在的文件
rm -i 文件 # 删除前逐一确认(交互式)
rm -rf 目录 # 递归+强制删除(最常用,也最危险)
文件查看(cat、less、more、head、tail、nl)
cat:查看文件内容
#命令格式:cat [选项] 文件
[root@localhost ~]# cat a.txt #直接查看文件内容
[root@localhost ~]# cat -n a.txt #查看文件内容及行号
more:全屏分页显示文件内容
#more文件名(一般不用选项)
[root@localhost ~]# more a.txt
#按Enter向下滚动一行
#按空格键向下滚动一页
#按b向上翻滚动一页
按q退出
less:与more基本相同,但扩展功能更多
#命令格式:less 一般不使用选项
[root@localhost ~]# less a.txt
b:向上翻整页
空格键:向下翻整页
u:向上翻半页
d:向下翻半页
G:跳转到最后一行
1G:跳转到第一行
PgUp:向上翻整页
PgDn:向下翻整页
按“/”查找内容, “n”移动到匹配的下一个, “N”移动到匹配到的上一个
head:查看开头那几行内容,默认10行
#命令格式:head 选项 文件
#查看开头5行文件
[root@localhost etc]# head -n 5 /etc/a.txt
[root@localhost etc]# head -n 5 a.txt #这个因为已经提前进入到/etc目录了,所以可以直接选择a.txt
tail:查看结尾几行内容,默认10行,使用 -f动态查看
#命令格式:tail 选项 文件
[root@localhost etc]# tail -n 5 /etc/a.txt #查看结尾5行
[root@localhost etc]# tail -f a.txt #滚动查看文件内容及行号
nl:查看文件时自动添加编号,与cat -n等效
#命令格式:less一般不使用选项
[root@localhost etc]# nl /etc/a.txt
系统信息查询(uname、df、free、who、uptime)
uname:显示操作系统的硬件的相关信息,比如内核版本、系统架构、主机名等
#命令格式:uname [选项]
[root@localhost etc]# uname -a #显示所有信息(最常用)
[root@localhost etc]# uname -r #显示内核版本
[root@localhost etc]# uname -s #显示内核名称
[root@localhost etc]# uname -n #显示网络主机名
[root@localhost etc]# uname -m #显示硬件架构(机器类型)
[root@localhost etc]# uname -p #显示处理器类型
[root@localhost etc]# uname -o #显示操作系统名称
df:显示文件系统的磁盘空间使用情况(Disk Free),即各挂载点的总容量、已用、可用空间
#命令格式:df [选项] [文件或目录]
#以人类可读格式显示(GB、MB),最常用
[root@localhost etc]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 894M 0 894M 0% /dev
tmpfs 910M 0 910M 0% /dev/shm
tmpfs 910M 12M 899M 2% /run
tmpfs 910M 0 910M 0% /sys/fs/cgroup
/dev/mapper/centos-root 17G 6.2G 11G 36% /
/dev/sda1 1014M 186M 829M 19% /boot
tmpfs 182M 64K 182M 1% /run/user/1000
tmpfs 182M 0 182M 0% /run/user/0
[root@localhost etc]# df -h a.txt
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 6.2G 11G 36% /
# 显示文件系统类型(如 ext4、xfs)
[root@localhost etc]# df -T
文件系统 类型 1K-块 已用 可用 已用% 挂载点
devtmpfs devtmpfs 914488 0 914488 0% /dev
tmpfs tmpfs 931512 0 931512 0% /dev/shm
tmpfs tmpfs 931512 11392 920120 2% /run
tmpfs tmpfs 931512 0 931512 0% /sys/fs/cgroup
/dev/mapper/centos-root xfs 17811456 6396652 11414804 36% /
/dev/sda1 xfs 1038336 190380 847956 19% /boot
tmpfs tmpfs 186304 64 186240 1% /run/user/1000
tmpfs tmpfs 186304 0 186304 0% /run/user/0
[root@localhost etc]# df -T a.txt
文件系统 类型 1K-块 已用 可用 已用% 挂载点
/dev/mapper/centos-root xfs 17811456 6396652 11414804 36% /
#组合使用
[root@localhost etc]# df -hT a.txt
free:显示系统的物理内存和交换分区(swap)的使用情况
#命令格式:free [选项]
[root@localhost etc]# free -h #以人类可读格式显示(最常用)
[root@localhost etc]# free -m #以 MB 为单位显示
[root@localhost etc]# free -g #以 GB 为单位显示
[root@localhost etc]# free -m -s 1 #持续观察,每 N 秒刷新一次
关键列说明:
- total:总内存
- used:已用内存(含缓存)
- free:完全空闲的内存
- available:真正可用的内存(含可回收的缓存),这个数值最值得关注
who:显示当前登录到系统的用户信息,包括用户名、登录终端、登录时间和来源IP
#命令格式:who [选项] [文件]
[root@localhost etc]# who #查看当前登录用户及来源ip
[root@localhost etc]# who -r #查看当前运行级别
[root@localhost etc]# who -b #查看系统启动时间
uptime:显示系统已经运行了多长时间,同时显示系统负载(load average)
#命令格式:less一般不使用选项
[root@localhost etc]# uptime -s #查看系统启动时间
[root@localhost etc]# uptime #查看启动时长及负载情况