[Linux] history - 이전 명령어 실행 방법.

리눅스는 이전에 실행했었던 명령들을 기억합니다. 그리고 이전에 실행했던 명령어들을 확인해 볼수 있고 그 명령어를 다시 실행할 수 있습니다. 이 명령어는 바로 'history'라고 합니다. 이 history라는 명령어에 대해 알아보겠습니다.

 

■ history 명령어

1. history 명령어 사용해보기

[root@hostname /home/user]# history
    1  ls -al
    2  hostnamectl set-hostname hostname
    3  cd /usr/local
    4  reboot
    5  /var/log
    6  cat /etc/passwd
    7  su - username
    8  passwd username
    9  ls -al
   10  ifconfig -a
   11  yum install net-tools
   12  yum update
   13  poweroff
   14  /etc/init.d/mysqld start
   15  mysqladmin -u root password 'password'
   16  vi /etc/my.cnf
   17  pwd
   18  ls -al
   19  cd ~

간단히 프롬프트에 'history'라는 명령어를 입력합니다. 그러면 실행되었던 명령어들이 출력됩니다. 실행했던 명령어들의 갯수는 기본적으로 1000개 입니다.

 

2. 최근 명령어 갯수 제한으로 출력

shell > history 5
  548  ifconfig -a
  549  ls -al
  550  /etc/init.d/mysqld start
  551  ls -al
  552  mysql -uroot -p

최근 실행했던 명령어 5개만 출력합니다. 위의 숫자를 변경하면 그만큼의 이전 명령어를 출력합니다.

 

3. 명령어 검색하기

shell > history | grep -i mysql
   58  cp mysql80-community-release-el7-3.noarch.rpm /root/
   60  yum install mysql80-community-release-el7-3.noarch.rpm 
   61  sudo yum-config-manager --enable mysql57-community
   62  yum-config-manager --enable mysql57-community
   64  sudo yum-config-manager --enable mysql57-community
   65  yum list | grep -i mysql
   66  yum-config-manager --enable mysql57-community
   67  yum list | grep -i mysql
   68  yum-config-manager --disable mysql80-community
   69  yum list | grep -i mysql
   73  cd mysql-8.0.19

수행했던 명령어중에서 필요한 항목들만 추출합니다.

 

4. history 명령어중 특정 행의 명령어 다시 수행.

shell > history | grep -i HISTSIZE
554  echo $HISTSIZE
shell> !554
echo $HISTSIZE
1000

특정 행 번호를 알아낸 후 !숫자를 입력하면 실행했던 명령 그대로 다시 수행합니다.

 

5. 수행했던 명령어를 파일로 출력하기.

shell > history -w CommandHistory.txt
shell > cat CommandHistory.txt
    1  history
    2  cat .bash_history 
    3  exit
    4  cat .bash_history 
    5  cd 123
    6  cd asdf
    7  mkdir 1111
    8  mkdir 2222
    9  cd asdfa
   10  cd 1111/
   11  cd ~
   12  history
   13  cat .bash_history 
   14  exit
   15  cat .bash_history 
   16  cd zxcvzxvc
   17  cd aa

 

 

 

 

■ history 아키텍쳐

1. history에 대해 알아보기

위에서 얘기했듯이 사용자가 입력했던 명령어에 대해 사용 후 버려지는게 아니라 그 명령어들이 저장되게 됩니다. 그리고 그 명령어들은 추후 다시 사용되거나 확인하는 용도로 사용됩니다.

history 명령을 통하여 이전 명령어들이 나온다는 것은 특정 어느 공간에 저장된다는 얘기입니다. 보통 2곳을 이용하게 됩니다.

: 메모리

사용자 명령어를 먼저 메모리 버퍼에 입력를 해두게 됩니다.

: .bash_history

메모리에 저장되었던 명령어 히스토리를 사용자 홈 디렉토리의 .bash_history파일에 저장합니다..

 

2. history 내용 저장 갯수

위에서 history 명령어는 1000개가 기억이 된다고 했습니다. 이에 대한 확인방법은 다음과 같습니다.

shell> echo $HISTSIZE
1000

 

3. hisotry 저장 순서 및 아키텍쳐

명령어를 수행하면 위에서 얘기했듯이 메모리에 명령어가 먼저 저장이 됩니다. 이를 확인해보면 다음과 같습니다.

먼저 임의의 명령어를 여러번 수행합니다.

shell > history
    1  history
    2  cat .bash_history 
    3  exit
    4  cat .bash_history 
    5  cd 123
    6  cd asdf
    7  mkdir 1111
    8  mkdir 2222
    9  cd asdfa
   10  cd 1111/
   11  cd ~
   12  history
shell> cat .bash_history 
history
cat .bash_history 
exit

위에서 .bash_history가 이전에 수행했던 명령어들을 저장한다고 했습니다. 하지만 비교를 해보면 같지가 않습니다. 즉 명령어를 수행한다고 해서 바로 파일에 저장되는것이 아닙니다. 수행되었던 명령어들은 바로 log out(log off)시 저장이 됩니다.

로그아웃 후 다시 접속해보면...

shell> cat .bash_history 
history
cat .bash_history 
exit
cat .bash_history 
cd 123
cd asdf
mkdir 1111
mkdir 2222
cd asdfa
cd 1111/
cd ~
history
cat .bash_history 
exit

보시는바와 같이 이전에 수행했던 명령어들이 .bash_history에 저장되는것을 확인해볼 수 있습니다.

 

4. 메모리상의 수행내역 삭제.

shell > history -c

수행했던 명령어들을 .bash_history상에 저장되지 않게 하려면 메모리에 저장되어 있는 명령어들을 삭제해야 한다. 이렇게 하면 로그아웃을 하더라도 메모리에 명령어들이 남아있지 않기 때문에 .bash_history파일에도 기록이 되지 않는다.

 

■ 터미널 출력 방법

history는 단순히 라인넘버와 명령어만을 출력한다. 그러나 해당명령어가 언제 수행이 되었는지 궁금할수도 있다. 다음과 같은 환경변수를 이용해서 설정한다.

홈 디렉토리의 .bash_profile안에 다음을 입력한다.

export HISTTIMEFORMAT="%F %T "

 

모든 사용자 계정에, 그리고 영구적으로 하고 싶다면 다음의 파일에 입력한다.

shell > vi /etc/profile

맨아래에 다음과 같이 입력한다.

# History Command Execute Time
export HISTTIMEFORMAT="%F %T "

 

출력 형식은 다음과 같다.

shell> history
    1  2020-09-02 23:09:09 history
    2  2020-09-02 23:09:09 cat .bash_history 
    3  2020-09-02 23:09:09 exit
    4  2020-09-02 23:09:09 cat .bash_history 
    5  2020-09-02 23:09:09 cd 123
    6  2020-09-02 23:09:09 cd asdf
    7  2020-09-02 23:09:09 mkdir 1111
    8  2020-09-02 23:09:09 mkdir 2222
    9  2020-09-02 23:09:09 cd asdfa
   10  2020-09-02 23:09:09 cd 1111/
   11  2020-09-02 23:09:09 cd ~
   12  2020-09-02 23:09:09 history
   13  2020-09-02 23:09:09 cat .bash_history 
   14  2020-09-02 23:09:09 exit
   15  2020-09-02 23:09:09 cat .bash_history 
   16  2020-09-02 23:09:09 cd zxcvzxvc
   17  2020-09-02 23:09:09 cd aa
   18  2020-09-02 23:09:09 cd cc
   19  2020-09-02 23:09:09 history -w 1234.txt
   20  2020-09-02 23:09:09 vi 1234.txt 
   21  2020-09-02 23:09:09 ls -al
   22  2020-09-02 23:09:09 exit

 

※도움이 되셨다면 광고클릭 한번 부탁드립니다.※

'Operating System > Linux' 카테고리의 다른 글

[Shell Script] Environment[Shebang-ENV]  (0) 2020.11.29
[Linux] CentOS, RHEL - EPEL  (0) 2020.09.15
[Linux] grep, egrep, fgrep  (0) 2020.05.17
[Linux] 특정 디렉토리의 파일 대소문자 변환  (0) 2020.05.06
[Linux] VNC 설정  (0) 2020.05.02

Designed by JB FACTORY