• Linux基本命令


    目录操作

    格式:指令 -[参数] [目录]

    1. pwd (Print Working Direcotry)

    查看当前工作路径
    在这里插入图片描述

    2. cd (Change Direcotry)

    改变 shell 工作目录
    在这里插入图片描述

    3. ls (List Files)

    列出当前目录下的文件和目录
    ls -[参数] [目录]
    ls的参数均是短命令,可以使用-后添加多个参数
    可选参数:
    -l: 以长格式显示

    [root@localhost test]# ls
    demo1  demo2  demo3  demo4
    [root@localhost test]# ls -l
    总用量 0
    d-------wx. 2 redhat root    6 113 11:37 demo1
    drwxrwxrwx. 6 redhat redhat 58 118 10:46 demo2
    drwxrwxrwx. 2 redhat redhat  6 113 11:37 demo3
    drwxrwxrwx. 5 redhat redhat 45 118 10:54 demo4
    ### 如果目录下文件过多,此时我们只想查看某个文件的时间戳,大小等的信息,可以使用以下命令。
    [redhat@localhost test]$ ls a.txt -l
    -rw-r--r--. 1 root root 3781 121 12:12 a.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    -a:显示所有子目录和文件的信息,包括隐藏文件(开头为.的文件)

    [root@localhost test]# ls
    demo1  demo2  demo3  demo4
    [root@localhost test]# ls -a
    .  ..  .a.txt  demo1  demo2  demo3  demo4
    // 使用参数 -a 后可以看到当前目录 . 和上级目录 .. 以及隐藏文件 .a.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5

    -A:同a,但不显示当前目录 “.” 和上级目录"…"信息

    [root@localhost test]# ls -a
    .  ..  .a.txt  demo1  demo2  demo3  demo4
    [root@localhost test]# ls -A
    .a.txt  demo1  demo2  demo3  demo4
    
    • 1
    • 2
    • 3
    • 4

    -d:显示目录本身属性

    [root@localhost test]# ls -d
    .
    //-d 参数可以结合 -l一起使用,查看当前目录的相关属性
    [root@localhost test]# ls -dl
    drwxrwxrwx. 7 redhat redhat 72 12月  1 12:00 .
    
    • 1
    • 2
    • 3
    • 4
    • 5

    -h:以更易读的方式显示文件信息(K,M)

    [root@localhost test]# ls -l
    总用量 4
    -rw-r--r--. 1 root   root   3781 12月  1 12:12 a.txt
    d-------wx. 2 redhat root      6 11月  3 11:37 demo1
    drwxrwxrwx. 6 redhat redhat   58 11月  8 10:46 demo2
    drwxrwxrwx. 2 redhat redhat    6 11月  3 11:37 demo3
    drwxrwxrwx. 5 redhat redhat   45 11月  8 10:54 demo4
    [root@localhost test]# ls -hl
    总用量 4.0K
    -rw-r--r--. 1 root   root   3.7K 12月  1 12:12 a.txt   # 可以看到这里是3.7k
    d-------wx. 2 redhat root      6 11月  3 11:37 demo1
    drwxrwxrwx. 6 redhat redhat   58 11月  8 10:46 demo2
    drwxrwxrwx. 2 redhat redhat    6 11月  3 11:37 demo3
    drwxrwxrwx. 5 redhat redhat   45 11月  8 10:54 demo4
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    4. mkdir(Make Directory)

    创建新的目录

    • p:递归创建
    • m:直接添加权限
    [redhat@localhost test]$ mkdir -m 777 -p  demo5/a1/a2/a3
    [redhat@localhost test]$ ls -l
    drwxr-xr-x. 3 redhat redhat   16 12月  6 10:33 demo5
    [redhat@localhost demo5]$ ls -l
    drwxr-xr-x. 3 redhat redhat 16 12月  6 10:33 a1
    [redhat@localhost a1]$ ls -l
    drwxr-xr-x. 3 redhat redhat 16 12月  6 10:33 a2
    [redhat@localhost a1]$ cd a2/
    drwxrwxrwx. 2 redhat redhat 6 12月  6 10:33 a3
    # 只会修改最后一个的权限,前面的权限依旧为默认的
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5. rmdir(Remove Directory)

    删除文件夹

    • p :一次删除多个表
    [redhat@localhost test]$ rmdir -p demo5/a1/a2/a3/
    [redhat@localhost test]$ ls
    a.txt  demo1  demo2  demo3  demo4
    
    • 1
    • 2
    • 3

    6. du

    统计目录及文件占用情况

    • a:统计时包含所有文件
    • h:以K,M显示信息
    • s:只显示当前文件夹的信息
    [redhat@localhost test]$ sudo du -hs
    4.0K	.  #当前文件夹
    
    • 1
    • 2

    文件操作

    touch

    创建一个文件;如果存在修改时间戳。

    [redhat@localhost test]$ touch a1.txt
    [redhat@localhost test]$ ll
    -rw-r--r--. 1 redhat redhat    0 12月  6 10:59 a1.txt
    [redhat@localhost test]$ touch a1.txt
    [redhat@localhost test]$ ll
    -rw-r--r--. 1 redhat redhat 2554 12月  6 11:00 a1.txt
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    file

    查看指定文件的类型

    [redhat@localhost test]$ file a.txt 
    a.txt: UTF-8 Unicode text
    [redhat@localhost test]$ file .a
    .a: directory
    
    • 1
    • 2
    • 3
    • 4

    cp (copy)

    cp [可选参数] 源文件/目录 目的文件/目录

    • r:递归复制目录树
    • p:保持属性不变
    • f:强制覆盖目标同名文件或目录
    • i:需要覆盖文件或目录时提醒

    mv (Move)

    移动文件/目录。还可用来重命名

    • f:force 强制移动,若存在,直接覆盖(就是替换)
    • i: 若存在,会询问是否覆盖
    • u:若存在,且源文件Source是新的,才会升级Update
    [redhat@localhost test]$ ls
    a1.txt  a.txt  demo1  demo2  demo3  demo4
    [redhat@localhost test]$ mv a.txt ./demo4  # 移动
    [redhat@localhost test]$ ls
    a1.txt  demo1  demo2  demo3  demo4
    [redhat@localhost test]$ ls demo4
    a.txt  demo4-1  demo4-2  demo4-3
    [redhat@localhost test]$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    rm (Remove)

    删除文件夹后目录的

    • f: force强制删除,不提醒。
    • i: 删除时提醒
    • r:递归删除整个目录树

    which

    用于查找命令的,并且显示所在位置

    [redhat@localhost test]$ which ll
    alias ll='ls -l --color=auto'
    	/usr/bin/ls
    [redhat@localhost test]$ which ls
    alias ls='ls --color=auto'
    	/usr/bin/ls
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    find

    查找文件或目录
    find [查找范围] [条件]

    • name:按名查找,可使用*?等的通配符
    • size: 按大小
    • user:按属主
    • type:按类型

    ln

    创建链接(Link)
    格式: ln [-s 不选该参数即为硬链接] 源文件 链接文件或目标目录
    修改源文件后链接文件都会修改,硬链接是使用同一个数据,但是是两个一样的inode。

    文件内容操作

    cat

    查看文件内容

    • n:带行号显示
    • b:仅显示非空白行

    tac

    同上面相反,反着显示,从最后一行显示,是按行算的!!

    more less

    都是以全屏的形式进行分页查看

    • q:退出

    head tail

    格式:head/tail [-n] xx.txt

    grep

    在文件中查找字符串
    grep [参数] string a.txt

    • i: 查找时忽略大小
    • v:反转查找,输出与条件不相符的行
    • “^…” 以 … 开头,"…$"以…结尾。
    • “^$” 表示空行

    归档及压缩命令

    tar

    tar [参数] 归档名称 源文件

    • c:建立一个压缩文件的参数指令(create 的意思);
    • x:解开一个压缩文件的参数指令!
    • t:查看 tarfile 里面的文件!
    • r:向压缩归档文件末尾追加文件
    • u:更新原压缩包中的文件

    以上5个只能5选一使用

    • z:有gzip属性,即需要用 gzip 压缩
    • j:有bz2属性,即需要用 bzip2 压缩
    • Z:有compress属性的
    • v :压缩的过程中显示文件(显示所有过程)!这个常用,但不建议用在背景执行过程!
    • O:将文件解开到标准输出
    • f :使用档名,请留意,在 f 之后要立即接档名!不要再加参数!
      例如:使用『 tar -zcvfP tfile sfile』就是错误的写法,要写成『 tar -zcvPf tfile sfile』才对喔!
    • p :使用原文件的原来属性(属性不会依据使用者而变)
    • P :可以使用绝对路径来压缩!
    • N :比后面接的日期(yyyy/mm/dd)还要新的才会被打包进新建的文件中!
    • – exclude FILE:在压缩的过程中,不要将 FILE 打包!
    • f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

    常用组合命令

    tar -zxf aaa.tar.gz # 解压aaa.tar.gz 
    tar -zcf demo.tar.gz demo4 # 创建压缩包demo.tar.gz  使用demo4的内容创建
    tar -tf  demo.tar.gz  # 在未解压的情况下查看压缩包
    tar -xvf file.tar         // 解压 tar包
    tar -zxvf file.tar.gz     // 解压 tar.gz  
    tar -jxvf file.tar.bz2    // 解压 tar.bz2  
    tar -Zxvf file.tar.Z      // 解压 tar.Z  
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    tar详解

    文件属性

    更改文件权限
    用户,用户组。一个用户可以属于多个用户组。
    属主:所属的用户,文档所有者,这是一个账户,这是一个人
    属组:所属的用户组,这是一个组

    • chgrp(change group):修改所属组
    • chown (change owner) : 修改所属用户与组。
    • chmod (change mode) : 修改用户的权限

    使用 llls-al 命令来查看文件相关信息

    drwxrwxrwx. 2 redhat redhat 6 113 11:37 demo1
    drwxrwxrwx. 2 redhat redhat 6 113 11:37 demo2
    drwxrwxrwx. 2 redhat redhat 6 113 11:37 demo3
    drwxrwxrwx. 2 redhat redhat 6 113 11:37 demo4
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述
    第一个表示属性,是文件还是目录。

    • d表示目录,document
    • l表示链接文档 link-file;
    • - 表示文件

    后面9个字符,3个一组,分别表示rwx

    • r :read 4
    • w: write 2
    • x:execute 1
    • - :没有权限的时候就会显示-
    • 顺序是固定的,就是rwx

    更改用户属组:

    chgrp [-R] 属组名 文件名
    -R 表示递归更改,就是把文件夹里的文件全改了
    chgrp root demo1 //把demo1所属的组改成root
    
    • 1
    • 2
    • 3

    更改用户属主:

    chown [–R] 属主名 文件名
    chown [-R] 属主名:属组名 文件名
    chown root demo1  //把文件demo1所属的主改成root
    chown root:root demo2  //把文件demo2的用户组和所有者都改成root
    
    • 1
    • 2
    • 3
    • 4

    更改文件9个属性:

    Linux 文件的基本权限就有九个,分别是 owner/group/others(拥有者/组/其他) 三种身份各有自己的 read/write/execute 权限。

    chmod [-R] -777 demo1
    
    • 1

    用户身份就三个,user,group,other, + 代表添加权限,-代表删除权限,=表示设置权限

    chmod [-R] u=rwx,g-w,o+r demo1
    
    • 1

    环境变量

    env

    查看所有环境变量

    [redhat@localhost test]$ env
    SHELL=/bin/bash
    HISTCONTROL=ignoredups
    HISTSIZE=1000
    HOSTNAME=localhost
    
    • 1
    • 2
    • 3
    • 4
    • 5

    有时环境变量过多,可使用grep来筛选

    [redhat@localhost test]$ env|grep HOME
    HOME=/home/redhat
    
    • 1
    • 2

    echo

    查看环境变量的内容
    echo $环境变量名称

    [redhat@localhost test]$ echo $HOME
    /home/redhat
    
    • 1
    • 2

    export

    设置环境变量
    export 变量名=‘值’
    在退出shell后就会失效,并非永久

    [redhat@localhost test]$ export h="hello"
    [redhat@localhost test]$ echo $h
    hello
    
    • 1
    • 2
    • 3

    set

    用于显示所有本地定义的shell变量

    unset

    用于清除环境变量。如果未指定值,该变量被置为NULL

    一些乱七八糟的命令

    history

      history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]
      -c	删除所有条目从而清空历史列表。
      
      -d 偏移量	从指定位置删除历史列表。负偏移量将从历史条目末尾
    		开始计数
    
      -a	将当前会话的历史行追加到历史文件中
      -n	从历史文件中读取所有未被读取的行
    		并且将它们附加到历史列表
      -r	读取历史文件并将内容追加到历史列表中
      -w	将当前历史写入到历史文件中
    
      -p	对每一个 ARG 参数展开历史并显示结果,而不存储到历史列表中
      -s	以单条记录追加 ARG 到历史列表中
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    H5 tab点击切换CSS样式- 微信小程序开发技术要点总结(一)
    vue新一代状态管理插件Pinia
    竞赛选题 目标检测-行人车辆检测流量计数
    Webpack--动态 import 原理及源码分析
    dependencyManagement和dependencies区别
    基于 JMeter API 开发性能测试平台
    C语言学习笔记(三)
    暴力递归到动态规划 07(516. 最长回文子序列)
    DES和3DES等常见加解密的关键要素---安全行业基础篇3
    2023年9月11日-9月16日(上午熟悉公司代码,周一到周五晚上优先工作所急视频教程,其他业余时间进行ue视频教程,为独立游戏做准备)
  • 原文地址:https://blog.csdn.net/haizichetian/article/details/127667482