Linux 命令行工具

9/18/2023 CLI

# 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 权限情况下安装)
  • 源码编译安装

参考资料:


# zsh


# 安装

  • Package Managers
sudo apt install zsh  # Ubuntu
sudo pacman -S zsh    # Arch Linux
brew install zsh      # macOS

编译 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

下载 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

GitHub - magicmonty/bash-git-prompt: An informative and fancy bash prompt for Git users (opens new window)

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"
            },
		    // ...
        ],
    },
}

# 相关问题


# 数据处理相关命令行工具

  • 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  # 统计特定列

cat data.json | jq .  # 输出 json 文件内容

cat data.json | jq '.user.name'  # 获取特定键值

# 其他命令行工具

ripgrep、lsd、sd、bat、git-delta、gitui 等由 Rust 编写的 CLI 均可通过 cargo 安装

# 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 进阶用法

# 搜索整个 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 - theryangeary/choose: A human-friendly and fast alternative to cut and (sometimes) awk (opens new window)

GitHub - imsnif/bandwhich: Terminal bandwidth utilization tool (opens new window)

GitHub - context-labs/mactop: mactop - Apple Silicon Monitor Top written in pure Golang! Under 1,000 lines of code. (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
Last Updated: 7/22/2024, 3:23:19 AM