Linux命令

1、echo命令

  1. 是内部命令,功能是向屏幕输出
  2. echo字符串,字符串可以没有引号,也可以有(单,双)引号。
    1. [root@localhost ~]# echo abc
      abc
      [root@localhost ~]# echo -n abc
      abc[root@localhost ~]# echo $123
      23
      [root@localhost ~]# echo $'123'
      123
      [root@localhost ~]# echo "abc\\nabc"
      abc\nabc
      [root@localhost ~]# echo -e "abc\nabc"
      abc
      abc

       

  3. echo的选项 -n取消换⾏
    1. -e 与引号⼀起使⽤,可以让\n\t\起作⽤
  4. echo 输出变量名 : echo $变量名

2、printf命令

  1. 是shell内部命令
  2. 带格式化的输出,不会⾃动换⾏,需要⾃⼰加\n且引号引起来,⾃动识别\t..
  3. [root@localhost ~]# printf 'abc'
    abc[root@localhost ~]# printf 'abc\n'
    abc

     

  4. 可以使⽤占位符%[-][n]s , 其中n表⽰字符串输出的宽度, -表⽰左对齐, 当s的实际长度⼩n时有效
    1. [root@localhost ~]# printf '%5s\n' abc
        abc
      [root@localhost ~]# printf '%-5s\n' abc
      abc  
      [root@localhost ~]# printf '%-5s\n' abcdefghijk
      abcdefghijk

       

  5. 可以使⽤占位符%[-|0][n]d , 其中n表示输出的宽度, -表示左对齐, 0表⽰当宽度不够时的左填充字符且不能与-同时使⽤,当d的实际长度⼩n时有效。
    1. [root@localhost ~]# printf '%8d\n' abc
      -bash: printf: abc: 无效数字
             0
      [root@localhost ~]# printf '%8d\n' 123
           123
      [root@localhost ~]# printf '%8d%5d\n' 123 123
           123  123
      [root@localhost ~]# printf '%d%d\n' 123 123
      123123
      [root@localhost ~]# printf '%08d%5d\n' 123 123
      00000123  123
      [root@localhost ~]# printf '%-8d%5d\n' 123 123
      123       123
      [root@localhost ~]#
  6. 可以使⽤占位符%[-|0][n[.m]]f , 其中n表示输出的宽度, -表示左对齐, 0表示当宽度不够时的左填充字符且不能与-同时使⽤,m表示输出⼩数位数会四会五⼊。
    1. [root@localhost ~]# printf '%8f\n' 123.123
      123.123000
      [root@localhost ~]# printf '%15f\n' 123.123
           123.123000
      [root@localhost ~]# printf '%015f\n' 123.123
      00000123.123000
      [root@localhost ~]# printf '%015.2f\n' 123.123
      000000000123.12
      [root@localhost ~]# printf '%15.2f\n' 123.123
               123.12
      [root@localhost ~]# printf '%-15.2f\n' 123.123
      123.12         
      [root@localhost ~]# printf '%-15.2f\n' 123.129
      123.13         
      [root@localhost ~]#

       

3、printenv 命令

  1. 输出系统环境变量,环境变量中都是变量名=变量值,变量名都是⼤写,同⼀⾏声明多个变量时⽤冒号分隔
  2. [root@localhost ~]# printenv
    XDG_SESSION_ID=1
    HOSTNAME=localhost.localdomain
    SELINUX_ROLE_REQUESTED=
    TERM=xterm
    SHELL=/bin/bash
    HISTSIZE=1000
    SSH_CLIENT=192.168.18.1 64862 22
    SELINUX_USE_CURRENT_RANGE=
    SSH_TTY=/dev/pts/0
    USER=root

4、date命令

  1. 输出当前的⽇期时间 date
  2. 修改⽇期时间 date -s "yyyy-MM-dd HH:mm:ss"
    1. [root@localhost ~]# date -s "2023-11-10"
      2023年 11月 10日 星期五 00:00:00 CST
  3. 同步标准⽇期时间
    1. 安装同步时间⼯具: yum install -y ntpdate
    2. 执⾏同步时间 命令: ntpdate -u ntp.aliyun.com
      1. [root@localhost ~]# ntpdate -u ntp.aliyun.com
        11 Nov 10:46:26 ntpdate[1641]: step time server 203.107.6.88 offset 125172.071642 sec
        [root@localhost ~]# date
        2023年 11月 11日 星期六 10:46:31 CST

         

  4. 格式化显⽰时间 + %Y年 %m⽉ %d⽇ %H时 %M分 %S秒%u周
    1. [root@localhost ~]# date +"%Y"
      2023
      [root@localhost ~]# date +"%m"
      11
      [root@localhost ~]# date +"%d"
      11
      [root@localhost ~]# date +"%H"
      10
      [root@localhost ~]# date +"%H:%M:%S"
      10:48:28
      [root@localhost ~]# date -T
      date:无效选项 -- T
      Try 'date --help' for more information.
      [root@localhost ~]# date +"%T"
      10:49:12

       

  5. 计算
    1. 以当前⽇期为基准 date -d "n day|year|month|hour|minute|second"
    2. [root@localhost ~]# date -d "-1999year"
      0024年 11月 11日 星期一 10:51:10 LMT
      [root@localhost ~]# date -d "1year"
      2024年 11月 11日 星期一 10:51:36 CST
      [root@localhost ~]# date -d "1day"
      2023年 11月 12日 星期日 10:51:44 CST
      [root@localhost ~]# date -d "1day ago"
      2023年 11月 10日 星期五 10:52:04 CST
      [root@localhost ~]# date -d "1 day ago"
      2023年 11月 10日 星期五 10:52:12 CST
      [root@localhost ~]# date -d "1 minute ago"
      2023年 11月 11日 星期六 10:51:31 CST
      [root@localhost ~]# date -d "1 minute"
      2023年 11月 11日 星期六 10:53:43 CST
      [root@localhost ~]#

5、管道符 | , 命令A | 命令B 即命令A返回的结果作为命令B的输⼊

6、grep命令

  1. 可以字符串,⽂件,⽬录中搜索指定的内容的⼯具
  2. 即可以单独使⽤也可以与其他命令通过管道符配合使⽤
    1. [root@hadoop101 ~]# printenv | grep JAVA_HOME
      JAVA_HOME=/opt/install/jdk
  3. 常⽤的选项
    1.  -i 不区分⼤⼩写
      1. [root@hadoop101 ~]# printenv | grep -i java_home
        JAVA_HOME=/opt/install/jdk
    2.  -w 完整单词
    3. -l 仅⽂件名(不显示查到⾏的内容,只显⽰⽂件名,哪个⽂件中有love单词) -r递归(查⽬录)
      1. 查询当前⽬录下⽂件中内容有love的⽂件名
    4. -n ⾏号
      1. [root@hadoop101 ~]# grep -n love ./test/a.txt
        3:I love you!
        4:Do you love me?
        5:Who are you love?
    5. -v反向 查询a.txt⽂件中,不带love的⾏并显示行号
      1. [root@hadoop101 ~]# grep -nv love ./test/a.txt
        1:sdfasdf
        2:a
        6:dsfasdfZZ
    6. -o 仅显示匹配部分
      1. [root@hadoop101 ~]# grep -no love ./test/a.txt
        3:love
        4:love
        5:love
    7.  -P | -E 正则
      1. [root@hadoop101 ~]# echo "1234asd1234" | grep -P [0-9]
        1234asd1234
        [root@hadoop101 ~]# vim a.txt
        [root@hadoop101 ~]# grep -P [0-9] a.txt
        12341234
        1234

7、find 搜索指定⽂件: find 起始⽬录 [选项]

  1. -name ⽂件名
    1. [root@hadoop101 ~]# find . -name a.txt./a.txt./test/a.txt
      [root@hadoop101 ~]# find / -name a.txt
      /root/a.txt
      /root/test/a.txt
      /home/zhangsan/a.txt
      /home/u1/a.txt
  2. -user 所有者
    1. [root@hadoop101 ~]# find / -user root -name a.txt
      /root/a.txt
      /root/test/a.txt
  3. -type ⽂件类型
    f 普通⽂件
    d ⽬录⽂件
    l 符号链接⽂件
    1. [root@hadoop101 ~]# find . -type d
      .
      ./test
      ./javacode
      ./.ssh
      ./d101
      [root@hadoop101 ~]# find . -type f
      ./.bash_logout
      ./.bash_profile
      ./.bashrc
      ./.cshrc
      ./.tcshrc
      ./.bash_history
      ./b.txt
      ./d.txt

       

  4. -size ⽂件⼤小字节c -93c查找⼩于93字节的⽂件 +93c查找⼤于93字节的⽂件。
    1. [root@hadoop101 ~]# find . -size 93c
      ./a.txt
      ./aa.txt
      [root@hadoop101 ~]# find . -size -93c
      ./.bash_logout
      ./b.txt
      ./d.txt
  5.  -[a|c|m]time 时间 修改时间
    1. 根据时间戳查找:
      以天为单位(time):访问时间
      -atime [+|-]#
      +: 表示(#+1)天之外被访问过;
      -: 表示#天之内被访问过;
      ⽆符号:表示短于(#+1)> x >=#天
      的时间段被访问过;
      -mtime:修改时间
      -ctime:创建时间
      以分钟为单位(min):
      -amin [+|-]#:访问时间
      -mmin:修改时间
      -cmin:创建时间
      查询10分钟之内被修改的⽂件
      [root@hadoop101 ~]# find . -mmin -10
      .
      ./a.txt
      ./.viminfo
  6.  -maxdepth n 表⽰搜索深度当前默认为1
    1. [root@hadoop101 ~]# mkdir -p a1/a2/a3/a4/a5
      [root@hadoop101 ~]# touch a1/a2/a3/a4/a5/a51.txt
      [root@hadoop101 ~]# find . -maxdepth 6 -name a51.txt
      ./a1/a2/a3/a4/a5/a51.txt
      [root@hadoop101 ~]# find . -maxdepth 6 -name "a*.txt"
      ./a.txt
      ./test/a.txt
      ./a101.txt
      ./aa.txt
      ./a1/a2/a3/a4/a5/a51.txt
  7. find 命令中通过 -name 查找⽂件时可以使⽤通配符 ?任意⼀个*任意个任意字符 且必须⽤引号
    1. [root@hadoop101 ~]# find . -name "a?.txt"
      ./aa.txt
      [root@hadoop101 ~]# find . -name "a*.txt"
      ./a.txt
      ./test/a.txt
      ./a101.txt
      ./aa.txt
      ./a1/a2/a3/a4/a5/a51.txt

       

  8. xagrs 命令作⽤是把上⼀个命令的输出字符串化,或重新获取,通常与管道符配合使⽤,此功能也可以⽤⼀对反引号代替
    1. 查询⽂件名为a.txt的⽂件中包含love的⾏
      [root@hadoop101 ~]# find . -name 'a.txt' | xarg
      s grep -i love
      ./a.txt:I love you!
      ./a.txt:Do you love_you me?
      ./a.txt:Who are you love_aaa?
      ./test/a.txt:I love you!
      ./test/a.txt:Do you love me?
      ./test/a.txt:Who are you love?
      [root@hadoop101 ~]# grep -i love `find . -name
      'a.txt'`
      ./a.txt:I love you!
      ./a.txt:Do you love_you me?
      ./a.txt:Who are you love_aaa?
      ./test/a.txt:I love you!
      ./test/a.txt:Do you love me?
      ./test/a.txt:Who are you love?
  9. locate 功能 根据部分⽂件名搜索⽂件
    1. 安装 yum install -y mlocate
    2. 更新 updatedb
    3. [root@hadoop101 ~]# touch aaaa.txt
      [root@hadoop101 ~]# locate a.txt
      /home/u1/a.txt
      /home/zhangsan/a.txt
      /root/a.txt
      /root/aa.txt
      /root/test/a.txt
      /usr/share/doc/vim-common-7.4.629/README_extra.
      txt
      /usr/share/gnupg/help.ca.txt
      [root@hadoop101 ~]# updatedb
      [root@hadoop101 ~]# locate a.txt
      /home/u1/a.txt
      /home/zhangsan/a.txt
      /root/a.txt
      /root/aa.txt
      /root/aaaa.txt
      /root/test/a.txt
      /usr/share/doc/vim-common-7.4.629/README_extra.
      txt
      /usr/share/gnupg/help.ca.txt
  10. which 找到指定命令的对应⽂件
    1. [root@hadoop101 ~]# which pwd
      /usr/bin/pwd
      [root@hadoop101 ~]# which java
      /opt/install/jdk/bin/java
      [root@hadoop101 ~]#
  11. whereis 命令功能是找到指定的命令的所有 相关的对应⽂件
    1. [root@hadoop101 ~]# whereis pwd
      pwd: /usr/bin/pwd /usr/share/man/man1/pwd.1.gz
      [root@hadoop101 ~]# whereis java
      java: /opt/install/jdk1.8.0_351/bin/java
      [root@hadoop101 ~]# whereis mysql
      mysql: /usr/lib64/mysql /usr/share/mysql
  12. 标准的输⼊和输出流
    1. stdin, 0 标准的输⼊设备 键盘
    2. stdout, 1 标准的正常的输出设备 屏幕
    3. stderr, 2 标准的错误输出设备 屏幕
  13. 标准的输⼊输出流的使⽤
    1. > : ll > test.txt 把原本输出到屏幕的正确数据覆盖到指定⽂件
      [root@hadoop101 ~]# ll >a11.txt
      [root@hadoop101 ~]# cat a11.txt
      总⽤量 8000
      drwxr-xr-x. 3 root root 16 11⽉ 11 14:41 a1
      -rw-r--r--. 1 root root 10 11⽉ 9 16:42 a101.txt
      -rw-r--r--. 1 root root 0 11⽉ 11 15:30 a11.txt
      -rw-r--r--. 1 root root 0 11⽉ 11 15:21 aaaa.txt
      -rw-r--r--. 1 root root 93 11⽉ 11 12:25 aa.txt
      -rwxrw-r--. 1 root root 103 11⽉ 11 14:31 a.txt
      -rw-r-----. 1 root root 74 11⽉ 11 11:16 b.txt
      -rwxr--r--. 1 root root 7765430 11⽉ 11 11:19 c.txt
      drwxr-xr-x. 2 root root 6 11⽉ 9 16:48 d101
      -rwx------. 1 root root 11 11⽉ 4 19 d.txt
      drwxr-xr-x. 2 root root 53 11⽉ 9 39 javacode
      -rw-r--r--. 1 root root 10 11⽉ 9 16:42 root@hadoop102
      drwxr-xrwx. 2 zhangsan zhangsan 73 11⽉ 9 10:52 test
      -rw-r--r--. 1 root root 395931 11⽉ 9 10:44 txt1.tar.gz
      -rw-r--r--. 1 root root 291 11⽉ 9 10:43 txt.tar.gz
      
      < : cat < file1 从指定⽂件读取数据
      [root@hadoop101 ~]# cat <a11.txt
      总⽤量 8000
      drwxr-xr-x. 3 root root 16 11⽉ 11 14:41 a1
      -rw-r--r--. 1 root root 10 11⽉ 9 16:42 a101.txt
      -rw-r--r--. 1 root root 0 11⽉ 11 15:30 a11.txt
      -rw-r--r--. 1 root root 0 11⽉ 11 15:21 aaaa.txt
      -rw-r--r--. 1 root root 93 11⽉ 11 12:25 aa.txt
      -rwxrw-r--. 1 root root 103 11⽉ 11 14:31 a.txt
      -rw-r-----. 1 root root 74 11⽉ 11 11:16 b.txt
      -rwxr--r--. 1 root root 7765430 11⽉ 11 11:19 c.txt
      drwxr-xr-x. 2 root root 6 11⽉ 9 16:48 d101
      -rwx------. 1 root root 11 11⽉ 4 14:19 d.txt
      drwxr-xr-x. 2 root root 53 11⽉ 9 14:39 javacode
      -rw-r--r--. 1 root root 10 11⽉ 9 16:42 root@hadoop102
      drwxr-xrwx. 2 zhangsan zhangsan 73 11⽉ 9 10:52 test
      
      <<, 例如 cat << eof 从键盘读取并以eof作为结束符
      [root@hadoop101 ~]# cat <<test
      > asdf
      > test1
      > test2
      > test
      > test
      asdf
      test1
      test2
      test
      
      >> 例如 ll >> test.txt 把原本输出到屏幕的正确数据追加到指定⽂件
      [root@hadoop101 ~]# ll test>>a11.txt
      [root@hadoop101 ~]# cat a11.txt
      
      2> 例如 ll test2 2> error 把原本输出到屏幕的错误信息覆盖到指定⽂件
      [root@hadoop101 ~]# ll aaa
      ls: ⽆法访问aaa: 没有那个⽂件或⽬录
      [root@hadoop101 ~]# ll aaa 2>a11.txt
      [root@hadoop101 ~]# cat a11.txt
      ls: ⽆法访问aaa: 没有那个⽂件或⽬录
      
      2>> 例如 ll test2 2>> error 把原本输出到屏幕的错误信息追加到指定⽂件
      [root@hadoop101 ~]# ll bbb 2>>a11.txt
      [root@hadoop101 ~]# cat a11.txt
      ls: ⽆法访问aaa: 没有那个⽂件或⽬录
      ls: ⽆法访问bbb: 没有那个⽂件或⽬录
      
      
      [root@hadoop101 ~]# ll aaa 1>test.txt 2>test2.txt
      [root@hadoop101 ~]# cat test.txt
      [root@hadoop101 ~]# cat test2.txt
      ls: ⽆法访问aaa: 没有那个⽂件或⽬录
      [root@hadoop101 ~]# ll . 1>test.txt 2>test2.txt
      [root@hadoop101 ~]# cat test.txt
      总⽤量 8004
      drwxr-xr-x. 3 root root 16 11⽉ 11 14:41 a1
      -rw-r--r--. 1 root root 10 11⽉ 9 16:42 a101.txt
      -rw-r--r--. 1 root root 98 11⽉ 11 15:40 a11.txt
      -rw-r--r--. 1 root root 0 11⽉ 11 15:31 a12.txt
      
      
      效果相同,正常的和错误的都放到test.txt⽂件中
      [root@hadoop101 ~]# ll . 1>test.txt 2>test.txt
      [root@hadoop101 ~]# ll . 1>test.txt 2>&1

       

  14. ⽂件统计wc -m字符数 -w单词数 -l⾏数 L 最长⾏的字符数指定⽂件或键盘输⼊
    1. [root@hadoop101 ~]# cat a12.txt
      abc
      abcabc
      abcabcabc
      [root@hadoop101 ~]# wc a12.txt
      3 3 21 a12.txt
      [root@hadoop101 ~]# wc -m a12.txt
      21 a12.txt
      [root@hadoop101 ~]# wc -w a12.txt
      3 a12.txt
      [root@hadoop101 ~]# wc -l a12.txt
      3 a12.txt
      [root@hadoop101 ~]# wc -L a12.txt
      9 a12.txt
  15. ⽂本切割 cut -d 分割符,默认为制表符⽆需指定 -f 字段序号从1开始,逗号,中划线 -c字符序号,单独使⽤
    1. [root@hadoop101 ~]# date
      2023年 11⽉ 11⽇ 星期六 16:09:38 CST
      [root@hadoop101 ~]# date | cut -d " " -f2,3,6
      11⽉ 11⽇ CST
      [root@hadoop101 ~]# date | cut -d " " -f1-3
      2023年 11⽉ 11⽇
      [root@hadoop101 ~]# date | cut -d " " -f1,2,3
      2023年 11⽉ 11⽇
      [root@hadoop101 ~]# vim a.txt
      [root@hadoop101 ~]# cut -f2 a.txt
      bbb
      bbb
      bbb
      [root@hadoop101 ~]# cat a.txt
      aaa bbb ccc
      aaa bbb ccc
      aaa bbb ccc
      [root@hadoop101 ~]# cut -f2-3 a.txt
      bbb ccc
      bbb ccc
      bbb ccc
      [root@hadoop101 ~]# cut -f2,3 a.txt
      bbb ccc
      bbb ccc
      bbb ccc
  16. tr命令 ⽂本替换命令 tr '原字符列表' '新字符列表'
    1. [root@hadoop101 ~]# echo 'abcdabcd'
      abcdabcd
      [root@hadoop101 ~]# echo 'abcdabcd' | tr 'a' 'm'
      mbcdmbcd
      [root@hadoop101 ~]# echo 'abcdabcd' | tr 'a-z' 'A-Z'
      ABCDABCD
      [root@hadoop101 ~]# echo 'abcdabcd' | tr 'a-z' '1-9'
      12341234
      [root@hadoop101 ~]# echo 'abcdabcd' | tr 'abc' '12'
      122d122d
      [root@hadoop101 ~]# echo 'abcdabcd' | tr 'ab' '123'
      12cd12cd
    2. 选项 -d 删除
      1. [root@hadoop101 ~]# echo 'abcdabcd' | tr -d 'ab'
        cdcd
    3.  -s 去重
      1. [root@hadoop101 ~]# echo 'aaaaabbbbbbcefa' | tr -s 'ab'
        abcefa

         

posted @ 2023-11-13 22:15  韩世康  阅读(8)  评论(0编辑  收藏  举报