我有以下命令
find . -type f -print0 | xargs -0 chmod 644
Run Code Online (Sandbox Code Playgroud)
如果文件名不包含嵌入的空格,这将成功地将 . 中所有文件的权限更改为 644。但是,它通常不起作用。
例如
touch "hullo world"
chmod 777 "hullo*"
find . -type f -print0 | xargs -0 chmod 644
Run Code Online (Sandbox Code Playgroud)
返回
/bin/chmod: cannot access `./hello': No such file or directory
/bin/chmod: cannot access `world': No such file or directory
Run Code Online (Sandbox Code Playgroud)
有没有办法修改命令,以便它可以处理带有嵌入空格的文件?
非常感谢您的任何建议。
当我管道到gzip它不能接受 stdin 我应该使用xargs将 stdin 转换为参数
$ls
1.txt
$ls |xargs gzip && ls
1.txt.gz
Run Code Online (Sandbox Code Playgroud)
一切都好 。但是当我想压缩 cpio 存档文件时
$ls | cpio -ov | gzip > archive.cpio.gz
Run Code Online (Sandbox Code Playgroud)
也没关系,这ls | cpio -ov | xargs gzip 不起作用。为什么在第二种情况下gzip接受标准输入而不能接受参数?
我试图执行此命令来修复另一个错误(无法使用 TTY - 输入不是终端或正确类型的文件):
kubectl get pods -n foobar | grep baz | awk '{print $1}' | xargs -J % kubectl exec -it -n foobar % /bin/bash
Run Code Online (Sandbox Code Playgroud)
这导致了以下错误:
xargs: invalid option -- 'o'
Run Code Online (Sandbox Code Playgroud)
我能够在 Mac Mojave 上正确执行该命令,但在 Ubuntu 16.04 上却不能。
根据xargs 网站,应该有一个 -o 选项:
--open-tty
-o
在执行命令之前,在子进程中将 stdin 重新打开为 /dev/tty,从而允许该命令与终端关联,同时 xargs 从不同的流(例如,从管道)读取。如果您希望 xargs 运行交互式应用程序,这很有用。
grep -lz 模式 * | xargs -0o vi
但是man xargs没有显示这个选项。
该更新日志没有提到任何变化的标志。
我在 Ubuntu 16.04 LTS 上的 xargs 版本: …
我正在远程主机中并行运行一些查询命令,xargs该命令运行高效且运行良好,但在查找从哪个主机获取查询时遇到一些问题。
现在执行此操作的脚本部分如下所示:
export commands="cmd1; cmd2; "
hosts=("host1" "host2" "host3" "host4")
MaxInstance=10
echo_var(){
echo "Executing in $1:"; sleep 1; echo "Output of: $commands"; return 0;
}
export -f echo_var
printf "%s\n" ${hosts[@]} | xargs -n 2 -P $MaxInstance -I {} $(command -v bash) -c 'echo_var "$1"' _ {}
Run Code Online (Sandbox Code Playgroud)
其输出应如下所示,因为sleep 1:
Executing in host1:
Executing in host2:
Executing in host3:
Executing in host4:
Output of: cmd1; cmd2;
Output of: cmd1; cmd2;
Output of: cmd1; cmd2;
Output of: cmd1; …Run Code Online (Sandbox Code Playgroud) 作为大学课程的一部分,我试图在 500 个文件的列表中隐蔽地搜索一个字符串,然后将找到的这些项目复制到一个新文件夹中。
我可以毫无问题地找到文件
grep -rnw '/home/steve/Desktop.CEDFOR.arnold-j/sent/' -e 'vladimir.gorny@enron.com'
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试将结果通过管道传送到名为 Suspicious 的文件夹时,它会出错。
我正在使用的完整代码(基于几年前报告的类似问题)是
grep -rnw '/home/steve/Desktop.CEDFOR.arnold-j/sent/' -e 'vladimir.gorny@enron.com' | xargs -0 cp -t /home/steve/Desktop/CEDFOR/arnold-j/Suspicious/
Run Code Online (Sandbox Code Playgroud)
但是,错误指出不存在此类文件夹或文件。我可以确认文件夹 /home/steve/Desktop/CEDFOR/arnold-j/Suspicious/ 肯定存在。
澄清...我想将文件复制到“可疑”文件夹中,以便稍后查看。
我哪里错了?
为什么管道不能与 'find' 和 'ls' 一起使用如果我这样做了find . -name *foo* | ls -lah,它会执行 ls$PWD而不是 find 的输出。
然而,解决方案是find . -name *foo* | xargs -r ls -alh或可以使用 exec。
mkdir dregsfolder
find /home/tony/Desktop/unsorted_files/ -maxdepth 1 -not \( -type d -or -iname "*.jpg" -or -iname "*.gif" -or -iname "*.docx" \)xargs -0 --no-run-if-empty mv /home/tony/Desktop/dregsfolder
Run Code Online (Sandbox Code Playgroud)
当我运行该代码时;我被告知
find: paths must precede expression: )xargs
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为我认为路径已经用/home/tony/Desktop/unsorted_files.
我更加困惑,因为在Unix & Linux Stack Exchange 上的这个问题中,我们有一个xargs将文件复制到名为 的目录的示例play:
find /tmp/ -ctime -1 -name "x*" | xargs -I '{}' mv '{}' ~/play/
Run Code Online (Sandbox Code Playgroud)
而这里,似乎是目标目录,即播放;已在xargs...之后指定
我想澄清与之相关的精确机制xargs和其中的细微差别,不仅要解析目录中的文件(如上一个问题中所述),而且还要解析它在 unix 世界中与参数相关的更广泛的使用.
xargs ×7
bash ×4
command-line ×3
find ×2
chmod ×1
directory ×1
grep ×1
gzip ×1
kubernetes ×1
ls ×1
permissions ×1
pipe ×1
scripts ×1
tty ×1