Computer >> 컴퓨터 >  >> 프로그래밍 >> Python

Ubuntu에서 명령줄 치트 시트(Cheat) 설치하고 활용하는 완벽 가이드

Cheat는 파이썬(Python) 기반의 명령줄 도구로, 시스템 관리자가 유용한 치트 시트(cheat sheet)를 조회하고 저장할 수 있게 해줍니다. 지정한 명령어의 일반 텍스트 예제를 불러와 옵션, 인자, 자주 쓰이는 용법을 빠르게 상기시켜 주는 역할을 합니다. Cheat는 "자주 사용하지만 자주 쓰는 만큼은 외우지 못하는 명령어"를 위해 만들어진 도구입니다.

Cheat 설치하기

Cheat를 설치하기 전에 먼저 시스템이 최신 상태인지 확인해야 합니다. 아래 명령어를 실행하세요.

$ sudo apt-get update && sudo apt-get upgrade

Cheat 설치는 파이썬 패키지 관리자인 Pip을 이용하는 것이 가장 좋습니다. 먼저 pip를 설치하려면 다음 명령어를 사용합니다.

$ sudo apt-get install python-pip

이제 cheat를 설치합니다.

$ sudo pip install cheat

정상적으로 실행되면 아래와 같은 출력 결과를 확인할 수 있습니다.

Collecting cheat
    Downloading cheat-2.1.24.tar.gz (42kB)
       100% |████████████████████████████████| 51kB 89kB/s
Collecting docopt>=0.6.1 (from cheat)
    Downloading docopt-0.6.2.tar.gz
Collecting pygments>=1.6.0 (from cheat)
    Downloading Pygments-2.1.3-py2.py3-none-any.whl (755kB)
       100% |████████████████████████████████| 757kB 892kB/s
Installing collected packages: docopt, pygments, cheat
    Running setup.py install for docopt ... done
    Running setup.py install for cheat ... done
Successfully installed cheat-2.1.24 docopt-0.6.2 pygments-2.1.3

Cheat가 제대로 설치되었는지 확인하려면 다음 명령어를 입력하세요.

$ cheat -v

출력 결과는 다음과 같습니다.

cheat 2.1.24

텍스트 편집기 설정하기

직접 나만의 치트 시트를 작성하려면, Cheat가 기본적으로 어떤 텍스트 편집기를 사용할지 알려주어야 합니다. nano 편집기를 설정하려면 다음 명령어를 사용합니다.

$ export EDITOR=/usr/bin/vim

위 명령어가 정상적으로 적용되었는지 확인하려면 아래 명령어를 실행합니다.

$ printenv EDITOR

출력 결과는 다음과 같아야 합니다.

/usr/bin/vim

이 변경 사항을 모든 향후 셸 세션에서도 유지되도록 영구적으로 적용하려면 환경 변수 선언을 .bashrc 파일에 추가해야 합니다. 이 파일은 bash 셸 세션이 시작될 때 실행되는 여러 파일 중 하나입니다. .bashrc 파일을 열려면 다음 명령어를 사용하세요.

$ nano ~/.bashrc

파일을 열면 아래와 비슷한 내용이 보일 것입니다.

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything
case $- in
    *i*) ;;
      *) return;;
esac
export EDITOR=/usr/bin/vim

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

# append to the history file, don't overwrite it
shopt -s histappend

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000

파일 안에 아래와 같이 동일한 export 명령어를 추가합니다.

.....................................................
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac

export EDITOR=/usr/bin/vim

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth
...........................................

편집이 끝나면 파일을 저장하고 종료합니다.

Cheat 실행하기

가장 기본적인 형태로 tail 명령어에 대한 치트 시트를 확인하려면 다음 명령어를 사용합니다.

$ cheat tail

실행 결과는 아래와 같습니다.

# To show the last 10 lines of file
tail file

# To show the last N lines of file
tail -n N file

# To show the last lines of file starting with the Nth
tail -n +N file

# To show the last N bytes of file
tail -c N file

# To show the last 10 lines of file and to wait for file to grow
tail -f file

현재 사용 가능한 모든 치트 시트 목록을 보려면 다음 명령어를 입력하세요.

$ cheat -l

출력 결과는 다음과 같습니다.

7z /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/7z
ab                     /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/ab
apk                    /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/apk
apparmor               /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/apparmor
apt                    /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/apt
apt-cache              /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/apt-cache
apt-get                /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/apt-get
aptitude               /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/aptitude
asciiart               /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/asciiart
asterisk               /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/asterisk
at                     /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/at
awk                    /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/awk
bash                   /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/bash
bower                  /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/bower
chmod                  /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/chmod
chown                  /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/chown
convert                /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/convert
crontab                /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/crontab
csplit                 /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/csplit
cups                   /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/cups
curl                   /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/curl
cut                    /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/cut
date                   /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/date
dd                     /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/dd
df                     /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/df
dhclient               /usr/local/lib/python2.7/dist-packages/cheat/cheatsheets/dhclient
..............................................................................................

치트 시트 생성 및 편집하기

새로운 치트 시트를 만들려면 다음 명령어를 사용합니다.

$ cheat -e tutorialspoint

여기서 tutorialspoint는 치트 시트의 이름입니다. 위 명령어를 실행하면 빈 치트 시트가 열리며, 원하는 내용을 추가한 뒤 저장하면 됩니다.

치트 시트 검색하기

치트 시트 내용을 검색하려면 다음 명령어를 사용합니다.

$ cheat -s tail

위 명령어는 tail 명령어를 검색하는 예시입니다. 실행 결과는 아래와 같습니다.

asterisk:
    # To print out the details of SIP accounts:
dnf:
    # To search package details for the given string
dpkg:
    # List all installed packages with versions and details
hardware-info:
    # Display all hardware details
journalctl:
    # Actively follow log (like tail -f)
mdadm:
    # See detailed array confiration/status
    mdadm --detail /dev/md${M}
    mdadm --detail --scan > /etc/mdadm/mdadm.conf
p4:
    # Print details related to Client and server configuration
pacman:
    pacman -Ql | sed -n -e 's/.*\/bin\///p' | tail -n +2
pip:
    # Show details of a package
tail:
    tail file
    tail -n N file
    tail -n +N file
    tail -c N file
    tail -f file

이것으로 Ubuntu에서 명령줄 치트 시트(Cheat)를 설치하고 사용하는 방법을 모두 살펴보았습니다. 앞으로도 더 많은 Linux 팁과 트릭을 소개해 드릴 예정이니 계속 지켜봐 주세요!