前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux系统使用基础

Linux系统使用基础

原创
作者头像
鱼丸葱面
修改2020-12-01 14:39:50
1.9K0
修改2020-12-01 14:39:50
举报
文章被收录于专栏:ShadowLinuxerShadowLinuxer

Linux基本使用

交互接口,前面简单介绍了shell的使用,通过shell用户就可以和系统进行交互,也可以称shell为命令解释器,我们通过shell来实现Linux系统操作功能。

代码语言:text
复制
我们可以通过echo $SHELL来看当前所使用的shell
[09:16:55 root@centos7 ~]#echo $SHELL 
/bin/bash 

Linux命令执行过程和命令类型

命令到底是如何执行的呢?我们通过在shell中输入命令就能返显相应的结果,shell提交给系统是如何执行的?

执行命令过程

在用户输入命令回车提交给shell,shell找到命令对应的可执行程序或代码,shell分析后提交给内核分配资源将命令运行起来。

命令分类

  • 内部命令:是shell自带并且通过命令形式提供
  • 外部命令:在文件系统路径下有对应的可执行程序文件

如何来判断内部命令还是外部命令呢?

可以通过type来查看命令的路径,如果显示文件路径则表示该命令为外部命令,如果没有路径则表示为内部命令。

例如:

通过对hostname进行查看后显示了命令的路径,表示hostname为外部命令

[09:23:31 root@centos7 ~]#type hostname

hostname is hashed (/usr/bin/hostname)

通过对echo进行查看后显该命令为shell内置,表示echo为内部命令

代码语言:text
复制
[09:55:43 root@centos7 ~]#type echo 
echo is a shell builtin 

外部命令的存放路径是有规范的,不是任意文件夹可以存放的,我们通过echo $PASH来查看外部命令执行的顺序,在执行外部命令时系统就会通过以下路径顺序依次查找:

代码语言:text
复制
[09:59:21 root@centos7 ~]#echo $PATH
 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 

如果执行了错误的命令则会有报错提示:

[09:59:25 root@centos7 ~]#stand bash: stand: command not found...

外部命令的执行效率如果每次都按照上面的路径顺序查找,执行效率会很低,所以为加速访问,linux系统会对第一次执行的外部命令记录在内存中,此功能为hash,这样再次执行同样命令后会直接进行hash查找提高了访问效率。

可以通过hash来查看命令访问情况:

代码语言:text
复制
[10:05:48 root@centos7 ~]#type hostname 
hostname is hashed (/usr/bin/hostname) 
[10:05:52 root@centos7 ~]#hash 
hits	command    
1	/usr/bin/tty    
5	/usr/bin/hostname    
5	/usr/bin/cat    
1	/usr/bin/ps    
2	/usr/bin/who    
1	/usr/bin/whoami    
2	/usr/bin/ls 

可以通过enable来查看内部命令:

代码语言:text
复制
[03:22:32 root@centos7 ~]#enable 
enable .
enable :
enable [
enable alias
enable bg
enable bind
enable break
enable builtin
enable calle
enable cd
enable command
enable compgen
enable complete
enable compopt
enable continue
enable declare

命令格式和基本命令介绍

COMMAND [OPTIONS...] [ARGUMENTS...]

选项:用于启用或者关闭命令的某个或某些功能

  • 短选项:unix风格选项 -c -l -h 等
代码语言:text
复制
[10:16:41 root@centos7 ~]#ls -a 
.   anaconda-ks.cfg  .bash_logout   .bashrc  .config  .dbus                 .local   .Xauthority ..  
.bash_history    .bash_profile  .cache   .cshrc   initial-setup-ks.cfg  .tcshrc 
代码语言:text
复制
[10:24:07 root@centos7 ~]#df -h 
Filesystem      Size  Used Avail Use% Mounted on 
/dev/sda2       100G  3.6G   97G   4% / 
devtmpfs        895M     0  895M   0% /dev  
  • 长选项:GNU风格选项 --word --all等
代码语言:text
复制
[10:29:09 root@centos7 ~]#free --human               
total        used        free      shared  buff/cache   available 
Mem:           1.8G        234M        1.1G        9.9M        456M        1.3G 
Swap:          4.0G          0B        4.0G 
  • BSD风格选项:
代码语言:text
复制
[10:30:35 root@centos7 ~]#ps a    
PID TTY      STAT   TIME COMMAND  
15449 tty3     Ss+    0:00 -bash  
20519 pts/0    Ss     0:00 -bash 

参数:命令的作用对象,比如文件名、用户名等

代码语言:text
复制
[10:28:41 root@centos7 ~]#id -u mingbo 
1000 

Linux常见命令和字符集

查看硬件信息

查看CPU

代码语言:text
复制
[06:19:32 root@centos7 ~]#lscpu 
Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                4
On-line CPU(s) list:   0-3
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             2
NUMA node(s):          1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 58
Model name:            Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
Stepping:              9
CPU MHz:               2394.568
BogoMIPS:              4789.13
Hypervisor vendor:     VMware
Virtualization type:   full
L1d cache:             32K
L1i cache:             32K
L2 cache:              256K
L3 cache:              6144K
NUMA node0 CPU(s):     0-3
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm fsgsbase tsc_adjust smep arat

cat /pro/cpuinfo也可以查看

代码语言:text
复制
[06:22:39 root@centos7 ~]#cat /proc/cpuinfo 
processor    : 0
vendor_id    : GenuineIntel
cpu family    : 6
model        : 58
model name    : Intel(R) Core(TM) i7-3630QM CPU @ 2.40GHz
stepping    : 9
microcode    : 0x1b
cpu MHz        : 2394.568
cache size    : 6144 KB
physical id    : 0
siblings    : 2
core id        : 0
cpu cores    : 2
apicid        : 0
initial apicid    : 0
fpu        : yes
fpu_exception    : yes
cpuid level    : 13
wp        : yes
flags        : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx rdtscp lm constant_tsc arch_perfmon nopl xtopology tsc_reliable nonstop_tsc eagerfpu pni pclmulqdq ssse3 cx16 pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm fsgsbase tsc_adjust smep arat
bogomips    : 4789.13
clflush size    : 64
cache_alignment    : 64
address sizes    : 43 bits physical, 48 bits virtual
power management:

查看内存

可以通过free命令来查看

代码语言:text
复制
[06:23:05 root@centos7 ~]#free
              total        used        free      shared  buff/cache   available
Mem:        1863252      245440      982344       10168      635468     1385240
Swap:       4194300           0     4194300

还可以通过cat /proc/meminfo

代码语言:text
复制
[06:24:33 root@centos7 ~]#cat /proc/meminfo 
MemTotal:        1863252 kB
MemFree:          982740 kB
MemAvailable:    1385636 kB
Buffers:            2088 kB
Cached:           508388 kB
SwapCached:            0 kB
Active:           313788 kB

查看硬盘和分区情况

通过lsblk来查看硬盘和分区情况

代码语言:text
复制
[06:25:01 root@centos7 ~]#lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0  200G  0 disk 
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0  100G  0 part /
├─sda3   8:3    0   50G  0 part /data
├─sda4   8:4    0    1K  0 part 
└─sda5   8:5    0    4G  0 part [SWAP]
sr0     11:0    1   10G  0 rom  

还可以通过cat /pro/partions来查看

代码语言:text
复制
[06:25:23 root@centos7 ~]#cat /proc/partitions 
major minor  #blocks  name

   8        0  209715200 sda
   8        1    1048576 sda1
   8        2  104857600 sda2
   8        3   52428800 sda3
   8        4          1 sda4
   8        5    4194304 sda5
  11        0   10491904 sr0

查看系统版本信息

查看内核版本信息

通过uname -r来看

代码语言:text
复制
[20:45:00 root@centos7 ~]#uname -r 
3.10.0-957.el7.x86_64 

查看操作系统发行版本

查看当前操作系统版本

代码语言:text
复制
[20:45:02 root@centos7 ~]#cat /etc/redhat-release  
CentOS Linux release 7.6.1810 (Core)   

也可以通过cat /etc/os-release来查看,不同系统版本命令支持情况不一样

代码语言:text
复制
[06:25:47 root@centos7 ~]#cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

日期和时间

Linux的两种时钟分为系统时钟(由Linux内核通过cpu的工作频率进行的)和硬件时钟(主板)

查看系统当前时间

代码语言:text
复制
[20:51:07 root@centos7 ~]#date 
Sun Nov 29 20:51:09 CST 2020 

修改系统时间

通过date也可以修改系统时间,格式为date 月日分年秒,但是这样更改只是内存中修改,系统重启后就不生效会恢复至之前时间

代码语言:text
复制
[20:51:09 root@centos7 ~]#date  112908522020 
Sun Nov 29 08:52:00 CST 2020 

查看当前硬件时钟

通过clok来查看硬件时钟

代码语言:text
复制
[08:52:00 root@centos7 ~]#clock 
Sun 29 Nov 2020 08:56:20 PM CST  -0.981497 seconds 

如果要调整时钟命令为

-s,--hctosys以硬件时钟为准,校正系统时钟,如果硬件时钟准确可以将系统时钟参照修改

-w,--systohc以系统时钟为准,校正硬件时钟,如果系统时钟准确可以将硬件时钟参照修改

查看当前系统的时区

通过ll /etc/localtime来查看系统时区

代码语言:text
复制
[21:39:54 root@centos7 ~]#ll /etc/localtime  
lrwxrwxrwx. 1 root root 35 Nov 26 10:44 /etc/localtime -> ../usr/share/zoneinfo/Asia/Shanghai 

通过timedatectl来更改时区

代码语言:text
复制
[06:28:42 root@centos7 ~]#timedatectl 
list-timezones  set-local-rtc   set-ntp         set-time        set-timezone    status   
代码语言:text
复制
[21:40:49 root@centos7 ~]#timedatectl set-timezone Asia/Shanghai  

查看日历

cal 查看当前月日历

代码语言:text
复制
[06:28:42 root@centos7 ~]#cal
    November 2020   
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30

cal -y来查看一年的日历

代码语言:text
复制
[06:31:04 root@centos7 ~]#cal -y
                               2020                               

       January               February                 March       
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          1  2  3  4                      1    1  2  3  4  5  6  7
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    8  9 10 11 12 13 14
12 13 14 15 16 17 18    9 10 11 12 13 14 15   15 16 17 18 19 20 21
19 20 21 22 23 24 25   16 17 18 19 20 21 22   22 23 24 25 26 27 28
26 27 28 29 30 31      23 24 25 26 27 28 29   29 30 31

        April                   May                   June        
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          1  2  3  4                   1  2       1  2  3  4  5  6
 5  6  7  8  9 10 11    3  4  5  6  7  8  9    7  8  9 10 11 12 13
12 13 14 15 16 17 18   10 11 12 13 14 15 16   14 15 16 17 18 19 20
19 20 21 22 23 24 25   17 18 19 20 21 22 23   21 22 23 24 25 26 27
26 27 28 29 30         24 25 26 27 28 29 30   28 29 30
                       31
        July                  August                September     
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
          1  2  3  4                      1          1  2  3  4  5
 5  6  7  8  9 10 11    2  3  4  5  6  7  8    6  7  8  9 10 11 12
12 13 14 15 16 17 18    9 10 11 12 13 14 15   13 14 15 16 17 18 19
19 20 21 22 23 24 25   16 17 18 19 20 21 22   20 21 22 23 24 25 26
26 27 28 29 30 31      23 24 25 26 27 28 29   27 28 29 30
                       30 31
       October               November               December      
Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa   Su Mo Tu We Th Fr Sa
             1  2  3    1  2  3  4  5  6  7          1  2  3  4  5
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    6  7  8  9 10 11 12
11 12 13 14 15 16 17   15 16 17 18 19 20 21   13 14 15 16 17 18 19
18 19 20 21 22 23 24   22 23 24 25 26 27 28   20 21 22 23 24 25 26
25 26 27 28 29 30 31   29 30                  27 28 29 30 31

关机和重启

关机

halt

poweroff

重启

reboot

-f:强制重启不调用shutdown

-p:切断电源

通过shutdown来实现关机或重启

命令格式为shutdown [OPTION]...[TIME]...[MESSAGE]

代码语言:text
复制
[09:40:14 root@centos7 ~]#shutdown --help
shutdown [OPTIONS...] [TIME] [WALL...]

Shut down the system.

     --help      Show this help
  -H --halt      Halt the machine
  -P --poweroff  Power-off the machine
  -r --reboot    Reboot the machine
  -h             Equivalent to --poweroff, overridden by --halt
  -k             Don't halt/power-off/reboot, just send warnings
     --no-wall   Don't send wall message before halt/power-off/reboot
  -c             Cancel a pending shutdown

TIME无指定默认相当于+1分钟,如果具体时间表示在指定时间执行关机或者重启

代码语言:text
复制
[09:41:10 root@centos7 ~]#shutdown 
Shutdown scheduled for Tue 2020-12-01 09:42:18 CST, use 'shutdown -c' to cancel.
[09:41:18 root@centos7 ~]#
Broadcast message from root@centos7.shadowlinuxer.org (Tue 2020-12-01 09:41:18 CST):

The system is going down for power-off at Tue 2020-12-01 09:42:18 CST!

shutdown -c

Broadcast message from root@centos7.shadowlinuxer.org (Tue 2020-12-01 09:41:23 CST):

The system shutdown has been cancelled at Tue 2020-12-01 09:42:23 CST!

用户登录信息查看

  • whoami查看当前登录有效用户
代码语言:text
复制
[21:50:26 root@centos7 ~]#whoami 
root 
  • who查看系统当前所有的登录会话
代码语言:text
复制
[09:41:23 root@centos7 ~]#who
root     tty3         2020-11-27 03:24
root     pts/0        2020-12-01 09:40 (192.168.3.9)
root     tty1         2020-11-27 08:26
  • w查看系统当前所有的登录会话及所做的操作
代码语言:text
复制
[09:42:04 root@centos7 ~]#w
 09:42:22 up 4 days,  6:20,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty3                      Fri03    4days  0.37s  0.37s -bash
root     pts/0    192.168.3.9      09:40    6.00s  0.11s  0.04s w
root     tty1                      Fri08    2:54   0.65s  0.65s -bash

会话管理

当用户SSH远程登录服务器打开窗口执行命令这就是一个会话,用户此时在执行操作时,如果因为网络或者其他原因导致会话中断,而再次登录后上次执行的命令就没有了,因为会话已经终止。解决这个问题,将会话与窗口解绑,窗口关闭会话不会中断,而继续运行。实现此类功能为终端复用器软件,常见的有screen,tmux

  • Screen

通过在系统中开启screen后,在开启screen的会话中执行命令不会因意外中断而关闭。

还有一种用法类似于远程协助,屏幕共享,登录同一台服务器进行屏幕共享操作

A用户通过开启screen

代码语言:text
复制
[22:29:44 root@centos7 ~]#screen -S support 
B用户通过登录服务器后进行screen会话查看
代码语言:text
复制
[22:29:00 root@centos7 ~]#screen -ls 
There is a screen on: 	56866.support	(Attached) 1 Socket in /var/run/screen/S-root. 

B用户登录该服务器后通过加入前面创建的screen support会话

代码语言:text
复制
[22:30:33 root@centos7 ~]#screen -x support 

此时AB两个用户的所有操作均是实时共享同步的。

同一服务器上开启加入screen会话后两个用户界面同步
同一服务器上开启加入screen会话后两个用户界面同步

如果B用户要退出screen会话界面,需要通过CtrlA+D

  • Tmux

Tmux功能类似screen但是比screen功能更丰富强大

ECHO

echo命令可以将后面跟的字符进行输出

功能主要是显示字符,echo会将输入的字符串送往标准输出。输出的字符串以空白字符隔开,并在最后加上换行号。语法为:

echo [-neE] [字符串]

显示变量

代码语言:text
复制
[22:48:57 root@centos7 ~]#echo $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin 

选项:

-E (默认)不支持\解释功能

-n 不自动换行

代码语言:text
复制
[23:29:45 root@centos7 ~]#echo -n $PATH 
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[23:30:57 root@centos7 ~]# 

-e 启用\字符的解释功能

在启用命令选项-e后,若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出

\a 发出警告声

\b 退格键

\c 最后不加换行符号

\e escape,相当于\033

\n 换行切光标移至行首

\r 回车,光标移至行首,但不换行

\t 插入tab

\ 插入\字符

\0nnn 插入nnn(八进制)所代表的的ASCII字符

\xHH插入HH(十六进制)所代表的ASCII数字

范例:

代码语言:text
复制
[00:35:04 root@centos7 ~]#echo -e 'a\x0Ae' 
a 
e 
[00:35:10 root@centos7 ~]#echo -e '\t a\x0Ae'
 	 a 
 e 

ifconfig

通过该命令可以查看网卡的配置信息,同时也可以修改网卡的相关信息

命令格式为:

ifconfig [-v] [-a] [-s] [interface]

ifconfig [-v] interface [aftype] options | address ...

-a 显示全部接口信息

代码语言:text
复制
[06:40:32 root@centos7 ~]#ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.22  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::2027:fa7b:dedf:7f0e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:97:7e:ed  txqueuelen 1000  (Ethernet)
        RX packets 67247  bytes 6936862 (6.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81212  bytes 143589563 (136.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

-s 显示摘要信息(类似于 netstat -i)

代码语言:text
复制
[06:40:58 root@centos7 ~]#ifconfig -s
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0             1500    67282      0      0 0         81234      0      0      0 BMRU
lo              65536      758      0      0 0           758      0      0      0 LRU
virbr0           1500        0      0      0 0             0      0      0      0 BMU

mtu 设置网卡的最大传输单元 (bytes)

[00:56:34 root@centos7 ~]#ifconfig eth0 mtu 1500

代码语言:text
复制
[06:41:35 root@centos7 ~]#ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.22  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::2027:fa7b:dedf:7f0e  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:97:7e:ed  txqueuelen 1000  (Ethernet)
        RX packets 67308  bytes 6941978 (6.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 81250  bytes 143596983 (136.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

up 启用指定网卡 ifconfig lo up

down 关闭指定网卡 ifconfig lo down

字符集和编码

字符集和编码是两个不同层面的概念

charset是character set的间歇,即字符集,即二进制和字符的对应关系,不关注最终的存储形式。

encoding是charset endcoding的简写,即字符集编码,简称编码,实现如何将字符转化为实际的二进制进行存储或相反,编码决定了空间的使用的大小。

ASCII码

ASCII码是上世纪60年代美国指定的一套字符编码,对英语字符与二进制位之间的关系做了统一规定,也就是American Standard Code for Information Interchange码。

ASCII码一共规定了128个字符的编码,占用一个字节的后面7位,最前面的一位统一规定为0。

Unicode

由于各个国家的文字不一样,每个国家都有自己国家的编码例如中国的GBK,由于世界有上百种语言,每个国家都有自己的编码标准,如果将躲过语言放入同一文本中,显示就会有乱码。为了将语言中字符所有的编码统一,将每一个符号给予一个独一无二的编码数字。Unicode仅仅只是一个字符集,规定了每个字符对应的二进制代码。

命令行扩展和被括起来的集合

命令行扩展:``和$()

``反向单引号

将一个命令的输出打印给另一个命令的参数

代码语言:text
复制
$(COMMAND)或'COMMAND'

单引号里面的内容为普通字符串。

代码语言:text
复制
[00:58:00 root@centos7 ~]#echo '$PASH' 
$PASH 

转义符\,如果输出\\需要两个转义符

代码语言:text
复制
[01:30:45 root@centos7 ~]#echo \\\\ 
\\ 

或者使用单引号输出

代码语言:text
复制
[01:38:39 root@centos7 ~]#echo '\\' 
\\ 

双引号输出:不识别命令,但是识别变量。

代码语言:text
复制
[01:39:35 root@centos7 ~]#echo "This computer name is $HOSTNAME" 
This computer name is centos7.shadowlinuxer.org 

反向单引号中识别变量和命令,执行完内容会交给调用反向单引号的命令继续执行

代码语言:text
复制
[01:45:10 root@centos7 ~]#echo "This computer name is `hostname`" 
This computer name is centos7.shadowlinuxer.org 

而$()与反向单引号是等价的,效果一样

代码语言:text
复制
[02:02:31 root@centos7 ~]#echo "This computer name is $(hostname)" 
This computer name is centos7.shadowlinuxer.org 

显示当前日期

代码语言:text
复制
[02:02:40 root@centos7 ~]#date +%F 
2020-11-30 

可以通过反向单引号 调用创建日期为名称的文档

代码语言:text
复制
[02:05:52 root@centos7 ~]#touch `date +%F`.txt 
[02:08:08 root@centos7 ~]#ls 2020-11-30.txt  anaconda-ks.cfg  initial-setup-ks.cfg 
  • 括号扩展{}

{}可以实现打印重复字符串的简化形式

代码语言:text
复制
[02:08:09 root@centos7 ~]#echo {1..10} 
1 2 3 4 5 6 7 8 9 10 
[02:17:30 root@centos7 ~]#echo {A..K} 
A B C D E F G H I J K 
[02:18:07 root@centos7 ~]#echo King{A,B,C}
KingA KingB KingC 

命令行历史

保存输入命令历史,可以用来重复执行命令,当用户退出时,会将内存中的命令历史存放在对应的历史文件中~/.bash_history

在登录shell时,会读取命令历史文件中记录下的命令~/.bash_history

命令格式

history [-c] [-d offset] [n]

  • -c 清除历史(只是清除内存中的)
  • n 显示最近的n条历史

history -anrw [filename]

history -ps arg [arg..]

查看命令历史

代码语言:text
复制
[02:35:28 root@centos7 ~]#history      
1  ip addr     
2  cd /etc/sysconfig/network-scripts/     
3  ls     
4  vi ifcfg-ens33      
5  service network restart      
6  ip addr 
... 

执行历史命令可以通过!历史编号执行

代码语言:text
复制
[02:40:23 root@centos7 ~]# !3  
ls 
2020-11-30.txt  anaconda-ks.cfg  initial-setup-ks.cfg 

查看记录的最大条数

代码语言:text
复制
[02:45:27 root@centos7 ~]#echo $HISTSIZE 
1000 

如何更改命令历史记录最大条数,通过修改/etc/profile中的HISTSIZE

代码语言:text
复制
[02:45:52 root@centos7 ~]#cat /etc/profile  
HISTSIZE=1000 

命令历史相关变量

  • HISTSIZE:命令历史记录的条数
  • HISTFILE:指定历史文件,默认为~/.bash_history
  • HISTFILESIZE:命令历史文件记录历史条数
  • HISTTIMEFORMAT=“%F%T`whoami`”显示时间和用户

通过此变量可以记录命令历史时间,通过对~.bash_profile中添加HISTTIMEFORMAT=“%F%T`whoami`”进行修改来实现。

代码语言:text
复制
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH
export HISTTIMEFORMAT="%F %T `whoami` "

[03:16:57 root@centos7 ~]#history 
    1  2020-11-29 22:16:23 root ip addr
    2  2020-11-29 22:16:23 root cd /etc/sysconfig/network-scripts/
    3  2020-11-29 22:16:23 root ls
    4  2020-11-29 22:16:23 root vi ifcfg-ens33 
    5  2020-11-29 22:16:23 root service network restart 
    6  2020-11-29 22:16:23 root ip addr
    7  2020-11-29 22:16:23 root cat /etc/sysconfig/network-scripts/
    8  2020-11-29 22:16:23 root cat /etc/sysconfig/network-scripts/ifcfg-ens33 
    9  2020-11-29 22:16:23 root ls
  • HISTIGNORE=“str;str2*...”忽略str1命令,str2开头的命令
  • HISTCONTROL:控制命令历史的记录方式

而上面中用到的export使用来设置环境变量,使用方法为:

export [-fnp][变量名称]=[变量设置值]

  • -f  代表[变量名称]中为函数名称。
  • -n  删除指定的变量。变量实际上并未删除,只是不会输出到后续指令的执行环境中。
  • -p  列出所有的shell赋予程序的环境变量。
代码语言:text
复制
[03:22:25 root@centos7 ~]#export -p
declare -x DISPLAY="localhost:10.0"
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HISTTIMEFORMAT="%F %T root "
declare -x HOME="/root"
declare -x HOSTNAME="centos7.shadowli

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
作者已关闭评论
0 条评论
热度
最新
推荐阅读
目录
  • Linux基本使用
    • Linux命令执行过程和命令类型
      • 执行命令过程
      • 命令分类
  • 命令格式和基本命令介绍
  • Linux常见命令和字符集
    • 查看硬件信息
      • 查看CPU
    • 查看内存
      • 查看硬盘和分区情况
        • 查看系统版本信息
          • 查看内核版本信息
          • 查看操作系统发行版本
        • 日期和时间
          • 查看系统当前时间
          • 修改系统时间
          • 查看当前硬件时钟
          • 查看当前系统的时区
          • 查看日历
        • 关机和重启
          • 关机:
          • 重启
          • 通过shutdown来实现关机或重启
          • 用户登录信息查看
        • 会话管理
          • ECHO
            • 字符集和编码
              • ASCII码
              • Unicode
          • 命令行扩展和被括起来的集合
            • 命令行扩展:``和$()
              • ``反向单引号
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com