Linux基础命令二,bash特性支持命令


Linux基础二


目录
  • Linux基础二
    • Linux基本原则
    • bash特性
    • 基础命令
    • 命令类型

Linux基本原则

由目的单一的小程序组成,组合小程序完成复杂任务;

  1. 一切皆文件;
  2. 配置文件保存为纯文本格式。

bash特性

支持命令历史、命令补全,支持管道、重定向,支持命令别名,支持命令行编辑,支持命令行展开,支持文件名通配,支持变量,支持编程
shell
shell(外壳),广义的shell可以理解为是用户的工作环境,在windows看来桌面就是一个shell,在linux看来终端就是shell
常见的shell有两种,一种是图形界面,即GUI,一种是命令行终端,即CLI。
常用的GUI:Graphic User Interface,Windows,XWindow,Gnome,KDE,Xface
常用的CLI:Command Line Interface,bash,sh,csh,zsh,ksh,tcsh

基础命令

Tab //路径补全

[root@lnh ~]# cd /etc/sys
sysconfig/ sysctl.d/  systemd/   
[root@lnh ~]# cd /etc/sysconfig/
console/         modules/         network-scripts/ 
[root@lnh ~]# cd /etc/sysconfig/network-scripts/
[root@lnh network-scripts]# 
//Tab一下没有补全,就Tab俩下补全路径

alias +定义名字='运行的命令'//命令别名

[root@lnh ~]# cd /etc/sysconfig/network-scripts/
[root@lnh network-scripts]# cd
[root@lnh ~]# alias dx='cd /etc/sysconfig/network-scripts/'
[root@lnh ~]# dx
[root@lnh network-scripts]# 
//复制上面运行的命令到下面的单引号里面,然后在alias 后面随便定义一个名字等于刚刚复制过来的命令,再去执行自己定义的名字就相当于执行了上面那个复制过来的命令

history //查看命令历史
-c //清空保存下来的历史
-d//指定删除哪一条命令(历史)
-w//保存命令历史到/.bash_history中

[root@lnh ~]# history 
    1  shell ls
    2  echo shell
    3  echo $SHELL
    4  cd /etc/sysconfig/network-scripts/
    5  cd
    6  alias dx='cd /etc/sysconfig/network-scripts/'
    7  dx
    8  cd
    9  history 
[root@lnh ~]# history -c
[root@lnh ~]# history 
    1  history 
[root@lnh ~]# pwd
/root
[root@lnh ~]# history -d 1
[root@lnh ~]# history 
    1  pwd
    2  history -d 1
    3  history 
[root@lnh ~]# history -w
[root@lnh ~]# history 
    1  pwd
    2  history -d 1
    3* 
    4  history -w
    5  history 
[root@lnh ~]# history -c
[root@lnh ~]# history 
    1  history 
//-d删除指定的命令后它后面的命令会前进它的命令顺序上面,后面的命令依次排序

命令历史的使用技巧
!n //执行历史中的第几条命令
!-n//执行历史中的倒数第几条命令
!! // 执行上一条命令
!+历史最近命令//执行次命令
!$//引用前一个命令的最后一个参数
Esc+. //按下esc后按.引用上一条命令

[root@lnh ~]# !1
history 
    1  history 
[root@lnh ~]# !-1
history 
    1  history 
[root@lnh ~]# !!
history 
    1  history 
[root@lnh ~]# !history
history 
    1  history 
[root@lnh ~]# !$
history
    1  history 
    2  history
[root@lnh ~]# history
    1  history 
    2  history

命令替换

[root@lnh ~]# date 
Wed Jun 29 23:50:25 CST 2022
[root@lnh ~]# echo $(date +%Y%m%d)
20220629
[root@lnh ~]# echo `date +%Y%m%d`
20220629
不建议用反引号(`),一般都是用小括号

PATH//命令搜索路径
HISTSIZE //可以查看命令历史的存储空间
RANDOM //保存着0-32768之间的随机数
SHELL //当前正在用的shell

[root@lnh ~]# echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@lnh ~]# echo $HISTSIZE 
1000
[root@lnh ~]# echo $RANDOM 
22103
[root@lnh ~]# echo $RANDOM 
3357
[root@lnh ~]# echo $RANDOM 
10760
[root@lnh ~]# echo $SHELL
/bin/bash //当前shell
[root@lnh ~]# cat /etc/shells 
/bin/sh
/bin/bash
/usr/bin/sh
/usr/bin/bash
//系统可以使用的shell

*,?,[],^,-的用法

[root@lnh ~]# cd xbz/
[root@lnh xbz]# touch {1..100}
[root@lnh xbz]# ls
1    14  2    24  3   35  40  46  51  57  62  68  73  79  84  9   95  bb
10   15  20   25  30  36  41  47  52  58  63  69  74  8   85  90  96
100  16  21   26  31  37  42  48  53  59  64  7   75  80  86  91  97
11   17  22   27  32  38  43  49  54  6   65  70  76  81  87  92  98
12   18  222  28  33  39  44  5   55  60  66  71  77  82  88  93  99
13   19  23   29  34  4   45  50  56  61  67  72  78  83  89  94  b
[root@lnh xbz]# ls 1*
1  10  100  11  12  13  14  15  16  17  18  19
[root@lnh xbz]# ls 2*
2  20  21  22  23  24  25  26  27  28  29
222:
a
//*表示列出了任意字符的长度(此处表示所有字符)
[root@lnh xbz]# ls ?
1  2  3  4  5  6  7  8  9

b:
[root@lnh xbz]# ls ??
10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95
11  16  21  26  31  36  41  46  51  56  61  66  71  76  81  86  91  96
12  17  22  27  32  37  42  47  52  57  62  67  72  77  82  87  92  97
13  18  23  28  33  38  43  48  53  58  63  68  73  78  83  88  93  98
14  19  24  29  34  39  44  49  54  59  64  69  74  79  84  89  94  99

bb:
[root@lnh xbz]# ls ???
100

222:
a
//?表示匹配任意单个字符(此时表示一个?为单个字符,两个?表示两个字符,三个?表示三个字符)
[root@lnh xbz]# ls [23-33]
2  3
[root@lnh xbz]# ls [23-89]
2  3  4  5  6  7  8  9
[root@lnh xbz]# ls [23-33][23-33]
22  23  32  33
[root@lnh xbz]# ls [23-89][23-89]
22  26  32  36  42  46  52  56  62  66  72  76  82  86  92  96
23  27  33  37  43  47  53  57  63  67  73  77  83  87  93  97
24  28  34  38  44  48  54  58  64  68  74  78  84  88  94  98
25  29  35  39  45  49  55  59  65  69  75  79  85  89  95  99
//[]表示范围内的单个字符,例如此处[23-33]里面只有两个字符2,3,[23-89]里面有2,3,4,5,6,7,8,9,[23-89][23-89]表示左边[]里面的字符与右边[]字符进行俩俩组合,用左边的与右边的进行组合
[root@lnh xbz]# ls [^23-89]
1

b:
[root@lnh xbz]# ls [^23-79]
1  8

b:
[root@lnh xbz]# ls [^23-71]
8  9

b:
[root@lnh xbz]# ls [^53-71]
2  8  9

b:
[root@lnh xbz]# ls [^53-49]
1  2  6  7  8

b:
[root@lnh xbz]# ls [^33]
1  2  4  5  6  7  8  9

b:

[root@lnh xbz]# ls [53-49^]
3  4  5  9
[root@lnh xbz]# ls [22-33^]
2  3
//^在前面表示除了括号里面的字符的其他所有字符,在后面的话没有表示什么意义或者说只是表示次括号里面的字符
[root@lnh xbz]# ls [22-33]
2  3
[root@lnh xbz]# ls [53-49]
3  4  5  9
[root@lnh xbz]# ls [2-44]
2  3  4
[root@lnh xbz]# ls [-100]
1
[root@lnh xbz]# ls [-59]
5  9
[root@lnh xbz]# ls [33-]
3
//-在前面表示就两个字符的单个的字符,这中间表示前一个字符到后一个范围内的字符,在后面也是和在前面效果一样

命令类型

type//内部命令(shell内置)外部命令:在文件系统的某个路径下有一个与命令名称相应的可执行文件

[root@lnh ~]# type pwd
pwd is a shell builtin //内部
[root@lnh ~]# type cd 
cd is a shell builtin  //内部
[root@lnh ~]# type ls
ls is aliased to `ls --color=auto' //外部
[root@lnh ~]# type /usr/bin/ls
/usr/bin/ls is /usr/bin/ls         //外部
圣人(内)生而知之,就是一直都存在的
凡人(外)学而知之,通过后来学习来的

相关内容