劲爆!超全的常用Linux指令它来啦!

简介: 劲爆!超全的常用Linux指令它来啦!

 

前言:

📕作者简介:热爱编程的敖云岚,致力于C、Java、Python等多编程语言,热爱编程和长板的运动少年!

📘相关专栏Java基础语法,JavaEE初阶,数据库,数据结构和算法系列等,大家有兴趣的可以看一看。

😇😇😇有兴趣的话关注博主一起学习,一起进步吧!

目录

01. ls 指令

02. pwd命令

03. cd 指令(换盘符)

04. touch指令

05.mkdir指令(重要)

06.rmdir指令 && rm 指令(重要)

07.man指令(重要)

08.cp指令(重要)

09.mv指令(重要)

10.cat

11.grep指令

12.zip/unzip指令

13.重要的几个热键[Tab],[ctrl]-c, [ctrl]-d

14.chmod

15.echo指令

16.ps 命令

17.netstat命令


第一章、常用基本指令

# 注意:
# #表示管理员
# $表示普通用户
[root@localhost Practice]# 说明此处表示管理员
image.gif

01. ls 指令

语法: ls [选项][目录或文件]

功能:对于目录,该命令列出该目录下的所有子目录与文件。对于文件,将列出文件名以及其他信息。

# 列出该目录下的所有子目录与文件信息
[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  MyLinux   Public     test.c
Desktop          Downloads  Music                 Pictures  Templates  Videos
# 同时列出隐藏文件
# .表示当前目录,  ..表示上级目录
[root@localhost ~]# ls -a
.                .bashrc  Documents             Music     Templates
..               .cache   Downloads             MyLinux   test.c
anaconda-ks.cfg  .config  .esd_auth             Pictures  Videos
.bash_history    .cshrc   .ICEauthority         .pki      .viminfo
.bash_logout     .dbus    initial-setup-ks.cfg  Public    .Xauthority
.bash_profile    Desktop  .local                .tcshrc

image.gif

image.gif编辑

~ 表示所在根目录(也就是家目录)

# /表示根目录,查看根目录下的文件
[root@localhost Practice]# ls /
bin   dev  home  lib64  mnt  proc  run   srv  tmp    usr
boot  etc  lib   media  opt  root  sbin  sys  Trash  var

image.gif

# 查看root用户的家目录
[root@localhost Practice]# ls /root
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  MyLinux  Pictures  Templates
Desktop          Downloads  Music                 MyTrash  Public    Videos

image.gif

常用选项:

    • -a 列出目录下的所有文件,包括以 . 开头的隐含文件。
    • -d 将目录像文件一样显示,而不是显示其下的文件。 如:ls –d 指定目录
    • -i 输出文件的 i 节点的索引信息。 如 ls –ai 指定文件
    • -k 以 k 字节的形式表示文件的大小。ls –alk 指定文件
    • -l 列出文件的详细信息。
    • -n 用数字的 UID,GID 代替名称。 (介绍 UID, GID)
    • -F 在每个文件名后附上一个字符以说明该文件的类型,“*”表示可执行的普通文件;“/”表示目录;“@”表示符号链接;“|”表示FIFOs;“=”表示套接字(sockets)。(目录类型识别)
    • -r 对目录反向排序。
    • -t 以时间排序。
    • -s 在l文件名后输出该文件的大小。(大小排序,如何找到目录下最大的文件)
    • -R 列出所有子目录下的文件。(递归)
    • -1 一行只输出一个文件。

    02. pwd命令

    语法: pwd

    功能:显示用户当前所在的目录

    # cd 进入MyLinux文件夹
    [root@localhost ~]# cd MyLinux
    # pwd查看当前所在目录
    [root@localhost MyLinux]# pwd
    /root/MyLinux

    image.gif

    03. cd 指令(换盘符)

    Linux系统中,磁盘上的文件和目录被组成一棵目录树,每个节点都是目录或文件。

    image.gif编辑

    语法 :cd  目录名

    功能 :改变工作目录。将当前工作目录改变到指定的目录下。

    # 进入MyLinux文件夹
    [root@localhost ~]# cd MyLinux
    # pwd查看当前所在文件目录
    [root@localhost MyLinux]# pwd
    /root/MyLinux
    # ls查看该文件夹下的子文件,我们进入到Practice文件中
    [root@localhost MyLinux]# ls
    Practice
    [root@localhost MyLinux]# cd Practice
    # 
    [root@localhost Practice]# pwd
    /root/MyLinux/Practice
    # 同样可通过 cd.. 表示进入上一级目录
    [root@localhost Practice]# cd ..
    # 此时我们又回到MyLinux目录了
    [root@localhost MyLinux]# pwd
    /root/MyLinux
    # cd .表示进入当前目录,所以不发生改变
    [root@localhost MyLinux]# cd .
    [root@localhost MyLinux]# pwd
    /root/MyLinux
    # 绝对路径:从根目录出发,以/开头
    [root@localhost ~]# cd /root/MyLinux/Practice
    [root@localhost Practice]# pwd
    /root/MyLinux/Practice
    # 相对路径:从基准目录出发,可以是./开头,也可以是../开头,也可以省略
    # 从Practice目录出发,到达上级目录的上级目录的MyLinux文件夹
    [root@localhost Practice]# cd ../../MyLinux
    [root@localhost MyLinux]# pwd
    /root/MyLinux

    image.gif

    # cd 不加任何选项可以回到根目录
    [root@localhost Practice]# cd
    [root@localhost ~]# pwd
    /root

    image.gif

    举例:

      • cd .. : 返回上级目录
      • cd /home/litao/linux/ : 绝对路径
      • cd ../day02/ : 相对路径
      • cd ~:进入用户家目
      • cd -:返回最近访问目录

      04. touch指令

      语法:touch [选项]... 文件...

      功能:touch命令参数可更改文档或目录的日期时间,包括存取时间和更改时间,或者新建一个不存在的文件。

      # 指定创建创建时间为22:00 的test2.c文件
      [root@localhost Practice]# touch -d 22:00 test2.c
      [root@localhost Practice]# ll
      total 16
      -rwxr-xr-x. 1 root root 8512 Aug  5 20:02 test
      -rw-r--r--. 1 root root    0 Aug  5 22:00 test2.c
      -rw-r--r--. 1 root root   73 Aug  5 20:02 test.c

      image.gif

      常用选项:

        • -a 或--time=atime或--time=access或--time=use只更改存取时间。
        • -c 或--no-create 不建立任何文档。
        • -d 使用指定的日期时间,而非现在的时间。
        • -f 此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题。
        • -m 或--time=mtime或--time=modify 只更改变动时间。
        • -r 把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。
        • -t 使用指定的日期时间,而非现在的时间。

        05.mkdir指令(重要)

        语法:mkdir [选项] dirname...

        功能:在当前目录下创建一个名为 “dirname”的目录

        常用选项:

          • -p, --parents 可以是一个路径名称。此时若路径中的某些目录尚不存在,加上此选项后,系统将自动建立好那些尚不存在的目录,即一次可以建立多个目录;
          # 递归建立多个目录
          [root@localhost MyLinux]# mkdir Practice/test3.txt
          [root@localhost MyLinux]# cd Practice
          [root@localhost Practice]# ll
          total 16
          -rwxr-xr-x. 1 root root 8512 Aug  5 20:02 test
          -rw-r--r--. 1 root root    0 Aug  5 22:00 test2.c
          drwxr-xr-x. 2 root root    6 Aug  5 22:31 test3.txt
          -rw-r--r--. 1 root root   73 Aug  5 20:02 test.c

          image.gif

          06.rmdir指令 && rm 指令(重要)

          rmdir是一个与mkdir相对应的命令。mkdir是建立目录,而rmdir是删除命令。

          语法:rmdir [-p][dirName]

          适用对象:具有当前目录操作权限的所有使用者

          功能:删除空目录

          常用选项:

            • -p 当子目录被删除后如果父目录也变成空目录的话,就连带父目录一起删除。

            rm命令可以同时删除文件或目录

            语法:rm [-f-i-r-v][dirName/dir]

            适用对象:所有使用者

            功能:删除文件或目录

            常用选项:

              • -f 即使文件属性为只读(即写保护),亦直接删除
              • -i 删除前逐一询问确认
              • -r 删除目录及其下所有文件
              # 删除test2.c文件
              [root@localhost Practice]# rm test2.c
              rm: remove regular empty file ‘test2.c’? y
              # 查看所有子目录
              [root@localhost Practice]# ll
              total 16
              -rwxr-xr-x. 1 root root 8512 Aug  5 20:02 test
              drwxr-xr-x. 2 root root    6 Aug  5 22:31 test3.txt
              -rw-r--r--. 1 root root   73 Aug  5 20:02 test.c
              #递归删除Practice及其下所有文件,会询问是否删除
              [root@localhost Practice]# rm -r Practice
              rm: cannot remove ‘Practice’: No such file or directory
              [root@localhost Practice]# cd ..
              [root@localhost MyLinux]# rm -r Practice
              rm: descend into directory ‘Practice’? y
              rm: remove regular file ‘Practice/test.c’? y
              rm: remove regular file ‘Practice/test’? y
              rm: remove directory ‘Practice/test3.txt’? y
              rm: remove directory ‘Practice’? y
              # Practice为空目录
              [root@localhost MyLinux]# ll
              total 0

              image.gif

              07.man指令(重要)

              Linux的命令有很多参数,我们不可能全记住,我们可以通过查看联机手册获取帮助。访问Linux手册页的命令是man 语法: man [选项] 命令。

              常用选项:

                • -k 根据关键字搜索联机帮助
                • num 只在第num章节找
                • -a 将所有章节的都显示出来,比如 man printf 它缺省从第一章开始搜索,知道就停止,用a选项,当按下q退出,他会继续往后面搜索,直到所有章节都搜索完毕。
                • 解释一下,面手册分为8章
                • 1 是普通的命令
                • 2 是系统调用,如open,write之类的(通过这个,至少可以很方便的查到调用这个函数,需要加什么头文件)
                • 3 是库函数,如printf,fread4是特殊文件,也就是/dev下的各种设备文件
                • 5 是指文件的格式,比如passwd, 就会说明这个文件中各个字段的含义
                • 6 是给游戏留的,由各个游戏自己定义
                • 7 是附件还有一些变量,比如向environ这种全局变量在这里就有说明
                • 8 是系统管理用的命令,这些命令只能由root使用,如ifconfig
                # 当我们想查看 mkdir 使用功能:
                man mkdir
                MKDIR(1)                          User Commands                         MKDIR(1)
                NAME
                       mkdir - make directories
                SYNOPSIS
                       mkdir [OPTION]... DIRECTORY...
                DESCRIPTION
                       Create the DIRECTORY(ies), if they do not already exist.
                       Mandatory arguments to long options are mandatory for short options too.
                       -m, --mode=MODE
                              set file mode (as in chmod), not a=rwx - umask
                       -p, --parents
                              no error if existing, make parent directories as needed
                       -v, --verbose
                              print a message for each created directory
                       -Z     set  SELinux  security  context  of  each created directory to the
                              default type
                       --context[=CTX]
                              like -Z, or if CTX is specified then  set  the  SELinux  or  SMACK
                 Manual page mkdir(1) line 1 (press h for help or q to quit)

                image.gif

                08.cp指令(重要)

                语法:cp [选项] 源文件或目录 目标文件或目录

                功能: 复制文件或目录

                说明: cp指令用于复制文件或目录,如同时指定两个以上的文件或目录,且最后的目的地是一个已经存在的目录,则它会把前面指定的所有文件或目录复制到此目录中。若同时指定多个文件或目录,而最后的目的地并非一个已存在的目录,则会出现错误信息。

                常用选项:

                  • -f --force 强行复制文件或目录, 不论目的文件或目录是否已经存在
                  • -i 或 --interactive 覆盖文件之前先询问用户
                  • -r递归处理,将指定目录下的文件与子目录一并处理。若源文件或目录的形态,不属于目录或符号链接,则一律视为普通文件处理
                  • -R 或 --recursive递归处理,将指定目录下的文件及子目录一并处理
                  # 进入Practice目录
                  [root@localhost MyLinux]# cd Practice
                  # 查看Practice目录中的文件
                  [root@localhost Practice]# ls
                  test1.txt  test2.txt
                  # 退到上级目录
                  [root@localhost Practice]# cd ..
                  # 在与Practice同级的目录中创建Practice2文件夹
                  [root@localhost MyLinux]# mkdir Practice2
                  # 将Practice文件及其所含内容递归复制到Practice2文件中
                  [root@localhost MyLinux]# cp -r Practice Practice2 
                  # 进入Practice2文件中查看发现有Practice文件
                  [root@localhost MyLinux]# cd Practice2
                  [root@localhost Practice2]# ls
                  Practice
                  [root@localhost Practice2]# cd Practice
                  # 发现Practice文件夹中的子文件也被复制
                  [root@localhost Practice]# ls
                  test1.txt  test2.txt
                  [root@localhost Practice]#

                  image.gif

                  一般情况下建议 cp 命令搭配 -r  一起使用

                  09.mv指令(重要)

                  mv命令是move的缩写,可以用来移动文件或者将文件改名(move (rename) files),是Linux系统下常用的命令,经常用来备份文件或者目录。

                  语法: mv [选项] 源文件或目录 目标文件或目录

                  功能:

                  1. 视mv命令中第二个参数类型的不同(是目标文件还是目标目录),mv命令将文件重命名或将其移至一个新的目录中。

                  2. 当第二个参数类型是文件时,mv命令完成文件重命名,此时,源文件只能有一个(也可以是源目录名),它将所给的源文件或目录重命名为给定的目标文件名。

                  3. 当第二个参数是已存在的目录名称时,源文件或目录参数可以有多个,mv命令将各参数指定的源文件均移至目标目录中。

                  常用选项:

                    • -f :force 强制的意思,如果目标文件已经存在,不会询问而直接覆盖
                    • -i :若目标文件 (destination) 已经存在时,就会询问是否覆盖!
                    # 重命名文件:在Practice文件夹中
                    [root@localhost Practice]# ls
                    test1.txt  test2.txt
                    # 将test2.txt重命名为test.txt
                    [root@localhost Practice]# mv test2.txt test.txt
                    [root@localhost Practice]# ls
                    test1.txt  test.txt

                    image.gif

                    # 移动文件
                    [root@localhost Practice2]# ls
                    Practice
                    [root@localhost Practice2]# touch test3.txt
                    [root@localhost Practice2]# touch test4.txt
                    # 将Practice2文件夹中的test3.txt和test4.txt移动到Practice的文件夹中
                    [root@localhost Practice2]# mv test3.txt Practice
                    [root@localhost Practice2]# mv test4.txt Practice
                    # 移动成功
                    [root@localhost Practice2]# cd Practice
                    [root@localhost Practice]# ls
                    test1.txt  test3.txt  test4.txt  test.txt

                    image.gif

                    # 备份文件
                    # 首先创建一个文件夹
                    [root@localhost ~]# mkdir MyTrash
                    [root@localhost ~]# ls
                    anaconda-ks.cfg  Documents  initial-setup-ks.cfg  MyLinux  Pictures  Templates
                    Desktop          Downloads  Music                 MyTrash  Public    Videos
                    [root@localhost ~]# cd MyLinux
                    [root@localhost MyLinux]# ls
                    Practice  Practice2
                    # 将Practice文件备份到上级目录的MyTrash文件夹
                    [root@localhost MyLinux]# mv Practice ../MyTrash
                    # 进入MyTrash文件夹查看备份成功
                    [root@localhost MyLinux]# cd ..
                    [root@localhost ~]# cd MyTrash
                    [root@localhost MyTrash]# ls
                    Practice

                    image.gif

                    10.cat

                    语法:cat [选项][文件]

                    功能: 查看目标文件的内容

                    常用选项:

                      • -b 对非空输出行编号
                      • -n 对输出的所有行编号
                      • -s 不输出多行空行
                      # 进入Practice文件夹
                      [root@localhost MyLinux]# cd Practice
                      [root@localhost Practice]# ls
                      # 编辑一个test.txt文件夹
                      # vim 按a进入编辑,编辑好按esc,再按shift+;即可
                      [root@localhost Practice]# vim test.txt
                      # 查看刚才我们编辑的内容
                      [root@localhost Practice]# cat test.txt
                      Hello World!
                      # 显示的同时输出行号
                      [root@localhost Practice]# cat -n test.txt
                           1  Hello World!

                      image.gif

                      11.grep指令

                      语法: grep [选项] 搜寻字符串 文件

                      功能:在文件中搜索字符串,将找到的行打印出来

                      常用选项:

                        • -i :忽略大小写的不同,所以大小写视为相同
                        • -n :顺便输出行号
                        • -v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行
                        # 搜索test.txt文件中的Hello相关的字符串
                        [root@localhost Practice]# grep 'Hello' test.txt
                        Hello World!

                        image.gif

                        # 多文件中匹配字符串
                        # * 表示当前目录下的所有文件
                        # -R表示递归搜索,如果当前目录下还有目录,就会自动进入下一个目录中进行搜索
                        [root@localhost Practice]# grep 'Hello' * -R 
                        test2.txt:Hello Linux
                        test.txt:Hello Linux

                        image.gif

                        12.zip/unzip指令

                        语法: zip 压缩文件.zip 目录或文件/解压缩

                        功能: 将目录或文件压缩成zip格式/解压缩

                        常用选项:

                          • -r 递 归处理,将指定目录下的所有文件和子目录一并处理
                          将test2目录压缩:zip test2.zip test2/*
                          解压到tmp目录:unzip test2.zip -d /tmp

                          image.gif

                          13.chmod

                          功能设置文件的访问权限

                          格式:chmod [参数] 权限 文件名

                          常用选项:

                            • R -> 递归修改目录文件的权限
                            • 说明:只有文件的拥有者和root才可以改变文件的权限

                            chmod命令权限值的格式:用户表示符+/-=权限字符

                              • +:向权限范围增加权限代号所表示的权限
                              • -:向权限范围取消权限代号所表示的权限
                              • =:向权限范围赋予权限代号所表示的权限
                              • 用户符号:
                              • u:拥有者
                              • g:拥有者同组用
                              • o:其它用户
                              • a:所有用户
                              [root@localhost Practice]# ll
                              total 8
                              -rw-r--r--. 1 root root 12 Aug  7 12:45 test2.txt
                              -rw-r--r--. 1 root root 12 Aug  7 13:05 test.txt
                              # 给test.txt增加可执行的权限
                              [root@localhost Practice]# chmod +x test.txt
                              [root@localhost Practice]# ll
                              total 8
                              -rw-r--r--. 1 root root 12 Aug  7 12:45 test2.txt
                              -rwxr-xr-x. 1 root root 12 Aug  7 13:05 test.txt

                              image.gif

                              14.echo指令

                              功能:用于写文件,搭配重定向使用

                              # echo没有把Hello Linux显示到控制台,而是写入了后面的test.txt文件中
                              # 每次echo+重定向,会清空原有的内容
                              [root@localhost Practice]# cat test.txt
                              Hello World!
                              [root@localhost Practice]# echo 'Hello Linux'>test.txt
                              [root@localhost Practice]# cat test.txt
                              Hello Linux

                              image.gif

                              echo 和 cat 命令只适合针对简单的文件进行操作(写脚本)。如果文件复杂就不太适合了。

                              15.ps 命令

                              功能: 查看系统进程

                              # 使用ps aux能够列出系统上的所有进程
                              # 配合grep就可以快速筛选
                              # | 管道用来连接两个命令:前一个命令的输出作为后一个命令的输入
                              [root@localhost Practice]# ps aux | grep mysql
                              root       3418  0.0  0.0 112812   980 pts/1    R+   13:31   0:00 grep --color=auto mysql
                              # 3418表示pid,是进程的身份标识,我们每次敲下的命令自身也是一个进程

                              image.gif

                              16.netstat命令

                              功能:查询网络状态

                              # netstat搭配-anp使用, 搭配grep使用进行筛选
                              # 查询mysql服务器的端口号
                              [root@localhost Practice]# netstat -anp | grep mysql

                              image.gif

                              17.重要的几个热键[Tab],[ctrl]-c, [ctrl]-d

                                • [Tab]按键---具有『命令补全』和『档案补齐』的功能
                                # 当我们输入文件名或其他时忘记全程通过Tab键可以补齐
                                # 按两次表示显示所有相关的名称

                                image.gif

                                  • [Ctrl]-c按键---让当前的程序『停掉』
                                  # 冻结程序:ctrl+c
                                  # 解除冻结:ctrl+q

                                  image.gif

                                    • [Ctrl]-d按键---通常代表着:『键盘输入结束(End Of File, EOF 戒 End OfInput)』的意思;另外,他也可以用来取代exit
                                    # 当我们不想让进程执行时,按ctrl + d 即可退出

                                    image.gif

                                    目录
                                    相关文章
                                    |
                                    3天前
                                    |
                                    Linux
                                    Linux:文件读取指令
                                    Linux:文件读取指令
                                    11 2
                                    |
                                    13天前
                                    |
                                    Linux
                                    【Linux系统编程】基础指令(二)(下)
                                    【Linux系统编程】基础指令(二)
                                    |
                                    13天前
                                    |
                                    Linux C语言
                                    【Linux系统编程】基础指令(二)(上)
                                    【Linux系统编程】基础指令(二)
                                    |
                                    3天前
                                    |
                                    缓存 安全 Linux
                                    Linux入门基本指令(2)
                                    Linux入门基本指令(2)
                                    10 0
                                    |
                                    3天前
                                    |
                                    Linux Windows
                                    Linux入门基本指令(1)-2
                                    Linux入门基本指令(1)
                                    9 1
                                    |
                                    3天前
                                    |
                                    Linux 数据安全/隐私保护 Windows
                                    Linux入门基本指令(1)-1
                                    Linux入门基本指令(1)
                                    13 1
                                    |
                                    3天前
                                    |
                                    算法 Linux
                                    Linux:文件增删 & 文件压缩指令
                                    Linux:文件增删 & 文件压缩指令
                                    6 0
                                    |
                                    3天前
                                    |
                                    Linux
                                    Linux:时间指令 - cal & date
                                    Linux:时间指令 - cal & date
                                    9 2
                                    |
                                    4天前
                                    |
                                    人工智能 数据可视化 Unix
                                    【Linux】Linux系统基础指令 ( 1 )
                                    本文讲解的指令是基础的操作,下一篇文章我们将继续讲解不同指令。
                                    14 0