cmdline

实用命令


后台运行
nginx &
redis-server &

jobs                #查看后台运行的进程

ctrl-z              #放到后台并暂停
bg                  #将后台暂停进程,改为运行 
fg                  #将后台进程,调到前台运行

nohup               #用户退出帐户,不挂断执行命令


curl参数
curl -x socks5://localhost:1088 -LO  https://github.com/k3s-io/k3s/releases/download/v1.28.5%2Bk3s1/k3s
curl -x socks5://user:passwd@localhost:1088 -LO  https://github.com/k3s-io/k3s/releases/download/v1.28.5%2Bk3s1/k3s

-L参数会让 HTTP 请求跟随服务器的重定向。curl 默认不跟随重定向。
-k参数指定跳过 SSL 检测。
-X参数指定 HTTP 请求的方法。
$ curl -H xxx  -d@xxx.json -X POST https://www.example.com

-F参数用来向服务器上传二进制文件。
$ curl -F 'file=@photo.png' https://google.com/profile
上面命令会给 HTTP 请求加上标头Content-Type: multipart/form-data,然后将文件photo.png作为file字段上传。
-F参数可以指定 MIME 类型。
$ curl -F 'file=@photo.png;type=image/png' https://google.com/profile
上面命令指定 MIME 类型为image/png,否则 curl 会把 MIME 类型设为application/octet-stream。
-F参数也可以指定文件名。
$ curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
上面命令中,原始文件名为photo.png,但是服务器接收到的文件名为me.png。

httpie Docs

HTTP service for test

HTTPie用法介绍

docker 私有仓库

http

安装:
pip install httpie

centOS:
yum install httpie

下载独立包升级
https --download packages.httpie.io/binaries/linux/http-latest -o http

模拟提交表单
http -f POST yhz.me username=nate

显示详细的请求
http -v yhz.me

只显示Header
http -h yhz.me

只显示Body
http -b yhz.me

下载文件
http -d yhz.me

请求删除的方法
http DELETE yhz.me

传递JSON数据请求(默认就是JSON数据请求)
http PUT yhz.me name=nate password=nate_password
如果JSON数据存在不是字符串则用:=分隔,例如
http PUT yhz.me name=nate password=nate_password age:=28 a:=true streets:='["a", "b"]'

模拟Form的Post请求, Content-Type: application/x-www-form-urlencoded; charset=utf-8
http --form POST yhz.me name='nate'
模拟Form的上传, Content-Type: multipart/form-data
http -f POST example.com/jobs name='John Smith' file@~/test.pdf

修改请求头, 使用:分隔
http yhz.me  User-Agent:Yhz/1.0  'Cookie:a=b;b=c'  Referer:http://yhz.me/

认证
http -a username:password yhz.me
http --auth-type=digest -a username:password yhz.me

使用http代理
http --proxy=http:http://192.168.1.100:8060 yhz.me
http --proxy=http:http://user:pass@192.168.1.100:8060 yhz.me


使用http socks5代理
http --proxy=http:socks5://user:pass@host:port --proxy=https:socks5://user:pass@host:port example.org
cp a.txt b.txt c.txt bak/
cp -u a.txt ../             只替换新文件

查看并发访问数
netstat -an | grep ESTABLISHED | wc -l

统计80端口数
netstat -nat | grep -i '80' | wc -l

查看进程
netstat -nato

统计进程个数
ps -aux | grep http | wc -l

netstat -n | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}'
其中:
  SYN_RECV表示正在等待处理的请求数;
  ESTABLISHED表示正常数据传输状态;
  TIME_WAIT表示处理完毕,等待超时结束的请求数。
find . -size +2000k     查找大文件
du | awk '$1 > 20000'   查找占用空间较大的目录
du -hs                  查看当前目录的占用空间大小

ls -d       只列出目录,不列出目录下的文件
ls -lS
ls -lSr     按文件大小从小到大排序
ls -lt
ls -ltr     从旧到新排序
ls -l | wc -l   统计文件个数
stat a.txt      显示文件创建修改等信息
touch       创建新文件,或修改文件时间属性

awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大。
简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理。

find命令不查找子目录
find . -type f 只查找当前目录下的文件
find . -path './src/emacs' -prune -o -print
find . -maxdepth 1 -name "*"
find . -maxdepth 6 -name '*.php' -ctime 30          30天内创建的文件
find . -maxdepth 6 -name '*.php' -size 3879c        文件的字节数
find . -maxdepth 3 -name '*.php' -size -1000c       字符少于1000字节的文件
find . -name '*.php' -mtime -1000                   100天内创建的文件

find . -name '*.txt' | sed -i 's/xwq/vick/g'        替换所有文件中的字串, 不加g表示一行中只替换一个
sed 's/xwq/vick/g' word1.txt > a.txt                加i写到本文件,不加i写到a.txt
sed -n '/adj/w a.txt' word1.txt                     -n只显示匹配行,adj形容词的行写到a.txt

sort -t ' ' -k 1 a.txt      按名字字母顺序排序
sort  -t' ' -k2nr k3 a.txt  按身高数字降序排序,-n表示数字而不是字母
uniq a.txt b.txt            删除重复行输出到b.txt
uniq -d a.txt               只显示重复的行 
uniq -u a.txt               只显示不重复的行
uniq -c a.txt               在行前加计数
sort a.txt | uniq -c        先排序后计算重复数

ln命令

默认创建硬链接,当使用–symbolic 时创建符号链接
理解硬链接的两个限制
(1)不允许给目录创建硬链接;
(2)只有在同一文件系统中的文件之间才能创建链接。

ln -s /tmp/abc.dat ~/                   创建同名链接
ln -s /tmp/abc.dat ~/xxx                创建不同名链接
rm -rf xxx                              删除链接
ln -s /usr/local/nginx/conf/vhost vhost 创建目录链接

ln -s file/a.txt test                   创建文件链接
ln -sf file/b.txt test                  强制覆盖文件链接

ln -s file/a_dir/ test                  创建目录链接
ln -sfn file/b_dir/ test                强制覆盖目录链接

xargs的使用

批量查找
find ./ -name *.php | xargs grep ‘关键字’

多文件批量替换
grep oldString -rl /path | xargs sed -i "s/oldString/newString/g"

文件中包含多个地址列表,批量下载
cat url-list.txt | xargs wget –c

想找所有jpg文件,并压缩
find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

wget

wget -c www.baidu.com -O du.xml -o down.log     //-c 断点续传,-O重命名 -o 输出文件名

设置http代理
wget http://www.baidu.com/ -e use_proxy=yes -e http_proxy=myproxy:port

设置https代理,不检查服务器证书
wget --no-check-certificate https://www.google.com/ -e use_proxy=yes -e https_proxy=myproxy:port

还可以在.wgetrc中设置代理

tar

tar -cvf xwtools.tar
tar -zcvf xwtools.tar.gz    //以gzip方式压缩
tar -zcvf wiki.tar.gz vimwiki      //以gzip方式压缩文件夹

tar -zcvf wiki dir1 dir2    //以gzip方式压缩多个文件夹
tar -zcvf wiki *.php dir1 dir2    //以gzip方式压缩文件和多个文件夹

tar -xvf xwtools.tar        //解压
tar -zxvf xwtools.tar.gz    //解压gzip
tar -zxvf vue.tar.gz index.html    //解压其中一个文件

tar -tf xwtools.tar.gz      //压缩文件列表
tar -tvf xwtools.tar.gz     //压缩文件列表

//解压其中一个文件
tar -zxvf vue.tar.gz index.html    

//排除目录
tar -zcvf tomcat.tar.gz --exclude=logs --exclude=libs

scp命令

复制文件
$scp local_file remote_username@remote_ip:remote_folder
$scp local_file remote_username@remote_ip:remote_file
$scp local_file remote_ip:remote_folder
$scp local_file remote_ip:remote_file

复制目录
$scp -r local_folder remote_username@remote_ip:remote_folder
$scp -r local_folder remote_ip:remote_folder

curl命令

1.-A,设置用户代理发送给服务器,即可以伪装客户端身份
curl -A testagent http://www.yinzhengjie.org.cn
tail /home/wwwlog/access.log 显示日志

2.-e,伪装跳转过来的网址
curl -e http://www.google.com/index.html http://www.gdctcm.cn

3.-cacert,指定CA证书 (SSL)
curl --cacert /etc/pki/CA/cacert.pem https://www.hkcd.com

4.-I选项,只获得对方的响应首部信息
curl -I www.baidu.com

5.显示http response的头信息
curl -i www.baidu.com

6.显示一次的http请求的通信过程
curl -v www.baidu.com

7.Curl执行GET/POST/PUT/DELETE操作
curl -X PUT www.baidu.com  
curl -X DELETE www.baidu.com
curl -X POST www.baidu.com  
curl -X GET www.baidu.com

8.用curl设置cookies
curl http://www.maxsourcemedia.com --cookie "user=root;pass=123456"

9.将cookie另存为一个文件
curl www.hkcd.com --cookie-jar cookie_file

linux录制

man sed         /模式查找,适应vim的编辑模式

script demo     录制操作过程到文件
script -a demo  追加录制过程到文件

录制终端操作
script -t 2 > timing.log -a output.session
运行操作
ctrl+d 结束进行
scriptreplay timing log output.session 回放
终端创建管道文件
mkfifo /tmp/scriptfile
另一个终端上运行
cat /tmp/scriptfile
第一个终端上运行
script -f /tmp/scriptfile
运行操作,另一个终端同步看到终端一的操作
exit或ctrl+d退出

cut

-b、-c、-f分别表示字节、字符、字段
(即byte、character、field)

cat /proc/interrupts | cut -c 1-15		列出每行的前15个字符
cut -f1 -d':' /etc/passwd | head -15	列出以:分隔的块field
cut -f1-3 -d':' /etc/passwd | head -15	列出以:分隔的块field, 1-3块
cut -f1,3 -d':' /etc/passwd | head -15	列出以:分隔的块field, 1,3块

cut -f1-3 -d':' -s --output delimiter='/' /etc/passwd 改:分隔为/分隔
cut -b1-3

汉字本身是双字节的,cut –c把汉字“我”当成一个字符来处理,而cut –b是以字节来处理,把“我”拆成了两个字节