一、什么是cheat?
cheat是在GNU通用公共许可证下,为Linux命令行用户发行的交互式备忘单应用程序。简单来说,它没有提供其他额外多余的信息,只通过使用实例告诉你一个命令参数如何使用。
二、在Redhat、CentOS系统中安装Cheat:
Cheat主要有两个依赖python和pip
1、安装python、pip
# yum install python python-pip -y # pip install --upgrade pip(更新pip到最新版本)
注:pip是一个方便的Python第三方包安装器。
2、下载并安装Cheat
目前只能通过Git下载Cheat,所以先安装git包:
# yum install git -y
使用pip安装所需要的python依赖包:
# pip install docopt pygments
接下来复制cheat的Git库:
# git clone https://github.com/chrisallenlane/cheat.git
进入cheat目录,运行setup.py脚本安装:
# cd cheat # python setup.py install
安装完成,运行cheat -v就可以看到目前的版本号。
三、cheat的一些配置设置:
1、你必须在~/.bashrc文件里设置EDITOR环境变量,打开用户.bashrc文件,加入下面这行保存退出:
export EDITOR=/usr/bin/vim
注:你也可以使用你喜欢的编辑器来替代vim。
2、添加cheat的自动补全特性,来确保不同解释器下命令行的自动补全。方法:将cheat.bash脚本clone下来,复制到你系统正确的路径下。
# wget https://github.com/chrisallenlane/cheat/raw/master/cheat/autocompletion/cheat.bash # cp cheat.bash /etc/bash_completion.d/ #mac默认的配置补全配置文件放置的地方为:/usr/local/Cellar/cheat/2.1.22/etc/bash_completion.d
其余解释器的自动补全脚本在这里:
https://github.com/chrisallenlane/cheat/tree/master/cheat/autocompletion
3、让语法高亮显示(可选):
在.bashrc文件中添加如下环境变量
export CHEATCOLOR=true
4、添加更多的命令参数:
Cheat默认只提供最基本和最常用的命令。cheat备忘单的内容保存在~/.cheat/目录里,我们可以手动在这个目录添加备忘单里面的内容,这样cheat将更强大。
# cheat -e xyz
这将打开xyz备忘单,如果对应的cheat-sheet可用的话。否则cheat会创建一个cheat-sheet。
使用关键字搜索备忘单,来看看包含所有命令的内置备忘单。
# cheat -d /root/.cheat /usr/lib/python2.7/site-packages/cheat/cheatsheets
复制内置的备忘单到你的本地目录。
# cp /usr/lib/python2.7/site-packages/cheat/cheatsheets/* /root/.cheat/
四、一些Cheat命令的实例
例子:
$ cheat find# To find files by case-insensitive extension (ex: .jpg, .JPG, .jpG): find . -iname "*.jpg" # To find directories: find . -type d # To find files: find . -type f # To find files by octal permission: find . -type f -perm 777 # To find files with setuid bit set: find . -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l # To find files with extension ‘.txt‘ and remove them: find ./path/ -name ‘*.txt‘ -exec rm ‘{}‘ \; # To find files with extension ‘.txt‘ and look for a string into them: find ./path/ -name ‘*.txt‘ | xargs grep ‘string‘ # To find files with size bigger than 5 Mb and sort them by size: find . -size +5M -type f -print0 | xargs -0 ls -Ssh | sort -z # To find files bigger thank 2 MB and list them: find . -type f -size +20000k -exec ls -lh {} \; | awk ‘{ print $9 ": " $5 }‘ # To find files modified more than 7 days ago and list file information find . -type f -mtime +7d -ls # To find symlinks owned by a user and list file information find . -type l --user=username -ls # To search for and delete empty directories find . -type d -empty -exec rmdir {} \; # To search for directories named build at a max depth of 2 directories find . -maxdepth 2 -name build -type d # To search all files who are not in .git directory find . ! -iwholename ‘*.git*‘ -type f # To find all files that have the same node (hard link) as MY_FILE_HERE find . -type f -samefile MY_FILE_HERE 2>/dev/null # To find all files in the current directory and modify their permissions find . -type f -exec chmod 644 {} \;
在mac上安装cheat
brew install cheat
之后再按照前面的说明进行配置即可
几个cheatsheet文档推荐下载:
参考文档:
Cheat – An Ultimate Command Line ‘Cheat-Sheet’ for Linux Beginners and Administrators