![linux中bash命令](https://i-blog.csdnimg.cn/blog_migrate/2579c14bac2de4ec160dae8babfa182e.png)
linux中bash命令
Linux find
command provides a lot of features to search and filter files in file system. One of the most popular and useful feature of find
command is exec
option. exec
option is used to to run commands with founded search results.
Linux find
命令提供了许多功能来搜索和过滤文件系统中的文件。 exec
选项是find
命令最流行和最有用的功能之一。 exec
选项用于运行具有已创建搜索结果的命令。
Linux Find Command With Examples
运行命令(Run Command)
We will with a simple example where we will just provide single command to run with results. We will use -exec
. In this example we will just print founded file names to the terminal. We will search for files with txt
extension and print their names.
我们将以一个简单的示例为例,在该示例中,我们将仅提供单个命令以运行结果。 我们将使用-exec
。 在此示例中,我们将仅将建立的文件名打印到终端。 我们将搜索带有txt
扩展名的文件并打印其名称。
$ find . -type f -name "*.txt" -exec echo {} \;
![Run Command](https://i-blog.csdnimg.cn/blog_migrate/8db2dfcdf0ec545f5b019e1f7514cceb.png)
运行多个命令(Run Multiple Commands)
We generally run single command to accomplish tasks. But in some cases we need to run multiple commands about the search result. We will specify multiple -exec
options and command. In this example we will write the file name and grep for address
.
我们通常运行单个命令来完成任务。 但是在某些情况下,我们需要针对搜索结果运行多个命令。 我们将指定多个-exec
选项和命令。 在此示例中,我们将为address
写入文件名和grep。
$ find . -type f -name "*.txt" -exec echo {} \; -exec grep "address" {} \;
![Run Multiple Commands](https://i-blog.csdnimg.cn/blog_migrate/c34617e7c4298480ce0919395703a51d.png)
删除文件(Remove Files)
We can remove files by using -exec
option with rm
bash command. In this example we will also provide the recursive and force options for rm
command. In this example we will delete all files with txt
extension.
我们可以通过在rm
bash命令中使用-exec
选项来删除文件。 在此示例中,我们还将为rm
命令提供递归和强制选项。 在此示例中,我们将删除所有带有txt
扩展名的文件。
$ find . -type f -name "*.txt" -exec rm -Rf {} \;
更改文件所有权 (Change Ownership Of Files)
We can change ownership of files with -exec
command. We will use chown
command too. In this example we will change the ownership to the user ismail
which have txt
extension.
我们可以使用-exec
命令更改文件的所有权。 我们也将使用chown
命令。 在此示例中,我们将所有权更改为具有txt
扩展名的用户ismail
。
$ find . -type f -name "*.txt" -exec chown ismail {} \;
翻译自: https://www.poftut.com/use-find-exec-command-linux-bash/
linux中bash命令