62

Is there way to tell the bash find command to output what it is doing (verbose mode)?

For example for the command: find /media/1Tb/videos -maxdepth 1 -type d -mtime +7 -exec rm -rf {} \; to output:

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

5 Answers 5

92

You could concoct something with -printf, but the easiest is just to tack on -print on the end. This will show what was successfully deleted.

5
  • this answer can be applied to anything when using find, so thumbs up
    – Alex
    Oct 7, 2011 at 8:09
  • my find love grew another just another little bit. thanks :) Jun 14, 2012 at 22:59
  • 10
    For me, using "-exec rm -vf {} \;" worked better.
    – djangofan
    Oct 24, 2012 at 20:50
  • 5
    Nice! Works with -delete too: find -L . -type l -delete -print
    – runlevel0
    Dec 13, 2016 at 10:16
  • Here is an example to let the find output look like rm -v's output: find "$path" -delete -printf "removed '%p'\n"
    – mgutt
    Dec 7, 2023 at 13:35
26

How about just using rm -vf for verbose rm output.

$ touch file1 file2 file3
$ find . -name "file?" -exec rm -vf {} \;
removed `./file2'
removed `./file3'
removed `./file1'
1
  • the verbose option for rm is cool but if I replaced it with something else I can no longer see what files are being worked on (unless I use echo inside -exec)
    – Alex
    Oct 7, 2011 at 8:10
7

An alternative is to let the commands be executed by sh -x:

$ find . -type f -print0 | xargs -0 -n1 echo rm | sh -x
+ rm ./file1
+ rm ./file2
+ rm ./file3
1
  • shell debug mode will be clear enough on what has happened. Thanks
    – user235422
    Sep 27, 2018 at 5:00
2

There is also find -D xxxx that could help in some cases.

 $ 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

Below are two examples of find -D search:

Using RHEL 6.3 (find v4.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)

Using Cygwin 1.7 (find 4.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)
2

@hlovdav's answer was enough for me but I did some modifications for my own use

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

Explanation

  1. find
  2. pattern
  3. Print null separated, important if you have filenames with spaces or unusual characters in it
  4. xargs read null separated, set each record placeholder to %% This also ensures every time it uses only one argument
  5. bash command, one-liner, anything goes inside, must be single quoted '
  6. -- meaning anything I do after this is not xargs or bash options but positional parameters to my one-liner script
  7. Placeholder is given as single argument by quoting it, single or double quote doesn't matter. If you use double quotes, you can insert shell variables, too.
  8. Inside bash script, you can access %% as $1, positional argument No #1

Note: You can change %% with anything, just make sure you don't need to use it for anything other than a placeholder. Using dollar $ or @ might not be good, unless it's double @ like @@.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .