删除嵌套的隐藏文件与egrep的和xargs的 [英] Delete nested hidden files with egrep and xargs

查看:140
本文介绍了删除嵌套的隐藏文件与egrep的和xargs的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除隐藏所有目录和文件,并递归删除它们。在当前目录级别唯一的隐藏目录是 .. 但每个几个目录中有几个隐藏文件(开始与 ._ )内他们。 LS-AR | egrep的'^ \\ \\ W +'| 将列出所有我想很容易的文件,但加入'| xargs的rm'`给我的RM错误没有这样的文件或目录。

我认为这意味着每一个我试图删除目录需要被附加与它的父目录和 / 。但是,也许我错了。

我如何更新此命令来删除这些文件?


解决方案

使用找到

 找到。型的F -name \\ * -exec RM -rf {} \\。

-exec 是白色的空间安全: {} 将文件路径(相传递给)作为一个单独的参数 RM

更好的是这样的:

 找到。 -name \\ *。-delete

(与感谢@ John1024)。第一种形式派生找到的每个文件的过程,而第二种形式没有。

的xargs 默认情况下是不是白色的空间安全:

  $触摸A \\ B
$找到。 -maxdepth 1 -name一个\\ \\ * | xargs的RM
RM:无法删除'./a':没有这样的文件或目录
RM:无法删除'B':没有这样的文件或目录

这是因为其在拆分空格它的输入提取文件名。我们可以用另外一个分隔符;从找人


  -print0
          真正;打印完整的文件名,在标准输出上,随后
          通过一个空字符(代替换行符那
          -print用途)。这允许包含换行符或文件名
          其他类型的空白是由亲PTED正确间$ P $
          处理该查找输出克。此选项对应
          的xargs -0选项。


所以:

 找到。型的F -name \\ * -print0。| xargs的-0 RM

I'm trying to remove all directories and files that are hidden and recursively remove them. the only hidden directories at current directory level are . and .. but each of several directories has several hidden files (begin with ._) within them. ls -aR | egrep '^\.\w+' | will list out all the files I want easy enough, but adding '| xargs 'rm'` gives me the rm error "No such file or directory".

I take this to mean that each directory I'm trying to remove needs to be appended with it's parent directory and /. But maybe I'm wrong.

How can I update this command to delete these files?

解决方案

Use find:

find . -type f -name .\* -exec rm -rf {} \;

The -exec is white-space safe: {} will pass the file path (relative to .) as a single argument to rm.

Better is this:

find . -name .\* -delete

(with thanks to @John1024). The first form spawns a process for each file found, whereas the second form does not.

xargs by default is not white-space safe:

$ touch a\ b
$ find . -maxdepth 1 -name a\ \* | xargs rm
rm: cannot remove ‘./a’: No such file or directory
rm: cannot remove ‘b’: No such file or directory

This is because it splits it's input on white-space to extract the file names. We can use another separator character; from man find:

  -print0
          True; print the full file name on the standard output,  followed
          by  a  null  character  (instead  of  the newline character that
          -print uses).  This allows file names that contain  newlines  or
          other  types  of white space to be correctly interpreted by pro‐
          grams that process the find output.  This option corresponds  to
          the -0 option of xargs.

So:

 find . -type f -name .\* -print0 | xargs -0 rm

这篇关于删除嵌套的隐藏文件与egrep的和xargs的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆