在 bash 中查找和删除 .txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12604468/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:45:08  来源:igfitidea点击:

Find and delete .txt files in bash

bashunix

提问by Propagator

Possible Duplicate:
Command line: piping find results to rm

可能的重复:
命令行:管道查找结果到 rm

recently frigged up my external hard drive with my photos on it (most are on DVD anyway, but..) by some partition friggery.

最近用我的照片启动了我的外部硬盘驱动器(大多数都在 DVD 上,但是..)通过一些分区冰箱。

Fortunately I was able to put things back together with PhotoRec another Unix partition utility and PDisk.

幸运的是,我能够使用 PhotoRec 另一个 Unix 分区实用程序和 PDisk 将事情重新组合在一起。

PhotoRec returned over one thousand folders chalk full of anything from .txt files to important .NEF's.

PhotoRec 返回了超过一千个文件夹,里面装满了从 .txt 文件到重要的 .NEF 的任何内容。

So I tried to make the sorting easier by using unix since the OSX Finder would simply crumble under such requests as to select and delete a billion .txt files.

所以我试图通过使用 unix 使排序更容易,因为 OSX Finder 会在诸如选择和删除十亿个 .txt 文件的请求下简单地崩溃。

But I encounter some BS when I tried to find and delete txt files, or find and move all jpegs recursively into a new folder called jpegs. I am a unix noob so I need some assistance please.

但是当我尝试查找和删除 txt 文件,或者查找所有 jpeg 并将其递归移动到名为 jpegs 的新文件夹时,我遇到了一些 BS。我是一个 unix noob,所以我需要一些帮助。

Here is what I did in bash. (I am in the directory that ls would list all the folders and files I need to act upon).

这是我在 bash 中所做的。(我在 ls 将列出我需要采取行动的所有文件夹和文件的目录中)。

find . -name *.txt | rm

找 。-name *.txt | R M

or

或者

sudo find . -name *.txt | rm -f

须藤查找。-name *.txt | rm -f

So it's giving me some BS that I need to unlink the files. Whatever.

所以它给了我一些我需要取消链接文件的BS。任何。

I need to find all .txt files recursively and delete them preferably verbose.

我需要递归地查找所有 .txt 文件并删除它们,最好是详细的。

Thanks.

谢谢。

回答by dogbane

You can't pipe filenames to rm. You need to use xargsinstead. Also, remember to quote the file pattern ".txt"or the shell will expand it.

您不能将文件名通过管道传输到rm. 你需要xargs改用。另外,请记住引用文件模式,".txt"否则 shell 会展开它。

find . -name "*.txt" | xargs rm

回答by mana

find . -name "*.txt" -exec rm {} \;

回答by b3h3m0th

$ find  . -name "*.txt" -type f -delete