命令行及shell强化
一. shell插件系统
1.定制命令提示符
PS1="%n@%M %~ %D{%Y/%-m/%-d} %t Ret: %?"
%n:当前用户名
%M:当前主机名
%~:当前工作目录
2.设置双行提示符
ubuntu@VM-0-4-ubuntu ~ 2025/7/9 4:04PM Ret: 0> cat << EOF > prompt3.sh
heredoc> PS1="%n@%M %~ %D{%Y/%-m/%-d} %t Ret: %?
heredoc> > "
source ubuntu.zsh-theme # 错误:尝试在当前目录查找
source ~/.oh-my-zsh/custom/themes/ubuntu.zsh-theme # 正确:使用完整路径
Shell 的工作机制:
- source 命令不会像 $PATH 那样搜索所有目录
- 必须提供文件的完整路径或相对路径
二. 目录跳转
1. 路径智能补全
ubuntu@VM-0-4-ubuntu ~ 2025/7/9 5:12PM Ret: 0
> cd
demo/ entr-4.6/ myenv/
输入cd <Tab>选择目录
cd D<Tab>选择D开头的目录
cd /u/l/d/a<Tab>在一个以u开头的文件夹下的以l开头的文件夹下的...那个以a开头的目录
ubuntu@VM-0-4-ubuntu ~ 2025/7/9 5:29PM Ret: 0
> ca
cache_check cache_restore cagent_tools captoinfo
cache_dump cachestat-bpfcc capable-bpfcc case
cache_metadata_size cachetop-bpfcc capable.bt cat
cache_repair cache_writeback capsh catman
同样的<Tab>也可以补全命令
输入ca<Tab>会列出所有ca开头的命令
注意:Zsh原则是执行命令大于目录跳转,且满足大小写混合匹配
2.历史目录跳转
ubuntu@VM-0-4-ubuntu ~ 2025/7/9 5:42PM Ret: 0
> d
0 ~
1 ~/myenv/bin
2 ~/demo
ubuntu@VM-0-4-ubuntu ~ 2025/7/9 5:42PM Ret: 0
> 1
~/myenv/bin
ubuntu@VM-0-4-ubuntu ~/myenv/bin 2025/7/9 5:42PM Ret: 0
> d
0 ~/myenv/bin
1 ~
2 ~/demo
- 按下d可以查看历史目录,再按下数字进行跳转
3.模糊匹配跳转
~/Documents/python-workspace/cool-project #分别跳转两个工作目录留下记录
~/Documents/R-workspace/hot-project
j coll #模糊跳转到cool-projec
j hot #模糊跳转到hot-project
- 如果多个路径包含hot,跳转到权重最高的路径,可通过
j -d 100 #把当前目录权重减小100
4.搜索文件和目录
- 按指定拓展名搜索
ubuntu@VM-0-4-ubuntu ~ 2025/7/12 3:08PM Ret: 0
> find /usr/lib -type f -name '*.conf' #-type f表示搜索文件,- name后面为拓展名
/usr/lib/dhcpcd/dhcpcd-hooks/50-timesyncd.conf
- 搜素指定名字的目录
ubuntu@VM-0-4-ubuntu ~ 2025/7/12 3:09PM Ret: 0
> find /usr/lib -type d -name tar #type -d表示搜素目录
- 按指定规则搜素并执行操作
ubuntu@VM-0-4-ubuntu ~ 2025/7/12 4:21PM Ret: 0
> find /usr/lib -type f -name 'c*' -exec wc {} + #-exec wc {} +是对搜素目标执行的动作,wc统计文件字符、单词、行数,+表示执行命令结束位置
106 356 6438 /usr/lib/python3/dist-packages/service_identity/__pycache__/cryptography.cpython-312.pyc
- 搜素指定文件
ubuntu@VM-0-4-ubuntu ~ 2025/7/12 4:22PM Ret: 0
> locate '/usr/*/cilk.h'
- 路径模糊匹配
ubuntu@VM-0-4-ubuntu ~ 2025/7/14 2:58PM Ret: 1
> head -5 **/python3*/**/apt/**/core*
head -5:
head 命令用于显示文件开头部分,-5 表示只显示每个文件的前 5 行。
**/python3*/**/apt*/**/core*
这是一个 递归通配符模式(需启用 globstar),用于匹配文件路径:
**/python3*
在任意子目录(包括多级)中匹配以 python3 开头的目录(如 python3.8、python3.10)。
/**/apt*
在 python3* 目录下的任意子目录中匹配以 apt 开头的目录(如 apt、apt_pkg)。
/**/core*
在 apt* 目录下的任意子目录中匹配以 core 开头的文件(如 core.py、core.so)。
head -5 <Ctrl-t> #fzf路径模糊匹配
python3aptcore
三、智能辅助
- 历史命令模糊匹配
ubuntu@VM-0-4-ubuntu ~ 2025/7/15 4:13PM Ret: 0
> head -5 times.csv|xargs -I {} python -c 'from datetime import datetime
dt = datetime.fromtimestamp({});
print(dt.strftime("Full date and time:%Y-%m-%d %H:%M:%S"))'
Full date and time:2018-04-27 09:36:01
Full date and time:2019-08-03 00:19:21
Full date and time:2020-02-24 05:22:41
Full date and time:2020-02-27 16:42:41
Full date and time:2020-03-21 20:16:01
xargs -I {}
将前一步的输出作为参数传递给后续命令
-I {} 表示用 {} 作为占位符替换传入的参数(每行内容)
对每一行输入执行以下操作:
读取一行输入(如 "2023-01-02 08:30")
用该行内容完全替换命令中的 {} 占位符
执行生成的新命令
python -c 'from datetime import datetime ...'
执行一个简短的Python脚本
-c 表示直接执行引号内的Python代码
from datetime import datetime 导入Python的日期时间处理模块
<Ctrl-t>查看历史命令
四、别名机制
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:05AM Ret: 0
> alias apla='asdf plugin-list-all'
格式:alias <alias-name>=<command> 其中alias-name是别名,command是原来的命令
当command中包括空格时,为了避免alias将空格后面的部分当成参数,需要用单引号或者双引号将其包裹起来
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:13AM Ret: 0
> alias ehw="echo 'hello world'"
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:14AM Ret: 0
> ehw
hello world
错误写法
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:14AM Ret: 0
> alias ehw='echo 'hello world''
'echo 'hello被当成command,而world''被当成无效命令,因为二者之间有一个空格
如果有单双引号混合使用,要使用转义技术""
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:14AM Ret: 0
> alias #列出当前会话中的别名X
-='cd -'
...=../..
....=../../..
.....=../../../..
......=../../../../..
1='cd -1'
2='cd -2'
3='cd -3'
4='cd -4'
5='cd -5'
6='cd -6'
7='cd -7'
8='cd -8'
9='cd -9'
_='sudo '
egrep='grep -E --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox}'
ehw='echo '\''hello world'\'
- 应用信息查询工具
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:37AM Ret: 0
> whatis whatis whereis which type file dpkg #打印各工具的功能介绍
whatis (1) - display one-line manual page descriptions
whereis (1) - locate the binary, source, and manual page files for a command
which (1) - locate a command
FILE (3type) - input/output stream
file (1) - determine file type
Dpkg (3perl) - module with core variables
dpkg (1) - package manager for Debian
type: nothing appropriate.
ubuntu@VM-0-4-ubuntu ~ 2025/7/17 10:39AM Ret: 0
> type whatis whereis which type file dpkg #打印各工具类型
whatis is /usr/bin/whatis
whereis is /usr/bin/whereis
which is a shell builtin
type is a shell builtin
file is /usr/bin/file
dpkg is /usr/bin/dpkg