linux中bash命令_如何在Linux Bash中使用Find Exec命令?

linux中bash命令

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

Linux使用示例查找命令

运行命令(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 
Run Command 
运行命令

运行多个命令(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
Run Multiple Commands
运行多个命令

删除文件(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 txtextension.

我们可以通过在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 {} \;
LEARN MORE  Linux xargs Command Tutorial With Examples
了解更多带有示例Linux xargs命令教程

翻译自: https://www.poftut.com/use-find-exec-command-linux-bash/

linux中bash命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值