jimlu

V1

2023/04/13阅读:22主题:默认主题

linux基础入门04

1 history命令

1、不加参数执行history命令

[root@jintest ~]# history 
    1  selinux 0
    2  setenforce 0
    3  systemctl stop firewalld
    4  nmcli con up
    5  nmcli con up ens332
    6  nmcli con up ens32
    7  reboot
    8  ip add
    9  cd Desktop/
   10  ls
...

默认历史命令报错根据1000行,查看方式如下

[root@jintest ~]# echo $HISTSIZE 
1000

2、修改HISTSIZE变量的值可以改变历史命令查看条数

如修改HISTSIZE=10,就只能看到最近执行的10条命令。

[root@jintest ~]# history 
  108  ls -a
  109  history 
  110  history -d 100
  111  history 
  112  history -d 100-112
  113  history -d 100 101
  114  history 
  115  echo $HISTSIZE 
  116  HISTSIZE=10
  117  history 

3、使用-d参数,可删除指定的历史命令

[root@jintest ~]# history 
  110  history -d 100
  111  history 
  112  history -d 100-112
  113  history -d 100 101
  114  history 
  115  echo $HISTSIZE 
  116  HISTSIZE=10
  117  history 
  118  history -d 108 115
  119  history 
[root@jintest ~]# history -d 115    #删除第115条命令
[root@jintest ~]# history 
  111  history 
  112  history -d 100-112
  113  history -d 100 101
  114  history 
  115  HISTSIZE=10
  116  history 
  117  history -d 108 115
  118  history 
  119  history -d 115 116
  120  history 

4、使用-c参数,可删除所有当前会话的历史命令

[root@jintest ~]# history -c 
[root@jintest ~]# history 
  112  history 

5、使用-r参数,将历史命令文件中的历史命令导入到当前终端

[root@jintest ~]# history -r
[root@jintest ~]# history 
  243  echo -h
  244  man echo
  245  echo "hello"
  246  echo -n "hello"
  247  echo -e 'he\nllo'
  248  echo -E 'he\nllo'
  249  cd
  250  ls
  251  vim test.sh
  252  history 

6、histor+数字,显示最近执行的n条命令

[root@jintest ~]# history 
  258  history  -250
  259  history 
  260  cd /root
  261  ls
  262  cd /etc/ssh
  263  ls
  264  cd ..
  265  cd
  266  ls
  267  history 
[root@jintest ~]# history 3
  266  ls
  267  history 
  268  history 3

7、set +o history 隐藏终端输入的历史命令

root@jintest ~]# history -c
[root@jintest ~]# history 
  260  history 
[root@jintest ~]# set +o history
[root@jintest ~]# ls
anaconda-ks.cfg  Documents  dump.rdb              Music     Public     Videos
Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates
[root@jintest ~]# cd /root
[root@jintest ~]# ls
anaconda-ks.cfg  Documents  dump.rdb              Music     Public     Videos
Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates
[root@jintest ~]# cd /home/jin/
[root@jintest jin]# ls
Desktop    Downloads  Pictures  redis-7.0.8         Templates
Documents  Music      Public    redis-7.0.8.tar.gz  Videos
[root@jintest jin]# cd
[root@jintest ~]# history 
  260  history 
  261  set +o history

8、-a参数,将当前终端的历史命令,输入到历史命令文件

[root@jintest ~]# tail .bash_history 
[root@jintest ~]# history 
  262  history 
  263  ls
  264  history 
  265  more .bash_history 
  266            history  -a
  267  tail  .bash_history 
  268  tail .bash_history 
  269  >.bash_history 
  270  tail .bash_history 
  271  history 
[root@jintest ~]# history  -a
[root@jintest ~]# tail .bash_history 
tail  .bash_history 
tail .bash_history 
>.bash_history 
tail .bash_history 
history 
history  -a

9、历史命令快捷操作

  • ctrl+r 快速查找历史命令
  • !num 执行指定序号的历史命令
  • !命令关键词 快速匹配并执行最近执行过的命令
[root@jintest ~]# history 
  271* 
  272  history  -a
  273  tail .bash_history 
  274  history 
  275  ls
  276  pwd
  277  date
  278  id
  279  whoami
  280  history 
[root@jintest ~]# !275
ls
anaconda-ks.cfg  Documents  dump.rdb              Music     Public     Videos
Desktop          Downloads  initial-setup-ks.cfg  Pictures  Templates
[root@jintest ~]# !who
whoami
root
[root@jintest ~]


2 date命令

1、date命令打印系统时间

[root@jintest ~]# date
Thu Apr 13 20:59:46 CST 2023

2、常用格式化输出

参数 用途
%T 显示时间
%F 显示年月日
%Y 年份
%m 月份
%d 日期
%H 小时
%M 分钟
%S 秒数
[root@jintest ~]# date +%T
21:02:18
[root@jintest ~]# date +%F
2023-04-13
[root@jintest ~]# date "+%Y%m%d %H%M%S"
20230413 210307
[root@jintest ~]# date "+%Y-%m-%d %H:%M:%S"
2023-04-13 21:03:20

3、时间戳和时间相互转换

[jin@jintest ~]$ date -d @1681391739 +"%Y-%m-%d %H:%M:%S"
2023-04-13 21:15:39
[jin@jintest ~]$ date -d "2023-04-13 21:15:39" +%s
1681391739

4、时间计算

[jin@jintest ~]$ date -d "+ 30 days"
Sat May 13 21:20:38 CST 2023
[jin@jintest ~]$ date -d "- 30 days"
Tue Mar 14 21:20:48 CST 2023

分类:

后端

标签:

后端

作者介绍

jimlu
V1