xargs
xargs 可以将管道或标准输入(stdin)数据转换成命令行参数,也能够从文件的输出中读取数据。
参数
- -a file 从文件中读入作为 stdin
- -e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
- -p 当每次执行一个argument的时候询问一次用户。
- -n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。
- -t 表示先打印命令,然后再执行。
- -i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替。
- -r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
- -s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
- -L num 从标准输入一次读取 num 行送给 command 命令。
- -l 同 -L。
- -d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符。
注意事项
由于
xargs
默认将空格作为分隔符,所以不太适合处理文件名,因为文件名可能包含空格。
find
命令有一个特别的参数-print0
,指定输出的文件列表以null
分隔。然后,xargs
命令的-0
参数表示用null
当作分隔符。
实例
# 复制 *.jpg 文件到 /data/images 目录下
ls *.jpg | xargs -n1 -I {} cp {} /data/images
# 删除/path路径下的所有文件。由于分隔符是null,所以处理包含空格的文件名,也不会报错。
find /path -type f -print0 | xargs -0 rm
curl
# 简单的发送 post 请求
curl -X POST https://www.db117.site
# 使用 pos t发送表单数据,-X 可以省略
curl -d 'login=emma&password=123' https://google.com/login
curl -d 'login=emma' -d 'password=123' https://google.com/login
# 发送表单请求,并支持重定向
curl -L -d 'tweet=hi' https://api.twitter.com/tweet
# 使用 post 发送 JSON 数据
curl -d '{"login": "emma", "pass": "123"}' -H 'Content-Type: application/json' https://google.com/login
# 使用 post 发送文件数据
curl -d '@data.txt' https://google.com/login
# 使用url编码数据(转义一些特殊字符)
curl --data-urlencode 'comment=hello world' https://google.com/login
# 使用 post 发送表单文件
curl -F 'file=@photo.png' https://google.com/profile
# 使用 post 发送表单文件并指定 MIME Type
curl -F 'file=@photo.png;type=image/png' https://google.com/profile
# 使用 post 发送表单文件并指定文件名称
curl -F 'file=@photo.png;filename=me.png' https://google.com/profile
# 添加文件头(可以添加多个)
curl -H 'Accept-Language: en-US' https://google.com
# 添加一个 cookie
curl -b 'session=1' https://google.com
# 从文件中加载 cookie
curl -b cookies.txt https://www.google.com
# 把响应 cookie 保存在文件中
curl -c cookies.txt https://www.google.com
# 支持重定向
curl -L https://www.google.com
# 忽略 ssl 认证
curl -k https://catonmat.net
# 使用 Basic 认证
curl -u 'bob:12345' https://google.com/login
curl https://bob:12345@google.com/login
# 把 username 放在 shell 命令里面,密码单独输入
curl -u 'bob' https://google.com/login
# 使用代理
# socks5
curl -x socks5://james:cats@myproxy.com:8080 https://catonmat.net
# socks4
curl -x socks4://james:cats@myproxy.com:8080 https://catonmat.net
# HTTP
curl -x james:cats@myproxy.com:8080 https://catonmat.net
# 使用 silent 模式
# 成功则打印返回值, 失败什么都不打印,出现异常只能通过 echo $? 获取上次 exit code 来判读
curl -s https://catonmat.net
# 什么都不会打印,不管是否成功 出现异常只能通过 echo $? 获取上次 exit code 来判读
curl -s -o /dev/null https://google.com
# 成功什么都不会打印 出现异常会打印出异常
curl -S -s -o /dev/null https://google.com
# 把响应保存到文件 response.txt 中
curl -o response.txt https://google.com?q=kitties
# 把响应保存到文件中,文件名称是 url 的最后一部分
curl -O https://catonmat.net/ftp/digg.pm
# 限速(默认单位是字节,可以使用 k,m,g)
curl --limit-rate 200k https://google.com
# debug
# 打印出所有请求,主要包括 ssl 认证
curl -v https://catonmat.net
# 打印出详细的数据
curl --trace - https://catonmat.net
# 打印出详细的数据,添加时间戳
curl --trace - --trace-time https://catonmat.net
# 把响应头也打印出来
curl -i https://catonmat.net
# 只打印响应头
curl -s -o /dev/null -D - https://catonmat.net
# 只打印请求头
curl -v -s -o /dev/null --stderr - https://catonmat.net | grep '^>'
# 只打印响应编码
curl -w '%{response_code}' -s -o /dev/null https://catonmat.net