44 篇文章带有标签 “command-line”

git pull 批量更新多个仓库

git -C <目录> pull

这个命令用于从远程仓库获取最新代码并合并到当前分支。-C 选项允许你在指定的目录中运行 git 命令,而不需要先切换到那个目录。

下面是 MCP 相关的仓库

create-python-server
create-typescript-server
docs
inspector
python-sdk
quickstart-resources
servers
specification
typescript-sdk

更新所有仓库

方法一:手动更新

通过命令使用 ChatGPT

ChatGPT Wrapper

ChatGPT Wrapper is an open-source unofficial Power CLI, Python API and Flask API that lets you interact programmatically with ChatGPT.

安装

必要条件

  • macOS
brew install moreutils
  • Ubuntu
sudo apt install moreutils

创建虚拟环境

mkdir chatgpt-wrapper && cd chatgpt-wrapper

python -m venv env
source ./env/bin/activate

使用 GitHub 安装最新版本

pip install --upgrade pip
pip install git+https://github.com/mmabrouk/chatgpt-wrapper

Playwright 中安装浏览器,默认为 firefox。

playwright install

ChatGPT 安装

以安装模式启动程序。 这将打开一个浏览器窗口。 在浏览器窗口中登录 ChatGPT,然后停止该程序。

命令curl

下载文件

下载单个文件

curl http://book.d2l.ai/_images/catdog.jpg -o catdog.jpg

下载多个文件

curl https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.xml https://download.01.org/opencv/2021/openvinotoolkit/2021.1/open_model_zoo/models_bin/1/face-detection-retail-0004/FP32/face-detection-retail-0004.bin -o face-detection-retail-0004.xml -o face-detection-retail-0004.bin

下载文件并创建目录 curl --create-dirs https://download.01.org/opencv/2021/openvinotoolkit/2021.

命令 nc

捕获 HTTP 请求的内容

  1. 监听端口,用于捕获数据。
nc -l port 
  1. 发送 HTTP 请求。
curl http://ip:port/

GET 请求

curl http://127.0.0.1:8000/
GET /?name=wjj HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.61.1
Accept: */*

POST JSON 请求

curl --location --request POST 'http://127.0.0.1:8000/users_by_json' \
--header 'Content-Type: application/json' \
--data-raw '{
    "name": "wjj",
    "age": 40
}'
POST /users_by_json HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/7.61.1
Accept: */*
Content-Type: application/json
Content-Length: 36

{
    "name": "wjj",
    "age": 40
}

HTTP 基准测试工具

wrk

wrk 使用的是 HTTP/1.1

安装

需要从 GitHub 上克隆代码自己编译,编译前需要安装 git, gcc。

git clone https://github.com/wg/wrk.git
cd wrk
#使用多线程(机器的处理器核数)加速编译,
make -j $(nproc)
cp wrk /usr/local/bin/

测试

10 个线程,保持打开 100 个并发连接,持续 10 秒。

wrk -t10 -c100 -d10 http://www.baidu.com/
Running 10s test @ http://www.baidu.com/
  10 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   138.46ms  219.05ms   1.89s    92.11%
    Req/Sec   128.07     79.83   700.00     94.40%
  12776 requests in 10.02s, 128.22MB read
  Socket errors: connect 0, read 57, write 0, timeout 3
Requests/sec:   1275.30
Transfer/sec:     12.80MB

打印详细的延时分布信息 wrk -c10 -t4 --latenc

Json Formatter

test.json

{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}

jq

在命令行运行

jq . <<< '{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}'
jq . test.json

在vim的命令模式下运行

%!jq .

python json.tool

在命令行运行

python -m json.tool <<< '{ "stuff": { "that": [1,2,3], "isin": true, "json": "end"}}'
python -m json.tool test.json

在vim的命令模式下运行

%!python -m json.tool

在线格式化 Format JSON JSON Formatter JSON Formatter, Validator, Vi

命令tr

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            126G     0  126G   0% /dev
tmpfs            26G  4.0M   26G   1% /run
/dev/sda2       548G   50G  471G  10% /
tmpfs           126G     0  126G   0% /sys/fs/cgroup
/dev/sda1       511M  7.9M  504M   2% /boot/efi
/dev/sdb1       2.0T  4.7G  1.9T   1% /data

字母小写转大写

$ df -h | tr [:lower:] [:upper:]
FILESYSTEM      SIZE  USED AVAIL USE% MOUNTED ON
UDEV            126G     0  126G   0% /DEV
TMPFS            26G  4.0M   26G   1% /RUN
/DEV/SDA2       548G   50G  471G  10% /
TMPFS           126G     0  126G   0% /SYS/FS/CGROUP
/DEV/SDA1       511M  7.9M  504M   2% /BOOT/EFI
/DEV/SDB1       2.0T  4.7G  1.9T   1% /DATA
$ df -h | tr a-z A-Z

字母大写转小写

$ df -h | tr [:upper:] [:lower:]
$ df -h | tr A-Z a-z

安装Go

安装

https://golang.google.cn/dl/ 下载对应操作系统的安装包。

Mac

wget https://golang.google.cn/dl/go1.17.7.darwin-amd64.pkg

Linux

wget https://golang.google.cn/dl/go1.17.7.linux-amd64.tar.gz
sudo tar -C /usr/local/ -xzf go1.17.7.linux-amd64.tar.gz

Ubuntu

sudo apt install golang

配置

$ sudo vim /etc/profile
PATH=$PATH:/usr/local/go/bin

#Go代理配置(解决墙的问题)
export GOPROXY=https://goproxy.cn,direct
export GO111MODULE=on
$ source /etc/profile

查看 Go 版本信息

$ go version
go version go1.17.7 linux/amd64

参考资料

使用终端浏览Markdown和HTML

浏览Markdown

sudo apt install lynx
sudo apt install pandoc

pandoc README.md | lynx -stdin
sudo pip install grip
sudo apt install lynx

grip -b README.md
lynx http://localhost:6419/
sudo apt install pandoc
pandoc README.md -t plain | less

浏览HTML

sudo apt install w3m

w3m index.html
sudo apt install lynx
sudo apt install pandoc

pandoc index.html | lynx -stdin
sudo pip install grip

grip -b index.html

参考资料

命令lftp

安装 lftp

sudo apt-get -y install lftp

登录 FTP

lftp [-d] [-e cmd] [-p port] [-u user[,pass]] [site]

lftp -p 8821 -u sdlrzn download.cambricon.com

执行内部命令

查看 FTP 服务器目录内容

ls

下载文件

get /product/GJD/MLU270/1.7.604/Ubuntu18.04/Driver/neuware-mlu270-driver-dkms_4.9.8_all.deb

下载目录

mirror remote local

mirror MLU270 MLU270

执行本地系统命令

local pwd
local ls
local rm -r MLU270

参考资料

命令man help info

man

manual 的缩写。在线参考手册的接口

  • man man
......
    下表显示了手册的 章节 号及其包含的手册页类型。

    1   可执行程序或 shell 命令
    2   系统调用(内核提供的函数)
    3   库调用(程序库中的函数)
    4   特殊文件(通常位于 /dev)
    5   文件格式和规范,如 /etc/passwd
    6   游戏
    7   杂项(包括宏包和规范,如 man(7),groff(7))
    8   系统管理命令(通常只针对 root 用户)
    9   内核例程 [非标准]
......
  • 查看指定章节
man 7 man
man man.7
  • 寻找所有匹配(-a, --all 寻找所有匹配的手册页)
man -a passwd
--Man-- 下一页: passwd(5) [ 查看 (return) | 跳过 (Ctrl-D) | 退出 (Ctrl-C) ]

help

shell 自带的命令为内部命令,其它的为外部命令。

  • 内部命令使用 help
help cd
cd --help
  • 外部命令使用 help
ls --help

type 查看内部命令还是外部命令

$ type cd
cd 是 shell 内建
$ type ls
ls 是 `ls --color=auto' 的别名
$ type curl
curl 是 /usr/bin/curl

builtin 查看所有内部命令 man builtin bash,

命令base64

编码

macOS

base64 file

Linux

-w, --wrap=COLS wrap encoded lines after COLS character (default 76). Use 0 to disable line wrapping

base64 -w0 file

解码

base64 -d

问题

YWRtaW4=admin 的 base64 编码。

  • 解码(正确,这里之所以正确是因为base64过滤了。)
$ echo 'YWRtaW4=' | base64 -d
admin[username@hostname ~]$
  • 编码(错误,这是因为 echo 输出字符后会在后面再输出换行符。)
$ echo 'admin' | base64
YWRtaW4K

解决方案

  • 方法一:使用 printf 命令。
$ printf 'admin' | base64
YWRtaW4=
  • 方法二:可以通过参数 -n 告诉 echo 不输出换行符。
$ echo -n 'admin' | base64
YWRtaW4=
  • 方法三:可以通过参数 -e 告诉 echo 启用反斜杠转义的解释。
$ echo -e 'admin\c' | base64
YWRtaW4=

可用的转义符(来自 man echo): - \ backslash - \a alert (BEL) - \b backspace - \c produce no further output - \e escape - \f form feed - \n new line - \r carriage return - \t horizontal tab - \v vertical tab - \0NNN byte with octal value NNN (1 to 3 digits) - \xHH byte with hexadecimal value HH (1 to 2 digits)

命令brew

安装

brew install putty

FAQ

1、Updating Homebrew... 卡住

$ brew install putty
Updating Homebrew...
  • 方法1:直接关闭brew每次执行命令时的自动更新
$ vim ~/.bash_profile
export HOMEBREW_NO_AUTO_UPDATE=true
$ source ~/.bash_profile
  • 方法2:替换brew源
cd "$(brew --repo)"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/brew.git
cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew-core.git
brew update

2、`initialize': Version value must be a string; got a NilClass () (TypeError) $ brew install putty /usr/local/Homebr

Linux Shell 实践

快捷键

  • Ctrl+r 快速查找历史命令
  • Ctrl+l 清理控制台屏幕

移动光标

  • Ctrl+a 移动光标到行首
  • Ctrl+e 移动光标到行尾
  • Alt+Left Arrow 移动光标到上一个单词
  • Alt+Right Arrow 移动光标到下一个单词

删除字符

  • Ctrl+u 删除光标之前的内容
  • Ctrl+k 删除光标之后的内容
  • Ctrl+w 删除光标前面的一个单词

进程

  • Ctrl+d 退出。等同于exit命令
  • Ctrl+z 当前运行的程序后台运行。如果一步到位,可以在命令后面加 &

重定向

  • 执行时的错误信息输出到文件(2>)
hello 2> log.error
$ cat log.error 
-bash: hello: 未找到命令
  • 执行时的所有信息都输出到文件(&>)
echo hello &> log.info
$ cat log.info 
hello
  • 创建一个文件并写入内容(> filename <<EOF)
cat > hello.sh << EOF
#!/bin/bash
echo hello
EOF

变量

变量赋值

  • 执行结果保存到变量($() ``)
var1=$(pwd)
var2=`pwd`
  • 整数四则运算(let)
let n=10-3+4/2
echo $n
9

变量引用 ${var} 大部分情况可省略为

Linux Shell 执行方式

在当前shell下一行执行多条命令(;)

cd /etc/ssh ; cat ssh_config ; pwd ; du -sh /etc/ssh/ssh_config

创建shell脚本

vim ssh_config-info.sh
#!/bin/bash
cd /etc/ssh
cat ssh_config
pwd
du -sh /etc/ssh/ssh_config

shell脚本执行方式

bash ssh_config-info.sh

  • 创建子进程执行脚本
bash ssh_config-info.sh
$ pwd
/root

./ssh_config-info.sh

  • 需要执行权限
chmod u+x ssh_config-info.sh
  • 使用脚本文件中第一行#!指定的shell创建子进程执行脚本
./ssh_config-info.sh
$ pwd
/root

source ssh_config-info.sh

  • 在当前shell进程中执行,会对当前shell产生影响
source ssh_config-info.sh
$ pwd
/etc/ssh

. ssh_config-info.sh

  • 在当前shell进程中执行,会对当前shell产生影响(.相当于source)
. ssh_config-info.sh
$ pwd
/etc/ssh

命令tar

    -c, --create Create a new archive.  Arguments supply the names of the files to be archived.
    -x, --extract, --get Extract files from an archive.
    -z, --gzip, --gunzip, --ungzip Filter the archive through gzip(1).
    -j, --bzip2 Filter the archive through bzip2(1).
    -f, --file=ARCHIVE Use archive file or device ARCHIVE.  If this option is not given, tar will first examine the environment variable `TAPE'.  If it is set, its value will be used as the archive  name.   Otherwise, tar will assume the compiled-in default.
    -t, --list List the contents of an archive.
    -v, --verbose Verbosely list files processed.

命令zip

准备 yolov5

yolov5/
├── detect.py
├── Dockerfile
├── hubconf.py
├── LICENSE
├── models
│   ├── common.py
│   ├── experimental.py
│   ├── export.py
│   ├── __init__.py
│   ├── yolo.py
│   ├── yolov5l.yaml
│   ├── yolov5m.yaml
│   ├── yolov5s.yaml
│   └── yolov5x.yaml
├── README.md
├── requirements.txt
├── test.py
├── train.py
├── tutorial.ipynb
└── weights
    └── download_weights.sh

打包整个目录

zip -r yolov5.zip yolov5/

排除目录和文件

打包前,最好把之前的 zip 文件删除,不然是增量追加,还会看到排除的目录和文件。

zip -r yolov5.zip yolov5/ -x "yolov5/.git/*" -x "yolov5/.github/*" -x "yolov5/.gitattributes"

排除所有的 yaml 文件 zip -r yolov5.