Bash查找命令详细输出


46

有没有办法告诉bash find命令输出它在做什么(详细模式)?

例如对于命令: find /media/1Tb/videos -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \;输出:

Found /media/1Tb/videos/102, executing rm -rf /media/1Tb/videos/102
...

Answers:


62

您可以使用进行炮制-printf,但最简单的方法就是坚持-print到底。这将显示已成功删除的内容。


使用时find,此答案可以应用于任何事物,所以请竖起大拇指
Alex

我的find爱又增加了一点。谢谢:)
达拉格(Darraghh Enright)2012年

8
对我来说,使用“ -exec rm -vf {} \;” 工作得更好。
djangofan 2012年

1
真好!也可与-delete一起使用: find -L . -type l -delete -print
runlevel0

19

rm -vf用于详细的rm输出怎么样?

$ touch file1 file2 file3
$ find . -name "file?" -exec rm -vf {} \;
removed `./file2'
removed `./file3'
removed `./file1'

的详细选项rm很酷,但是如果我将其替换为其他内容,我将无法再看到正在处理的文件(除非echo在内部使用-exec
Alex

8

一种替代方法是让命令由sh -x以下命令执行:

$ find . -type f -print0 | xargs -0 -n1 echo rm | sh -x
+ rm ./file1
+ rm ./file2
+ rm ./file3

shell debug模式将很清楚发生了什么。谢谢
sdkks 18/09/27

1

find -D xxxx在某些情况下也有帮助。

 $ find -D help
 Valid arguments for -D:
 help       Explain the various -D options
 tree       Display the expression tree
 search     Navigate the directory tree verbosely
 stat       Trace calls to stat(2) and lstat(2)
 rates      Indicate how often each predicate succeeded
 opt        Show diagnostic information relating to optimisation
 exec       Show diagnostic information relating to -exec, -execdir, -ok and -okdir

以下是两个示例find -D search

使用RHEL 6.3(findv4.4):

$ mkdir -p aa/bb
$ touch aa/11 aa/22 aa/33 aa/bb/44 aa/bb/55
$ find -D search aa -type f -delete
consider_visiting: fts_info=FTS_D , fts_level= 0, prev_depth=-2147483648 fts_path=`aa', fts_accpath=`aa'
consider_visiting: fts_info=FTS_D , fts_level= 1, prev_depth=0 fts_path=`aa/bb', fts_accpath=`bb'
consider_visiting: fts_info=FTS_NSOK, fts_level= 2, prev_depth=1 fts_path=`aa/bb/55', fts_accpath=`55'
consider_visiting: fts_info=FTS_NSOK, fts_level= 2, prev_depth=2 fts_path=`aa/bb/44', fts_accpath=`44'
consider_visiting: fts_info=FTS_DP, fts_level= 1, prev_depth=2 fts_path=`aa/bb', fts_accpath=`bb'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path=`aa/22', fts_accpath=`22'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path=`aa/33', fts_accpath=`33'
consider_visiting: fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path=`aa/11', fts_accpath=`11'
consider_visiting: fts_info=FTS_DP, fts_level= 0, prev_depth=1 fts_path=`aa', fts_accpath=`aa'
$ find --version
find (GNU findutils) 4.4.2
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX FTS() CBO(level=0)

使用Cygwin 1.7(find4.5):

$ mkdir -p aa/bb
$ touch aa/11 aa/22 aa/33 aa/bb/44 aa/bb/55
$ find -D search aa -type f -delete
consider_visiting (early): 'aa': fts_info=FTS_D , fts_level= 0, prev_depth=-2147483648 fts_path='aa', fts_accpath='aa'
consider_visiting (late): 'aa': fts_info=FTS_D , isdir=1 ignore=1 have_stat=1 have_type=1
consider_visiting (early): 'aa/11': fts_info=FTS_NSOK, fts_level= 1, prev_depth=0 fts_path='aa/11', fts_accpath='11'
consider_visiting (late): 'aa/11': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/22': fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/22', fts_accpath='22'
consider_visiting (late): 'aa/22': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/33': fts_info=FTS_NSOK, fts_level= 1, prev_depth=1 fts_path='aa/33', fts_accpath='33'
consider_visiting (late): 'aa/33': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb': fts_info=FTS_D , fts_level= 1, prev_depth=1 fts_path='aa/bb', fts_accpath='bb'
consider_visiting (late): 'aa/bb': fts_info=FTS_D , isdir=1 ignore=1 have_stat=1 have_type=1
consider_visiting (early): 'aa/bb/44': fts_info=FTS_NSOK, fts_level= 2, prev_depth=1 fts_path='aa/bb/44', fts_accpath='44'
consider_visiting (late): 'aa/bb/44': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb/55': fts_info=FTS_NSOK, fts_level= 2, prev_depth=2 fts_path='aa/bb/55', fts_accpath='55'
consider_visiting (late): 'aa/bb/55': fts_info=FTS_NSOK, isdir=0 ignore=0 have_stat=0 have_type=1
consider_visiting (early): 'aa/bb': fts_info=FTS_DP, fts_level= 1, prev_depth=2 fts_path='aa/bb', fts_accpath='bb'
consider_visiting (late): 'aa/bb': fts_info=FTS_DP, isdir=1 ignore=0 have_stat=1 have_type=1
consider_visiting (early): 'aa': fts_info=FTS_DP, fts_level= 0, prev_depth=1 fts_path='aa', fts_accpath='aa'
consider_visiting (late): 'aa': fts_info=FTS_DP, isdir=1 ignore=0 have_stat=1 have_type=1
$ find --version
find (GNU findutils) 4.5.11
Packaged by Cygwin (4.5.11-1)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Eric B. Decker, James Youngman, and Kevin Dalley.
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS(FTS_CWDFD) CBO(level=2)

1

@hlovdav的回答对我来说足够了,但我做了一些修改以供自己使用

find . -name 'application*.yml' -print0 | xargs -0 -I %% bash -c 'rm -v "$1"' -- "%%"

说明

  1. pattern
  2. null分开打印,如果文件名中包含空格或不寻常字符,则请务必注意
  3. xargsnull分开读取,将每个记录占位符设置为,%% 这还确保每次仅使用一个参数
  4. bash 命令,单行,任何内容都必须用单引号引起来 '
  5. --意思是我在此之后所做的任何事情都不是xargsbash选项,而是我的单行脚本的位置参数
  6. 占位符通过单引号给出,单引号或双引号无关紧要。如果使用双引号,则也可以插入外壳变量。
  7. 里面bash的脚本,您可以访问%%$1,位置参数号#1

注意:您可以%%进行任何更改,只需确保您无需将其用于占位符即可。使用美元$@可能不是很好,除非它是双@@@

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.