Mời bạn tham khảo ngay 15 thao tác với lệnh History trên Linux để làm chủ câu lệnh đó ngay cả khi bạn không sử dụng lệnh này thường xuyên.
[user@centos7 ~]$ history
1 ip a
2 exit
3 ls -la
4 pwd
[user@centos7 ~]#
[user@centos7 ~]$ history 3 16 passwd 17 getenforce 18 history 3
[root@centos7 ~]# date Wed Feb 27 01:55:48 UTC 2019 [root@centos7 ~]# !! date Wed Feb 27 01:56:03 UTC 2019 [root@centos7 ~]#
[root@centos7 ~]# history 2 101 date 102 history 2 [root@centos7 ~]# !101 date Wed Feb 27 01:55:48 UTC 2019
[root@centos7 ~]# systemctl start httpd [root@centos7 ~]# systemctl stop firewalld [root@centos7 ~]# systemctl restart httpd [root@centos7 ~]# !systemctl systemctl restart httpd
[root@centos7 ~]# history | grep httpd 65 yum install httpd -y 106 systemctl stop httpd 107 systemctl start httpd 117 history | grep httpd
[root@centos7 ~]# history -w
[root@centos7 ~]# history -c
[root@centos7 ~]# history | grep password 121 Wed 27 Feb 2019 01:56:03 AM PDT mysql -u root -p oops_this_is_my_password 122 Wed 27 Feb 2019 01:56:03 AM PDT history | grep password [root@centos7 ~]# history -d 121 [root@centos7 ~]# history | grep password 121 Wed 27 Feb 2019 01:56:03 AM PDT history | grep password 123 Wed 27 Feb 2019 01:56:03 AM PDT history | grep password
[root@centos7 ~]# echo "secret command";history -d $(history 1) secret command
HISTSIZE=2000 HISTFILESIZE=2000Sau đó logout và login lại là được. Kiểm tra xem kích thước hiện tại như sau:
[root@centos7 ~]# echo $HISTFILESIZE 2000 [root@centos7 ~]# echo $HISTSIZE 2000
echo 'export HISTTIMEFORMAT="%c "' >> ~/.bashrcSau đó logout và login trở lại và kiểm tra
[root@centos7 ~]# history 5 39 Wed 27 Feb 2019 01:46:03 AM PDT firewall-cmd --add-service=http --permanent 40 Wed 27 Feb 2019 01:48:03 AM PDT firewall-cmd --reload 41 Wed 27 Feb 2019 01:50:03 AM PDT tailf /var/log/messages 42 Wed 27 Feb 2019 01:52:03 AM PDT restorecon -v /var/www/html/index.html 43 Wed 27 Feb 2019 01:56:03 AM PDT history 5
(reverse-i-search)`cat': cat /etc/*release root@ITFROMZERO:~# cat /etc/*release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=18.04 DISTRIB_CODENAME=bionic DISTRIB_DESCRIPTION="Ubuntu 18.04.1 LTS" NAME="Ubuntu" VERSION="18.04.1 LTS (Bionic Beaver)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 18.04.1 LTS" VERSION_ID="18.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=bionic UBUNTU_CODENAME=bionic
root@ITFROMZERO:~# cd /home/ root@ITFROMZERO:/home# cd /root/ root@ITFROMZERO:~# export HISTCONTROL=ignorespace root@ITFROMZERO:~# ls -l total 0 root@ITFROMZERO:~# history 5 140 Wed 27 Feb 2019 01:56:03 clear 141 Wed 27 Feb 2019 01:56:03 cd /home/ 142 Wed 27 Feb 2019 01:56:03 cd /root/ 143 Wed 27 Feb 2019 01:56:03 export HISTCONTROL=ignorespace 144 Wed 27 Feb 2019 01:56:03 history 5 root@ITFROMZERO:~#Ở ví dụ trên mình đã không cho lưu lệnh “ls -l” bằng cách bỏ qua lưu lệnh đó với khoảng cách phía trước câu lệnh (export HISTCONTROL=ignorespace)
root@ITFROMZERO:~# cp test.txt /home/test.csv root@ITFROMZERO:~# ls -l !cp:2 ls -l /home/test.csv -rw-r–r– 1 root root 292 2019-02-26 15:02 /home/test.csv Hy vọng với những thao tác làm chủ lệnh History trên Linux sẽ giúp bạn sử dụng lệnh này một cách đơn giản và dễ dàng.
Tips: Tham gia Channel Telegram KDATA để không bỏ sót khuyến mãi hot nào