bash find -exec 与多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5119946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
find -exec with multiple commands
提问by Andy
I am trying to use find -exec with multiple commands without any success. Does anybody know if commands such as the following are possible?
我试图将 find -exec 与多个命令一起使用,但没有成功。有谁知道是否可以使用以下命令?
find *.txt -exec echo "$(tail -1 '{}'),$(ls '{}')" \;
Basically, I am trying to print the last line of each txt file in the current directory and print at the end of the line, a comma followed by the filename.
基本上,我试图打印当前目录中每个 txt 文件的最后一行,并在行尾打印,一个逗号后跟文件名。
回答by Tinker
find
accepts multiple -exec
portions to the command. For example:
find
接受-exec
命令的多个部分。例如:
find . -name "*.txt" -exec echo {} \; -exec grep banana {} \;
Note that in this case the second command will only run if the first one returns successfully, as mentioned by @Caleb. If you want both commands to run regardless of their success or failure, you could use this construct:
请注意,在这种情况下,只有在第一个命令成功返回时才会运行第二个命令,如@Caleb 所述。如果您希望两个命令无论成功或失败都运行,则可以使用以下构造:
find . -name "*.txt" \( -exec echo {} \; -o -exec true \; \) -exec grep banana {} \;
回答by Avari
find . -type d -exec sh -c "echo -n {}; echo -n ' x '; echo {}" \;
回答by Paused until further notice.
One of the following:
以下之一:
find *.txt -exec awk 'END {print multiple_cmd() {
tail -n1 ;
ls
};
export -f multiple_cmd;
find *.txt -exec bash -c 'multiple_cmd "multiple_cmd() { tail -1 ; ls }; export -f multiple_cmd; find *.txt -exec bash -c 'multiple_cmd "find ... | while read -r file; do
echo "look at my $file, my $file is amazing";
done
"' {} \;
"' {} \;
"," FILENAME}' {} \;
find *.txt -exec sh -c 'echo "$(tail -n 1 ""),"' _ {} \;
find *.txt -exec sh -c 'echo "$(sed -n "$p" ""),"' _ {} \;
回答by Al3x
Another way is like this:
另一种方式是这样的:
while read -r file; do
echo "look at my $file, my $file is amazing";
done <<< "$(find ...)"
in one line
一行
echo $(tail -1 ),
- "
multiple_cmd()
" - is a function - "
export -f multiple_cmd
" - will export it so any other subshell can see it - "
find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
" - find that will execute the function on your example
- "
multiple_cmd()
" - 是一个函数 - "
export -f multiple_cmd
" - 将导出它以便任何其他子shell都可以看到它 - “
find *.txt -exec bash -c 'multiple_cmd "$0"' {} \;
” - 找到将在您的示例上执行该功能
In this way multiple_cmd can be as long and as complex, as you need.
通过这种方式,multiple_cmd 可以根据您的需要变得尽可能长和复杂。
Hope this helps.
希望这可以帮助。
回答by Camilo Martin
There's an easier way:
有一个更简单的方法:
chmod +x lastline.sh
Alternatively:
或者:
find . -name "*.txt" -exec ./lastline.sh {} \;
回答by Andrea Spadaccini
I don't know if you can do this with find, but an alternate solution would be to create a shell script and to run this with find.
我不知道你是否可以用 find 来做到这一点,但另一种解决方案是创建一个 shell 脚本并用 find 运行它。
lastline.sh:
最后一行.sh:
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "echo tail -10000 {} \> fictmp; echo cat fictmp \> {} " \;
Make the script executable
使脚本可执行
find ... -exec zcat {} | wc -l \;
Use find
:
使用find
:
find ... | while read -r file; do echo "$file: `zcat $file | wc -l`"; done
回答by Eric Duruisseau
1st answer of Denis is the answer to resolve the trouble. But in fact it is no more a find with several commands in only one exec like the title suggest. To answer the one exec with several commands thing we will have to look for something else to resolv. Here is a example:
Denis 的第一个答案是解决问题的答案。但实际上,它不再像标题所暗示的那样在一个 exec 中包含多个命令。要用几个命令来回答一个 exec,我们将不得不寻找其他东西来解决。下面是一个例子:
Keep last 10000 lines of .log files which has been modified in the last 7 days using 1 exec command using severals {} references
使用 1 个 exec 命令使用多个 {} 引用保留过去 7 天内修改过的最后 10000 行 .log 文件
1) see what the command will do on which files:
1)查看命令将对哪些文件执行什么操作:
find . -name config -type f \( -exec grep "bitbucket" {} \; -a -exec echo {} \; \)
2) Do it: (note no more "\>" but only ">" this is wanted)
2)这样做:(注意不要更多的“\>”,而只有“>”这是想要的)
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "tail -10000 {} > fictmp; cat fictmp > {} ; rm fictmp" \;
find / -name "*.log" -a -type f -a -mtime -7 -exec sh -c "tail -10000 {} > fictmp; cat fictmp > {} ; rm fictmp" \;
回答by Greg Dougherty
Thanks to Camilo Martin, I was able to answer a related question:
感谢 Camilo Martin,我能够回答一个相关的问题:
What I wanted to do was
我想做的是
url = [email protected]:a/a.git
./a/.git/config
url = [email protected]:b/b.git
./b/.git/config
url = [email protected]:c/c.git
./c/.git/config
which didn't work. However,
这不起作用。然而,
find *.txt -type f -exec tail -1 {} \; | xargs -ICONSTANT echo $(pwd),CONSTANT
does work, so thank you!
确实有效,谢谢!
回答by user9869932
Extending @Tinker's answer,
扩展@Tinker 的回答,
In my case, I needed to make a command | command | command
inside the -exec
to print both the filename and the found text in files containing a certain text.
就我而言,我需要做一个command | command | command
内部的-exec
同时打印文件名,并在含有一定的文本文件找到的文本。
I was able to do it with:
我能够做到:
find *.txt -type f -exec echo ,$(PWD) {} + -exec tail -1 {} + | tr ' ' '/'
the result is:
结果是:
##代码##回答by smapira
should use xargs :)
应该使用 xargs :)
##代码##another one (working on osx)
另一个(在 osx 上工作)
##代码##