如何在 Ubuntu 上安装和使用命令行备忘单
Cheat 是一款主要基于 Python 的命令行软件,允许系统管理员查看和保存有用的备忘单。它检索给定命令的纯文本示例,这将提醒用户选项、参数或常见用法。Cheat 用于“您经常使用的命令,但频率不足以记住的命令”。
安装 Cheat
在安装 Cheat 之前,我们需要确保系统上的所有内容都已更新,如下面的命令所示:
$ sudo apt-get update && sudo apt-get upgrade
最好使用 Python 包管理器 Pip 安装 Cheat。要安装 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
为了使此更改在所有未来的 shell 会话中持续有效,您必须将环境变量声明添加到您的 .bashrc 文件中。这是在 bash shell 会话开始时运行的几个文件之一。要打开 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
然后添加与下面相同的导出命令
..................................................... # 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,请使用以下命令:
$ 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 上安装和使用命令行备忘单,我们将推出更多基于 Linux 的技巧和提示。继续关注!