1

What difference does it make when using the -print and -depth parameters in find command, given that they produce the same outcome:

/home/pkaramol/Desktop/testdir
$ find . 
.
./testfile3.txt
./testfile1.txt
./testfile4.txt
./testdir1
./testfile2.txt
./testdir2
/home/pkaramol/Desktop/testdir
$ find . -depth 
./testfile3.txt
./testfile1.txt
./testfile4.txt
./testdir1
./testfile2.txt
./testdir2
.
/home/pkaramol/Desktop/testdir
$ find . -depth -print
./testfile3.txt
./testfile1.txt
./testfile4.txt
./testdir1
./testfile2.txt
./testdir2
.
  • 1
    Only by coincidence. Try on an actual structure. – Ignacio Vazquez-Abrams Feb 25 '18 at 10:52
  • This question shows a bemusing lack of any basic research before posting. I ran the essential step 1, man find, and trivially searched for these switches to learn what -depth does and how it's a no-op here, and how -print is the default action and hence redundant in your examples. – underscore_d Feb 25 '18 at 15:10
  • 2
    @underscore_d Nitpick: -depth is actually not a no-op in this case. See the ordering of . compared to the other pathnames. – Kusalananda Feb 25 '18 at 15:57
10

-print will ensure that the current pathname is printed to standard output. Some flags turns off the default printing of pathnames (-exec for example).

-depth will cause a depth-first traversal of the file hierarchy, so that pathnames in directories without subdirectories are handled first (bottom up rather that top down). In your example, it makes little difference as you are working in directory without subdirectories, but notice that . is reported after the other pathnames when you use -depth (this is since . is the top-most directory to be searched, so its pathname is handled last with -depth and first without -depth).

It is useful to use -depth if one is deleting directories with find as you would get errors from trying to access already deleted directories without it.

As Scott points out in comments below, you would definitely need -depth when renaming directories too, or you would potentially not be able to traverse the directory structure at the same time as you're renaming directories in it.

The -delete flag turns on -depth by default.


Example: Delete all directories beneath the current directory whose names matches *deleteme (for example folder-deleteme), and also print the paths to the successfully deleted directories:

find . -depth -type d -name '*deleteme' -exec rm -rf {} ';' -print

Given the following directory structure,

$ tree
.
`-- folder-deleteme
    `-- another-deleteme
        `-- evenmore-deleteme

3 directories, 0 file

executing the above find command without -depth would result in

$ find . -type d -name '*deleteme' -exec rm -rf {} ';' -print
./folder-deleteme
find: ./folder-deleteme: No such file or directory

because find deletes the top-most folder-deleteme directory (and prints its path) and then tries to enter it to look for further directories to delete.

Also:

$ find .
.
./folder-deleteme
./folder-deleteme/another-deleteme
./folder-deleteme/another-deleteme/evenmore-deleteme

$ find . -depth
./folder-deleteme/another-deleteme/evenmore-deleteme
./folder-deleteme/another-deleteme
./folder-deleteme
.
  • 1
    Am I overlooking something? It looks like the end result of find . -type d -name '*deleteme' -exec rm -rf {} ';' is the same with or without -depth, except for the error message. Renaming directories is (IMHO) a better example of the need for -depth. – Scott Feb 25 '18 at 19:45
  • @Scott You are not overlooking anything. The error message is the only difference in this example. Yes, renaming directories is definitely another case where -depth is needed. I didn't consider that... I will add this (tomorrow, it's too late here now). I'll make a quick note about it now though. – Kusalananda Feb 25 '18 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.