Linux 命令行工具
# Linux 命令行工具
# 介绍
命令行工具安装方式:
- Linux 端:Ubuntu(apt、snap 等)、Arch Linux(pacman、yay 等)
- Windows 端:scoop、winget 等
- Mac 端:brew;
- 程序端:Python(pip、conda),Rust(cargo),Nodejs(npm、yarn)
- 从 webinstall.dev (opens new window) 网站安装(后三者可以在无 root 权限情况下安装)
- 源码编译安装
参考资料:
- 命令行常用工具的替代品 - 阮一峰的网络日志 (opens new window)
- GitHub - ibraheemdev/modern-unix: A collection of modern/faster/saner alternatives to common unix commands. (opens new window)
- 有意思/搞笑的 GitHub repo:GitHub - terremoth/awesome-hilarious-repos: Awesome hilarious github repositories (opens new window)
# zsh
- 提升终端使用体验
- 插件丰富:可实现命令自动补全、高亮、建议;
x
解压任意格式压缩文件;z
路径快速跳转等 - 丰富的 git 命令 alias,git 状态可视化
- 插件丰富:可实现命令自动补全、高亮、建议;
- master、manager 上没有 zsh;Pi 和思源一号有 zsh,但版本较老
- zsh 系列插件:awesome-zsh-plugins (opens new window)
- 管理 zsh 配置:ohmyzsh (opens new window)
- 管理 bash 配置:oh-my-bash (opens new window)(没 ohmyzsh 好用)
# 安装
- Package Managers
sudo apt install zsh # Ubuntu
sudo pacman -S zsh # Arch Linux
brew install zsh # macOS
- 源码编译:依赖 ncurses;Building Zsh from Source and Configuring It on CentOS - jdhao's digital space (opens new window)
编译 ncurses
wget https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.4.tar.gz --no-check-certificate
./configure --prefix=${HOME}/local CXXFLAGS="-fPIC" CFLAGS="-fPIC"
make -j && make install
编译 zsh
wget https://sourceforge.net/projects/zsh/files/zsh/5.9/zsh-5.9.tar.xz/download -O zsh-5.9.tar.xz --no-check-certificate
./configure --prefix="${HOME}/local" CPPFLAGS="-I${HOME}/local/include" LDFLAGS="-L${HOME}/local/lib"
make -j && make install
设置 zsh 为默认 shell:;无 root 权限:
# 有 root 权限
chsh -s /bin/zsh
# 无 root 权限 在 ~/.bashrc_profile 添加以下内容(不建议)
export PATH=$HOME/bin:$PATH
export SHELL=`which zsh`
[ -f "$SHELL" ] && exec "$SHELL" -l
- Windows:
- 两种方式:WSL+zsh,git bash+zsh:Windows高效开发环境配置(一) - 北鱼扶摇 (opens new window)、在 Windows 中使用 Bash shell - 北辞 (opens new window)
- Windows Terminal 以及 VSCode 本地设置默认终端为 git bash:Windows Terminal添加Git Bash支持 - TruthHell - 博客园 (opens new window)
下载 zsh 包;复制 etc/
、usr/
到 Git 安装目录中;打开 Git Bash,执行命令 zsh
;设置 zsh 为默认 shell,在 .bashrc
添加:
wget https://mirror.msys2.org/msys/x86_64/zsh-5.9-2-x86_64.pkg.tar.zst
tar --zstd -xvf zsh-5.9-2-x86_64.pkg.tar.zst
# Enable zsh
if [ -t 1 ]; then
exec zsh
fi
# 配置
- 安装 ohmyzsh
# gitee 源
sh -c "$(curl -fsSL https://gitee.com/Devkings/oh_my_zsh_install/raw/master/install.sh)" # via curl
sh -c "$(wget https://gitee.com/Devkings/oh_my_zsh_install/raw/master/install.sh -O -)" # via wget
# github 源
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # via curl
sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # via wget
- 插件下载:zsh-completions(自动补全)、zsh-syntax-highlighting(高亮)、zsh-autosuggestions(建议)、powerlevel10k(主题)
# github 源
git clone --depth=1 https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM}/plugins/zsh-completions && \
git clone --depth=1 https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM}/plugins/zsh-autosuggestions && \
git clone --depth=1 https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting && \
git clone --depth=1 https://github.com/zdharma-continuum/fast-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/fast-syntax-highlighting && \
git clone --depth=1 https://github.com/jeffreytse/zsh-vi-mode ${ZSH_CUSTOM}/plugins/zsh-vi-mode && \
git clone --depth=1 https://github.com/MichaelAquilina/zsh-you-should-use.git ${ZSH_CUSTOM}/plugins/you-should-use && \
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM}/themes/powerlevel10k
# gitee 源
git clone --depth=1 https://gitee.com/yuhldr/zsh-syntax-highlighting.git ${ZSH_CUSTOM}/plugins/zsh-syntax-highlighting && \
git clone --depth=1 https://gitee.com/yuhldr/zsh-autosuggestions ${ZSH_CUSTOM}/plugins/zsh-autosuggestions && \
git clone --depth=1 https://gitee.com/yuhldr/zsh-completions ${ZSH_CUSTOM}/plugins/zsh-completions && \
git clone --depth=1 https://gitee.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM}/themes/powerlevel10k
备份 ~/.zshrc(如果有):
cp ~/.zshrc ~/.zshrc.bak
;更新 ohmyzsh:omz update
;配置 powerlevel10k:p10k configure
下载安装好 ohmyzsh 和 powerlevel10k 后,重新登录,会进入配置 powerlevel10k 的交互,按照指示自定义设置即可。
ohmyzsh 有用的内置与外置插件:Plugins · ohmyzsh/ohmyzsh Wiki · GitHub (opens new window)
################ 内置插件 ################
# 目录自动跳转,模糊匹配最近进入过的目录
z
# 丰富的 git alias
git
################ 外置插件 ################
zsh-syntax-highlighting
zsh-autosuggestions
zsh-completions
# bash 插件
bash-git-prompt # 效果还不错
bash-language-server # 有 Bash IDE 的 VSCode 插件
bash-completion
bash-snippets # 有 cheat 等可执行命令
# zsh 插件
zsh-fast-syntax-highlighting
zsh-you-should-use
zsh-vi-mode # Crtl + [ 进入 Normal mode
zsh-lovers
zsh-git-prompt
zsh tips tricks examples: ZSH-LOVERS(1) (opens new window)
# Windows 端安装配置 zsh
- 安装、配置 ohmyzsh
- 修改 Windows Terminal 的
settings.json
内容
{
// ...
// 添加项
// 默认启动为 Git Bash
"defaultProfile": "{5D1F95DF-36E8-56AD-C203-EA75CE06422C}",
// "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
// ...
"list":
[
// 添加项
{
"guid" : "{5D1F95DF-36E8-56AD-C203-EA75CE06422C}",
"name" : "Git Bash",
"commandline" : "D:\\Scoop\\apps\\git\\current\\bin\\bash.exe --login -i",
"icon" : "D:\\Scoop\\apps\\git\\current\\usr\\share\\git\\git-for-windows.ico",
"startingDirectory": "C:\\Users\\SLY\\Desktop"
},
// ...
],
},
}
# 相关问题
- zsh 中的
[nyae]
的含义:What does nyae mean in Zsh? - Stack Overflow (opens new window) - zsh 安装后,
Home / End
键可能会失效,对应快捷键:Home = Ctrl + A
,End = Ctrl + E
。
# 数据处理相关命令行工具
- csv 命令行工具:csvkit(Python)
in2csv data.xlsx | csvlook # excel 表格转 csv 表格查看
csvlook data.csv | head # 以表格形式查看
csvcut -n data.csv # 查看列名
csvscut -c 1,2,3 data.csv # 查看特定列数据 1 2 3 也可以是具体列名
csvstat --count data.csv # 统计行数
csvstat file.csv # 统计所有列的情况
csvstat -c 1,2,3 data.csv # 统计特定列
- josn 命令行工具:jq、jnv (opens new window)(交互式)
cat data.json | jq . # 输出 json 文件内容
cat data.json | jq '.user.name' # 获取特定键值
# 其他命令行工具
ripgrep、lsd、sd、bat、git-delta、gitui 等由 Rust 编写的 CLI 均可通过 cargo 安装
- 替代
man
:tldr (opens new window)(有时会失效)、eg (opens new window)、navi (opens new window)(默认的 cheatsheet 很少,效果一般) CTRL + R
历史命令升级版:mcfly (opens new window)- 替代
ls
:lsd (opens new window)(可下载 x86_64-unknown-linux-gnu 二进制版本)、exa (opens new window)、eza (opens new window)(可以与.gitignore 结合) - 替代
grep
:ripgrep (opens new window)(命令rg
) - 替代
sed
:sd (opens new window) - 替代
cat
:bat (opens new window)(可与 git 结合使用) - 替代
find
:fd (opens new window) - 替代
ps
:procs (opens new window) - 替代 diff:difftastic (opens new window)(命令
difft
) - 替代 top:btop (opens new window)、htop (opens new window)
- 文本编辑器:helix (opens new window)
- 终端 Markdown 渲染:frogmouth (opens new window)、glow (opens new window)
- 显示系统信息:neofetch (opens new window)、fastfetch (opens new window)(比 neofetch 更快)、hyfetch (opens new window)
- 磁盘分析:ncdu (opens new window)(有时较耗时)
- 文件对比:difftastic (opens new window)
- 文件搜索:fzf (opens new window)
- 统计代码文件行数:cloc (opens new window)
- 终端文件管理器:yazi (opens new window)、superfile (opens new window)、ranger (opens new window)、lf (opens new window)(效果一般)
- 富文本:rich (opens new window)
- 命令纠正:thefuck (opens new window)
- 将源代码生成美观图片:silicon (opens new window)、carbon (opens new window)
- neovim 配置:lazyvim (opens new window)(siyuan 无法使用)
- 字符 logo 制作:figlet、toilet:Linux 运维相关 — OnlineNote latest documentation (opens new window)
- 查看 coreutils 工具的进度条:progress (opens new window)
- Slurm TUI 版本(查看集群任务):GitHub - kabouzeid/turm: TUI for the Slurm Workload Manager (opens new window)
# turm 安装
cargo install turm
- 其他小工具: cowsay、sl(火车)、fortune(幸运饼干;格言)、lolcat、boxes、cmatrix(黑客帝国)、asciiquarium(水族馆)
ncdu -o ncdu.txt # 输出信息到文件中
# navi 使用
navi repo browse # 按需添加 cheatsheet git repo 以增加丰富性
# 升级 fzf
cd ~/.fzf && git pull && ./install
# rg 使用
# -g 过滤搜索
rg 'content' -g '!docs/' # 排除
rg 'content' -g '*.py' # 包含
# eg 安装
pip install -U eg
brew install eg-examples
# figlet toilet 相关用法
showfigfonts # 查看可用字体
figlet spt
figlet -c spt # 居中
figlet spt | toilet -f term --gay # 彩色输出
fzf 进阶用法
- fzf/ADVANCED.md at master · junegunn/fzf · GitHub (opens new window)
- Linux 上有哪些工具软件堪称精美? - 知乎 (opens new window)
# 搜索整个 apt package;回车安装
apt-cache search '' | sort | cut --delimiter ' ' --fields 1 | fzf --multi --cycle --reverse \ --preview-window=right:70%:wrap \ --preview 'apt-cache show {1}' | xargs -r sudo apt install -y
# 用 bat 作为 previewer
fzf --preview "bat --color=always --style=numbers --line-range=:500 {}"
# mcfly
brew install mcfly
curl -LSfs https://raw.githubusercontent.com/cantino/mcfly/master/ci/install.sh | sh -s -- --git cantino/mcfly
eval "$(mcfly init zsh)"
# fastfetch Ubuntu 安装
sudo add-apt-repository ppa:zhangsongcui3371/fastfetch
sudo apt update
sudo apt install fastfetch
终端显示图片(效果一般):GitHub - SilinMeng0510/imgcatr: cat for images, by RUST 🦀️ (opens new window)
GitHub - imsnif/bandwhich: Terminal bandwidth utilization tool (opens new window)
安全替代 rm
的脚本:trash.sh (opens new window)
starship (opens new window): 美观、可自定义的 shell prompt(支持多种 shell,与 ohmyzsh 的主题不兼容)
# 安装
curl -sS https://starship.rs/install.sh | sh
brew install starship # macOS
# 配置
eval "$(starship init zsh)" # zsh
eval "$(starship init bash)" # bash
Invoke-Expression (&starship init powershell) # powershell