指定运行级别

基本介绍

运行级别说明:
0:关机
1:单用户【找回丢失密码】
2:多用户状态没有网络服务
3:多用户状态有网络服务
4:系统未使用保留给用户
5:图形界面
6:系统重启
常用运行级别是 3 和 5 ,也可以指定默认运行级别, 后面演示

应用实例

命令init [0123456]

通过 init 来切换不同的运行级别,比如动 5-3 , 然后关机

  1. [root@localhost home]# init 5
  2. [root@localhost home]# init 3
  3. [root@localhost home]# init 0

CentOS7 后运行级别说明

运行级别说明文件在 /etc/inittab 中 ,查看如下

  1. [root@localhost etc]# cat inittab
  2. # inittab is no longer used when using systemd.
  3. #
  4. # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
  5. #
  6. # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
  7. #
  8. # systemd uses 'targets' instead of runlevels. By default, there are two main targets:
  9. #
  10. # multi-user.target: analogous to runlevel 3 # multi-user.target 等价于 运行级别3
  11. # graphical.target: analogous to runlevel 5 # graphical.target 等价于 运行级别5
  12. #
  13. # To view current default target, run: # 查看当前默认运行级别
  14. # systemctl get-default
  15. #
  16. # To set a default target, run: # 设置当前默认运行级别
  17. # systemctl set-default TARGET.target
  18. #
  19. [root@localhost etc]#

查看当前默认运行级别并进行变更

  1. [root@localhost etc]# systemctl get-default
  2. multi-user.target
  3. [root@localhost etc]# systemctl set-default graphical.target
  4. Removed symlink /etc/systemd/system/default.target.
  5. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target.
  6. [root@localhost etc]# systemctl get-default
  7. graphical.target

找回root密码

  1. 首先,启动系统,进入开机界面,在界面中按“e”进入编辑界面。如图

image.png

  1. 进入编辑界面,使用键盘上的上下键把光标往下移动,找到以““Linux16”开头内容所在的行数”,在行的最后面输入:init=/bin/sh。如图

image.png

  1. 接着,输入完成后,直接按快捷键:Ctrl+x 进入单用户模式
  2. 接着,在光标闪烁的位置中输入:mount -o remount,rw /(注意:各个单词间有空格),完成后按键盘的回车键(Enter)。如图

image.png

  1. 在新的一行最后面输入:passwd, 完成后按键盘的回车键(Enter)。输入密码,然后再次确认密码即可(韩顺平提示: 密码长度最好8位以上,但不是必须的), 密码修改成功后,会显示passwd…..的样式,说明密码修改成功

image.png

  1. 接着,在鼠标闪烁的位置中(最后一行中)输入:touch /.autorelabel(注意:touch与 /后面有一个空格),完成后按键盘的回车键(Enter)

image.png

  1. 继续在光标闪烁的位置中,输入:exec /sbin/init(注意:exec与 /后面有一个空格),完成后按键盘的回车键(Enter),等待系统自动修改密码(韩顺平提示:这个过程时间可能有点长,耐心等待),完成后,系统会自动重启, 新的密码生效了

image.png

帮助指令

man 获得帮助信息

基本语法

  1. man [命令或配置文件] (功能描述:获得帮助信息)

查看 ls 命令的帮助信息

  1. [root@localhost ~]# man ls

细节说明

  • 隐藏文件是以 .开头 , 选项可以组合使用 比如 ls -al 比如 ls -al /root

    help 指令

    基本语法

    1. help 命令 # 功能描述:获得 shell 内置命令的帮助信息

    查看 cd 命令的帮助信息

  1. [root@localhost ~]# help cd
  2. cd: cd [-L|[-P [-e]]] [dir]
  3. Change the shell working directory.
  4. Change the current directory to DIR. The default DIR is the value of the
  5. HOME shell variable.
  6. The variable CDPATH defines the search path for the directory containing
  7. DIR. Alternative directory names in CDPATH are separated by a colon (:).
  8. A null directory name is the same as the current directory. If DIR begins
  9. with a slash (/), then CDPATH is not used.
  10. If the directory is not found, and the shell option `cdable_vars' is set,
  11. the word is assumed to be a variable name. If that variable has a value,
  12. its value is used for DIR.
  13. Options:
  14. -L force symbolic links to be followed
  15. -P use the physical directory structure without following symbolic
  16. links
  17. -e if the -P option is supplied, and the current working directory
  18. cannot be determined successfully, exit with a non-zero status
  19. The default is to follow symbolic links, as if `-L' were specified.
  20. Exit Status:
  21. Returns 0 if the directory is changed, and if $PWD is set successfully when
  22. -P is used; non-zero otherwise.

文件目录类

pwd 指令

基本语法

  1. pwd # 功能描述:显示当前工作目录的绝对路径

显示当前工作目录的绝对路径

  1. [root@localhost ~]# pwd
  2. /root

ls 指令

基本语法

  1. ls [选项] [目录或是文件]
  2. 常用选项
  3. -a :显示当前目录所有的文件和目录,包括隐藏的。
  4. -l :以列表的方式显示

查看当前目录的所有内容信息

  1. [root@localhost ~]# ls -al
  2. 总用量 181324
  3. dr-xr-x---. 17 root root 4096 7 3 15:36 .
  4. dr-xr-xr-x. 21 root root 4096 7 3 11:21 ..
  5. -rw-------. 1 root root 1242 10 23 2020 anaconda-ks.cfg
  6. -rw-------. 1 root root 18686 7 3 11:25 .bash_history
  7. -rw-r--r--. 1 root root 18 12 29 2013 .bash_logout
  8. -rw-r--r--. 1 root root 176 12 29 2013 .bash_profile
  9. -rw-r--r--. 1 root root 176 12 29 2013 .bashrc
  10. drwx------. 14 root root 4096 5 19 10:39 .cache
  11. drwxr-xr-x. 16 root root 4096 5 19 10:40 .config
  12. -rw-r--r--. 1 root root 100 12 29 2013 .cshrc
  13. drwx------. 3 root root 25 5 19 10:22 .dbus
  14. -rw-------. 1 root root 16 5 19 10:22 .esd_auth
  15. -rw-------. 1 root root 620 5 19 11:18 .ICEauthority
  16. -rw-------. 1 root root 35 7 3 15:36 .lesshst
  17. drwx------. 3 root root 19 5 19 10:22 .local
  18. drwxr-xr-x. 5 root root 66 5 19 10:32 .mozilla
  19. drwxr-xr-x. 2 root root 40 10 28 2020 .oracle_jre_usage
  20. drwxr-----. 3 root root 19 5 19 10:34 .pki
  21. -rwxr-xr-x. 1 root root 7042 6 17 17:44 showdoc
  22. -rw-r--r--. 1 root root 129 12 29 2013 .tcshrc
  23. -rw-------. 1 root root 12027 6 18 12:08 .viminfo
  24. drwxr-xr-x. 2 root root 6 5 19 10:22 公共
  25. drwxr-xr-x. 2 root root 6 5 19 10:22 模板
  26. drwxr-xr-x. 2 root root 6 5 19 10:22 视频
  27. drwxr-xr-x. 2 root root 6 5 19 10:22 图片
  28. drwxr-xr-x. 2 root root 6 5 19 10:22 文档
  29. drwxr-xr-x. 2 root root 6 5 19 15:03 下载
  30. drwxr-xr-x. 2 root root 6 5 19 10:22 音乐

cd 指令

基本语法

  1. cd [参数] # 功能描述:切换到指定目录
  2. 理解绝对路径和相对路径:
  3. cd ~ 或者 cd :回到自己的家目录
  4. 当你是root 用户时
  5. [root@localhost ~]# cd ~
  6. [root@localhost ~]# pwd
  7. /root # 回到了root目录
  8. 当你是tom 用户时
  9. [tom@localhost ~]$ cd
  10. [tom@localhost ~]$ pwd
  11. /home/tom # 回到了tom用户的家目录
  12. cd .. 回到当前目录的上一级目录 也是一种相对路径的用法

使用绝对路径切换到 root 目录

  1. [root@localhost tom]# cd /root
  2. [root@localhost ~]#

使用相对路径到/root 目录, 比如在 /home/tom

  1. [root@localhost tom]# cd
  2. [root@localhost ~]#

表示回到当前目录的上一级目录

  1. [root@localhost ~]# cd ..
  2. [root@localhost /]#

回到家目录

  1. [root@localhost /]# cd ~
  2. [root@localhost ~]#

mkdir 指令

基本语法

  1. mkdir [选项] 要创建的目录
  2. 常用选项-p :创建多级目录

创建一个目录 /home/dog

  1. [root@localhost home]# ls
  2. admin tom
  3. [root@localhost home]# mkdir /home/dog
  4. [root@localhost home]# ls
  5. admin dog tom

创建多级目录 /home/animal/tiger

  1. [root@localhost home]# mkdir -p /home/animal/tiger
  2. [root@localhost home]# ls
  3. admin animal dog tom
  4. [root@localhost home]# cd animal/
  5. [root@localhost animal]# ls
  6. tiger

rmdir 指令

基本语法

  1. rmdir [选项] 要删除的空目录
  2. 使用细节
  3. rmdir 删除的是空目录,如果目录下有内容时无法删除的

删除一个空目录 /home/dog

  1. [root@localhost home]# ls
  2. admin animal dog tom
  3. [root@localhost home]# rmdir /home/dog/
  4. [root@localhost home]# ls
  5. admin animal tom

删除一个非空目录 /home/animal

  1. [root@localhost home]# rmdir /home/animal/
  2. rmdir: 删除 "/home/animal/" 失败: 目录非空
  3. [root@localhost home]# rm -rf /home/animal/
  4. [root@localhost home]# ls
  5. admin tom

touch 指令

基本语法

  1. touch 文件名称 # 创建空文件

在/home 目录下 , 创建一个空文件 hello.txt

  1. [root@localhost home]# touch hello.txt
  2. [root@localhost home]# ls
  3. admin hello.txt tom

cp 指令

基本语法

  1. cp [选项] source dest # 拷贝文件到指定目录
  2. 常用选项
  3. -r :递归复制整个文件夹

将 /home/hello.txt 文件拷贝到 /home/tom/ 目录下

  1. [root@localhost tom]# cp /home/hello.txt /home/tom/
  2. [root@localhost tom]# ll
  3. 总用量 0
  4. -rw-r--r--. 1 root root 0 7 3 17:22 hello.txt

递归复制整个文件夹,举例, 比如将 /home/tom整个目录, 拷贝到 /opt

  1. [root@localhost tom]# cd /opt/
  2. [root@localhost opt]# ls
  3. [root@localhost opt]# cp -r /home/tom/ /opt/
  4. [root@localhost opt]# ls
  5. tom
  6. # 使用细节强制覆盖不提示的方法:\cp
  7. [root@localhost opt]# cp -r /home/tom/ /opt/
  8. cp:是否覆盖"/opt/tom/.bash_logout"
  9. cp:是否覆盖"/opt/tom/.bash_profile"
  10. cp:是否覆盖"/opt/tom/.bashrc"
  11. cp:是否覆盖"/opt/tom/.cache/abrt/lastnotification"
  12. cp:是否覆盖"/opt/tom/.bash_history"
  13. cp:是否覆盖"/opt/tom/hello.txt"
  14. [root@localhost tom]# \cp -r /home/tom/ /opt/

rm 指令

基本语法

  1. rm [选项] 要删除的文件或目录
  2. 常用选项
  3. -r :递归删除整个文件夹
  4. -f :强制删除不提示

将 /home/hello.txt 删除

  1. [root@localhost home]# ls
  2. hello.txt jack tom zwj
  3. [root@localhost home]# rm hello.txt
  4. rm:是否删除普通空文件 "hello.txt"y
  5. [root@localhost home]# ls
  6. jack tom zwj

递归删除整个文件夹 /home/bbb

  1. [root@localhost home]# ls
  2. ddd jack tom zwj
  3. [root@localhost home]# rm /home/ddd/
  4. rm: 无法删除"/home/ddd/": 是一个目录
  5. [root@localhost home]# rm -rf /home/ddd/
  6. [root@localhost home]# ls
  7. jack tom zwj
  8. # 使用细节强制删除不提示的方法:带上 -f 参数即可

mv 指令

基本语法

  1. mv oldNameFile newNameFile # 功能描述:重命名
  2. mv /temp/movefile /targetFolder #功能描述:移动文件

将 /home/cat.txt 文件 重新命名为 pig.txt

  1. [root@localhost home]# ls
  2. cat.txt jack tom zwj
  3. [root@localhost home]# mv cat.txt pig.txt
  4. [root@localhost home]# ls
  5. jack pig.txt tom zwj

将 /home/pig.txt 文件 移动到 /root 目录下

  1. [root@localhost home]# ls
  2. jack pig.txt tom zwj
  3. [root@localhost home]# mv pig.txt /root/
  4. [root@localhost home]# cd
  5. [root@localhost ~]# ls
  6. anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
  7. Hello.java pig.txt 模板 图片 下载 桌面

移动整个目录 , 比如将 /opt/bbb 移动到 /home

  1. [root@localhost opt]# mv /opt/ddd/ /home/
  2. [root@localhost opt]# cd /home/
  3. [root@localhost home]# ls
  4. ddd jack tom zwj

cat 指令

基本语法

  1. cat [选项] 要查看的文件
  2. 常用选项
  3. -n :显示行号
  4. 细节:cat 只能浏览文件,而不能修改文件,为了浏览方便,一般会带上 管道命令 |

查看 /etc/profile 文件内容,并显示行号

  1. [root@localhost home]# cat -n /etc/profile
  2. 1 # /etc/profile
  3. 2
  4. 3 # System wide environment and startup programs, for login setup
  5. ...
  6. 76 unset -f pathmunge

more 指令

more 指令more 指令是一个基于 VI 编辑器的文本过滤器,它以全屏幕的方式按页显示文本文件的内容。more 指令中内置了若干快捷键(交互的指令),详见操作说明

操作 功能说明
空格键(Space) 向下翻一页
Enter 向下翻一行
q 立即离开more , 不再显示该文件内容
Ctrl + F 向下滚动一屏
Ctrl + B 向上滚动一屏
= 输出当前行的行号
:f 输出文件名和当前行的行号

基本语法

  1. more 要查看的文件

采用 more 查看文件 /etc/profileless

  1. [root@localhost home]# more /etc/profile
  2. # /etc/profile
  3. ...
  4. if [ -x /usr/bin/id ]; then
  5. --More--(35%)

less 指令

less 指令用来分屏查看文件内容,它的功能与 more 指令类似,但是比 more 指令更加强大,支持各种显示终端。less 指令在显示文件内容时,并不是一次将整个文件加载之后才显示,而是根据显示需要加载内容,对于显示大型文件具有较高的效率。

操作 功能说明
空格键(Space) 向下翻一页
[pagedown] 向下翻一页
[pageup] 向上翻一页
/字串 向下搜寻【字串】的功能;n:向下查找 N:向上查找
?字串 向上搜寻【字串】的功能;n:向下查找 N:向上查找
q 离开less这个程序

基本语法

  1. less 要查看的文件

采用 less 查看一个大文件文件 /opt/杂文.txt

  1. [root@localhost home]# less /opt/杂文.txt

echo 指令

输出内容到控制台

  1. echo [选项] [输出内容]

使用 echo 指令输出环境变量, 比如输出 $PATH $HOSTNAME

  1. [root@localhost home]# echo $PATH
  2. /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

使用 echo 指令输出 hello,world!

  1. [root@localhost home]# echo hello,world!
  2. hello,world!

head 指令

head 用于显示文件的开头部分内容,默认情况下 head 指令显示文件的前 10 行内容

  1. head 文件 # 功能描述:查看文件头 10 行内容
  2. head -n 5 文件 # 功能描述:查看文件头 5 行内容,5 可以是任意行数

查看/etc/profile 的前面 5 行代码

  1. [root@localhost home]# head -n 5 /etc/profile
  2. # /etc/profile
  3. # System wide environment and startup programs, for login setup
  4. # Functions and aliases go in /etc/bashrc
  5. [root@localhost home]#

tail 指令

tail 用于输出文件中尾部的内容,默认情况下 tail 指令显示文件的前 10 行内容。

  1. tail 文件 # 功能描述:查看文件尾 10 行内容
  2. tail -n 5 文件 # 功能描述:查看文件尾 5 行内容,5 可以是任意行数
  3. tail -f 文件 # 功能描述:实时追踪该文档的所有更新

查看/etc/profile 最后 5 行的代码

  1. [root@localhost home]# tail -n 5 /etc/profile
  2. fi
  3. done
  4. unset i
  5. unset -f pathmunge
  6. [root@localhost home]#

实时监控 mydate.txt , 看看到文件有变化时,是否看到, 实时的追加 hello,world

image.png

> 指令 和 >> 指令

输出重定向和 >> 追加

  1. ls -l >文件 # 功能描述:列表的内容写入文件 a.txt 中(覆盖写)
  2. ls -al >>文件 # 功能描述:列表的内容追加到文件 aa.txt 的末尾
  3. cat 文件 1 > 文件 2 # 功能描述:将文件 1 的内容覆盖到文件 2
  4. echo "内容">> 文件 # 追加

将 /home 目录下的文件列表 写入到 /home/info.txt 中, 覆盖写入 [如果 info.txt 没有,则会创建]

  1. [root@localhost home]# ls -l /home/
  2. 总用量 20
  3. drwxr-xr-x. 2 root root 4096 7 3 22:37 ddd
  4. drwx------. 5 jack jack 4096 7 2 22:58 jack
  5. -rw-r--r--. 1 root root 12 7 3 23:37 mydate.txt
  6. drwx------. 5 tom tom 4096 7 2 22:44 tom
  7. drwx------. 3 zwj mojiao 4096 7 2 23:22 zwj
  8. [root@localhost home]# ls -l /home/ > /home/info.txt
  9. [root@localhost home]# ls
  10. ddd info.txt jack mydate.txt tom zwj
  11. [root@localhost home]# cat info.txt
  12. 总用量 20
  13. drwxr-xr-x. 2 root root 4096 7 3 22:37 ddd
  14. -rw-r--r--. 1 root root 0 7 3 23:44 info.txt
  15. drwx------. 5 jack jack 4096 7 2 22:58 jack
  16. -rw-r--r--. 1 root root 12 7 3 23:37 mydate.txt
  17. drwx------. 5 tom tom 4096 7 2 22:44 tom
  18. drwx------. 3 zwj mojiao 4096 7 2 23:22 zwj

将当前日历信息 追加到 /home/mycal 文件中

  1. [root@localhost home]# cal
  2. 七月 2021
  3. 1 2 3
  4. 4 5 6 7 8 9 10
  5. 11 12 13 14 15 16 17
  6. 18 19 20 21 22 23 24
  7. 25 26 27 28 29 30 31
  8. [root@localhost home]# cal >> /home/mycal
  9. [root@localhost home]# ls
  10. ddd info.txt jack mycal mydate.txt tom zwj
  11. [root@localhost home]# cat mycal
  12. 七月 2021
  13. 1 2 3
  14. 4 5 6 7 8 9 10
  15. 11 12 13 14 15 16 17
  16. 18 19 20 21 22 23 24
  17. 25 26 27 28 29 30 31

ln 指令

ln 指令软链接也称为符号链接,类似于 windows 里的快捷方式,主要存放了链接其他文件的路径

  1. ln -s [原文件或目录] [软链接名] # 功能描述:给原文件创建一个软链接
  2. 细节说明当我们使用 pwd 指令查看目录时,仍然看到的是软链接所在目录。

在/home 目录下创建一个软连接 myroot,连接到 /root 目录

  1. [root@localhost home]# ln -s /root/ /home/myroot
  2. [root@localhost home]# ll
  3. 总用量 28
  4. drwxr-xr-x. 2 root root 4096 7 3 22:37 ddd
  5. -rw-r--r--. 1 root root 326 7 3 23:44 info.txt
  6. drwx------. 5 jack jack 4096 7 2 22:58 jack
  7. -rw-r--r--. 1 root root 157 7 3 23:46 mycal
  8. -rw-r--r--. 1 root root 12 7 3 23:37 mydate.txt
  9. lrwxrwxrwx. 1 root root 6 7 3 23:51 myroot -> /root/
  10. drwx------. 5 tom tom 4096 7 2 22:44 tom
  11. drwx------. 3 zwj mojiao 4096 7 2 23:22 zwj
  12. [root@localhost home]# cd myroot/
  13. [root@localhost myroot]# ls
  14. anaconda-ks.cfg initial-setup-ks.cfg 公共 视频 文档 音乐
  15. Hello.java pig.txt 模板 图片 下载 桌面
  16. [root@localhost myroot]# pwd
  17. /home/myroot

删除软连接 myroot

  1. [root@localhost home]# rm /home/myroot
  2. rm:是否删除符号链接 "myroot"y

history 指令

查看已经执行过历史命令,也可以执行历史指令

  1. history # 功能描述:查看已经执行过历史命令

显示所有的历史命令

  1. [root@localhost home]# history
  2. 1 curl www.baidu.com
  3. 2 exit
  4. ...

显示最近使用过的 10 个指令

  1. [root@localhost home]# history 10
  2. 135 ll
  3. 136 cd /
  4. 137 ll
  5. 138 cd /home/
  6. 139 ll
  7. 140 rm myroot
  8. 141 ll
  9. 142 histroy
  10. 143 history
  11. 144 history 10

执行历史编号为 1 的指令

  1. [root@localhost home]# !1
  2. curl www.baidu.com
  3. <!DOCTYPE html>
  4. ...

时间日期类

date 指令-显示当前日期

  1. date # 功能描述:显示当前时间
  2. date +%Y # 功能描述:显示当前年份
  3. date +%m # 功能描述:显示当前月份
  4. date +%d # 功能描述:显示当前是哪一天
  5. date "+%Y-%m-%d %H:%M:%S" #功能描述:显示年月日时分秒

显示当前时间信息

  1. [root@localhost ~]# date
  2. 2021 07 04 星期日 21:41:03 CST

显示当前时间年月日

  1. [root@localhost ~]# date "+%Y-%m-%d"
  2. 2021-07-04

显示当前时间年月日时分秒

  1. [root@localhost ~]# date "+%Y-%m-%d %H:%M:%S"
  2. 2021-07-04 21:43:05

date 指令-设置日期

  1. date -s 字符串时间

设置系统当前时间 , 比如设置成 2020-11-03 20:02:10

  1. [root@localhost ~]# date -s "2020-11-03 20:02:10"
  2. 2020 11 03 星期二 20:02:10 CST
  3. [root@localhost ~]# date "+%Y-%m-%d %H:%M:%S"
  4. 2020-11-03 20:02:12

cal 指令-查看日历

  1. cal [选项] # 功能描述:不加选项,显示本月日历

显示当前日历 cal

  1. [root@localhost ~]# cal
  2. 十一月 2020
  3. 1 2 3 4 5 6 7
  4. 8 9 10 11 12 13 14
  5. 15 16 17 18 19 20 21
  6. 22 23 24 25 26 27 28
  7. 29 30

显示 2020 年日历 : cal 2020

  1. [root@localhost ~]# cal 2020
  2. 2020
  3. 一月 二月 三月
  4. 1 2 3 4 1 1 2 3 4 5 6 7
  5. 5 6 7 8 9 10 11 2 3 4 5 6 7 8 8 9 10 11 12 13 14
  6. 12 13 14 15 16 17 18 9 10 11 12 13 14 15 15 16 17 18 19 20 21
  7. 19 20 21 22 23 24 25 16 17 18 19 20 21 22 22 23 24 25 26 27 28
  8. 26 27 28 29 30 31 23 24 25 26 27 28 29 29 30 31
  9. 四月 五月 六月
  10. 1 2 3 4 1 2 1 2 3 4 5 6
  11. 5 6 7 8 9 10 11 3 4 5 6 7 8 9 7 8 9 10 11 12 13
  12. 12 13 14 15 16 17 18 10 11 12 13 14 15 16 14 15 16 17 18 19 20
  13. 19 20 21 22 23 24 25 17 18 19 20 21 22 23 21 22 23 24 25 26 27
  14. 26 27 28 29 30 24 25 26 27 28 29 30 28 29 30
  15. 31
  16. 七月 八月 九月
  17. 1 2 3 4 1 1 2 3 4 5
  18. 5 6 7 8 9 10 11 2 3 4 5 6 7 8 6 7 8 9 10 11 12
  19. 12 13 14 15 16 17 18 9 10 11 12 13 14 15 13 14 15 16 17 18 19
  20. 19 20 21 22 23 24 25 16 17 18 19 20 21 22 20 21 22 23 24 25 26
  21. 26 27 28 29 30 31 23 24 25 26 27 28 29 27 28 29 30
  22. 30 31
  23. 十月 十一月 十二月
  24. 1 2 3 1 2 3 4 5 6 7 1 2 3 4 5
  25. 4 5 6 7 8 9 10 8 9 10 11 12 13 14 6 7 8 9 10 11 12
  26. 11 12 13 14 15 16 17 15 16 17 18 19 20 21 13 14 15 16 17 18 19
  27. 18 19 20 21 22 23 24 22 23 24 25 26 27 28 20 21 22 23 24 25 26
  28. 25 26 27 28 29 30 31 29 30 27 28 29 30 31

搜索查找类

find 指令

find 指令将从指定目录向下递归地遍历其各个子目录,将满足条件的文件或者目录显示在终端。

  1. find [搜索范围] [选项]
  2. 常用选项
  3. -name<文件名> # 按指定的文件名查找文件 可以用*代替未知 如 *.txt
  4. -user<用户名> # 查找属于指定用户的所有文件
  5. -size<文件大小> # 按照指定的文件大小查找文件

按文件名:根据名称查找/home 目录下的 hello.txt 文件

  1. [root@localhost home]# find /home -name hello.txt
  2. /home/hello.txt

按拥有者:查找/opt 目录下,用户名称为 nobody 的文件

  1. [root@localhost home]# find /opt -user nobody
  2. [root@localhost home]#

查找整个 linux 系统下大于 200M 的文件(+n 大于 -n 小于 n 等于, 单位有 k,M,G)

  1. [root@localhost home]# find / -size +200M
  2. /proc/kcore
  3. find: ‘/proc/9948/task/9948/fd/5’: 没有那个文件或目录
  4. find: ‘/proc/9948/task/9948/fdinfo/5’: 没有那个文件或目录
  5. find: ‘/proc/9948/fd/6’: 没有那个文件或目录
  6. find: ‘/proc/9948/fdinfo/6’: 没有那个文件或目录

locate 指令

locate 指令可以快速定位文件路径。locate 指令利用事先建立的系统中所有文件名称及路径的 locate 数据库实现快速定位给定的文件。Locate 指令无需遍历整个文件系统,查询速度较快。为了保证查询结果的准确度,管理员必须定期更新 locate 时刻

  1. locate 搜索文件
  2. 特别说明
  3. 由于 locate 指令基于数据库进行查询,所以第一次运行前,必须使用 updatedb 指令创建 locate 数据库。

使用 locate 指令快速定位 hello.txt 文件所在目录

  1. [root@localhost home]# updatedb
  2. [root@localhost home]# locate hello.txt
  3. /home/hello.txt

which 指令

可以查看某个指令在哪个目录下

  1. which [指令名称]

查看ls 指令在那个目录

  1. [root@localhost home]# which ls
  2. alias ls='ls --color=auto'
  3. /usr/bin/ls

grep 指令和管道符号 |

grep 过滤查找 , 管道符 |,表示将前一个命令的处理结果输出传递给后面的命令处理。

  1. grep [选项] 查找内容 源文件
  2. 常用选项
  3. -n # 显示匹配行及行号
  4. -i # 忽略字母大小写

在 hello.txt 文件中,查找 “yes” 所在行,并且显示行号

  1. [root@localhost home]# cat /home/hello.txt |grep "yes"
  2. yes
  3. [root@localhost home]# grep -n "yes" /home/hello.txt
  4. 1:yes

压缩和解压类

gzip/gunzip 指令

gzip 用于压缩文件, gunzip 用于解压

  1. gzip 文件 # 功能描述:压缩文件,只能将文件压缩为*.gz 文件
  2. gunzip 文件.gz # 功能描述:解压缩文件命令

gzip 压缩, 将 /home 下的 hello.txt 文件进行压缩

  1. [root@localhost home]# gzip /home/hello.txt
  2. [root@localhost home]# ll
  3. 总用量 8
  4. -rw-r--r--. 1 root root 34 7 4 22:42 hello.txt.gz
  5. drwx------. 3 tom tom 4096 4 11 2018 tom

gunzip 压缩, 将 /home 下的 hello.txt.gz 文件进行解压缩

  1. [root@localhost home]# gunzip /home/hello.txt.gz
  2. [root@localhost home]# ll
  3. 总用量 8
  4. -rw-r--r--. 1 root root 4 7 4 22:42 hello.txt
  5. drwx------. 3 tom tom 4096 4 11 2018 tom

zip/unzip 指令

zip 用于压缩文件, unzip 用于解压的,这个在项目打包发布中很有用的

  1. zip [选项] XXX.zip 将要压缩的内容 # 功能描述:压缩文件和目录的命令
  2. unzip [选项] XXX.zip # 功能描述:解压缩文件
  3. zip 常用选项
  4. -r:递归压缩,即压缩目录
  5. unzip 的常用选项
  6. -d<目录> :指定解压后文件的存放目录

将 /home 下的所有文件/文件夹进行压缩成 myhome.zip

  1. [root@localhost home]# zip -r myhome.zip /home/
  2. adding: home/ (stored 0%)
  3. adding: home/hello.txt (stored 0%)
  4. adding: home/tom/ (stored 0%)
  5. adding: home/tom/.mozilla/ (stored 0%)
  6. adding: home/tom/.mozilla/extensions/ (stored 0%)
  7. adding: home/tom/.mozilla/plugins/ (stored 0%)
  8. adding: home/tom/.bash_logout (stored 0%)
  9. adding: home/tom/.bash_profile (deflated 21%)
  10. adding: home/tom/.bashrc (deflated 23%)
  11. [root@localhost home]# ll
  12. 总用量 12
  13. -rw-r--r--. 1 root root 4 7 4 22:42 hello.txt
  14. -rw-r--r--. 1 root root 1847 7 4 22:51 myhome.zip
  15. drwx------. 3 tom tom 4096 4 11 2018 tom

将 myhome.zip 解压到 /opt/tmp 目录下

  1. [root@localhost home]# mkdir /opt/tmp
  2. [root@localhost home]# unzip -d /opt/tmp/ /home/myhome.zip
  3. Archive: /home/myhome.zip
  4. creating: /opt/tmp/home/
  5. extracting: /opt/tmp/home/hello.txt
  6. creating: /opt/tmp/home/tom/
  7. creating: /opt/tmp/home/tom/.mozilla/
  8. creating: /opt/tmp/home/tom/.mozilla/extensions/
  9. creating: /opt/tmp/home/tom/.mozilla/plugins/
  10. extracting: /opt/tmp/home/tom/.bash_logout
  11. inflating: /opt/tmp/home/tom/.bash_profile
  12. inflating: /opt/tmp/home/tom/.bashrc
  13. [root@localhost home]# cd /opt/tmp/
  14. [root@localhost tmp]# ll
  15. 总用量 4
  16. drwxr-xr-x. 3 root root 4096 7 4 22:51 home

tar 指令

tar 指令 是打包指令,最后打包后的文件是 .tar.gz 的文件。

  1. tar [选项] XXX.tar.gz 打包的内容 # 功能描述:打包目录,压缩后的文件格式.tar.gz
  2. 常用选项
  3. -c # 产生.tar打包文件
  4. -v # 显示详细信息
  5. -f # 指定压缩后的文件名
  6. -z # 打包同时压缩
  7. -x # 解包.tar文件

压缩多个文件,将 /home/pig.txt 和 /home/cat.txt 压缩成 pc.tar.gz

  1. [root@localhost home]# tar -zcvf pc.tar.gz /home/pig.txt /home/cat.txt
  2. tar: 从成员名中删除开头的“/”
  3. /home/pig.txt
  4. /home/cat.txt
  5. [root@localhost home]# ls
  6. cat.txt hello.txt myhome.zip pc.tar.gz pig.txt tom

将/home 的文件夹 压缩成 myhome.tar.gz

  1. [root@localhost home]# tar -zcvf myhome.ter.gz /home/
  2. tar: 从成员名中删除开头的“/”
  3. /home/
  4. /home/myhome.zip
  5. /home/hello.txt
  6. /home/tom/
  7. /home/tom/.mozilla/
  8. /home/tom/.mozilla/extensions/
  9. /home/tom/.mozilla/plugins/
  10. /home/tom/.bash_logout
  11. /home/tom/.bash_profile
  12. /home/tom/.bashrc
  13. /home/cat.txt
  14. /home/pc.tar.gz
  15. /home/pig.txt
  16. /home/myhome.tar.gz
  17. [root@localhost home]# ls
  18. cat.txt hello.txt myhome.tar.gz myhome.ter.gz myhome.zip pc.tar.gz pig.txt tom

将 pc.tar.gz 解压到当前目录

  1. [root@localhost home]# tar -zxvf pc.tar.gz
  2. home/pig.txt
  3. home/cat.txt
  4. [root@localhost home]# ls
  5. cat.txt hello.txt home myhome.tar.gz myhome.zip pc.tar.gz pig.txt tom

将myhome.tar.gz 解压到 /opt/tmp2 目录下

  1. [root@localhost home]# tar -zxvf /home/myhome.tar.gz -C /opt/tmp2
  2. home/
  3. home/myhome.zip
  4. home/hello.txt
  5. home/tom/
  6. home/tom/.mozilla/
  7. home/tom/.mozilla/extensions/
  8. home/tom/.mozilla/plugins/
  9. home/tom/.bash_logout
  10. home/tom/.bash_profile
  11. home/tom/.bashrc
  12. home/cat.txt
  13. home/pc.tar.gz
  14. home/pig.txt
  15. [root@localhost home]# cd /opt/tmp2
  16. [root@localhost tmp2]# ll
  17. 总用量 4
  18. drwxr-xr-x. 3 root root 4096 7 4 23:02 home
  19. [root@localhost tmp2]# cd home/
  20. [root@localhost home]# ls
  21. cat.txt hello.txt myhome.zip pc.tar.gz pig.txt tom