Skip to content
Advertisement

Use find command with -perm and -maxdepth [closed]

When I Enter this command:

$ find . -perm 777 -maxdepth 1

The following error occures:

find: warning: you have specified the -maxdepth option after a non-option argument -perm, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it). Please specify options before other arguments.

What does that mean?

Advertisement

Answer

The order of find arguments is very important, because they are evaluated as a boolean expression left-to-right with short circuiting:

# Deletes *.tmp files
find . -name '*.tmp' -delete

# Deletes ALL file, because -delete is performed before -name
find . -delete -name '*.tmp'

However, -maxdepth does not behave like this. -maxdepth is an option that changes how find works, so it applies the same no matter where it’s placed:

# Deletes all '*.tmp' files, but only in the current dir, not subdirs
find . -maxdepth 1 -name '*.tmp' -delete  

# Deletes all '*.tmp' files, still only in the current dir
find . -name '*.tmp' -delete -maxdepth 1 

Since you put the -maxdepth 1 after a -perm 777, it looks like you are trying to make -maxdepth only apply to certain files. Since this is not possible, find prints this warning.

It suggests that you rewrite it into find . -maxdepth 1 -perm 777 to make it clear that you intended -maxdepth to apply to everything.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement