在 shell 上查找文件并回显内容

在 shell 上查找文件并回显内容

我试图按名称搜索目录中的所有文件并将文件的内容输出到 shell 上。

目前我只获取文件列表

find -name '.htaccess' -type f


./dir1/.htaccess
./dir23/folder/.htaccess
...

但我怎样才能输出内容每个文件的替代。想到类似的事情通过管道传输文件名cat-命令。

答案1

在的谓词cat内使用:-execfind

find -name '.htaccess' -type f -exec cat {} +

这将依次输出文件的内容。

答案2

请参阅手册页了解find( man find)。

-exec utility [argument ...] ;
         True if the program named utility returns a zero value as its exit
status. Optional arguments may be passed to the utility. The expression must be
terminated by a semicolon (``;'').  If you invoke find from a shell you may need
to quote the semicolon if the shell would otherwise treat it as a control
operator. If the string ``{}'' appears anywhere in the utility name or the
arguments it is replaced by the pathname of the current file.  Utility will be
executed from the directory from which find was executed. Utility and arguments
are not subject to the further expansion of shell patterns and constructs.

-exec utility [argument ...] {} +
         Same as -exec, except that ``{}'' is replaced with as
many pathnames as possible for each invocation of utility.  This behaviour is
similar to that of xargs(1).

所以,只需按下开关即可-exec

find -type f -name '.htaccess' -exec cat {} +

答案3

您可能想使用-exec的选项find

find -name some_pattern -type f -exec cat {} +

而且,如果都是纯文本,并且你想一一查看,请替换catless(或view来自vim)

find -name some_pattern -type f -exec less {} +

要查看和编辑,请使用vimemacsgedit(根据您自己的选择)

find -name some_pattern -type f -exec vim {} +

相关内容