0

I'm trying to filter out files using find, however am having difficulty. Does anyone know the correct command to filter out "Untitled.rtf" using file permissions? BSD like?

-rwxr-xr-x@ 1 X staff 368 Jan 24 03:02 Untitled.rtf* -rw------- 1 X staff 0 Jan 24 02:37 t -r-------- 1 X staff 0 Jan 24 02:40 t2 drwx------ 2 X staff 64 Jan 24 02:52 t3/ 

I can find the file I want to filter out:

$ find . \( -perm -g+r -o -perm -g+w -o -perm -g+x -o -perm -o+r -o -perm -o+w -o -perm -o+x \) -exec ls -l {} \; -rwxr-xr-x@ 1 X staff 368 Jan 24 03:02 ./Untitled.rtf 

Now, I try this find command using ! (NOT) and -a (AND), however it doesn't work as intended to remove Untitled.rtf from the resulting list:

$ find . \( -perm -000 -a ! \( -perm -g+r -o -perm -g+w -o -perm -g+x -o -perm -o+r -o -perm -o+w -o -perm -o+x \) \) -exec ls -l {} \; -rwxr-xr-x@ 1 X staff 368 Jan 24 03:02 Untitled.rtf -rw------- 1 X staff 0 Jan 24 02:37 t -r-------- 1 X staff 0 Jan 24 02:40 t2 drwx------ 2 X staff 64 Jan 24 02:52 t3 -r-------- 1 X staff 0 Jan 24 02:40 ./t2 -rw------- 1 X staff 0 Jan 24 02:37 ./t 
2
  • Are you certain that what you are seeing is not the output of ls -l .? There is no -type test. Commented Jan 24 at 9:31
  • What criteria do you want to apply to filter out the file? It looks like it's "group or other have any permissions set" Commented Jan 24 at 11:20

2 Answers 2

3

I'm assuming your criterion for avoiding a file/directory is that it has any permission bits set for group or other. You can use either of these constructs to avoid matching such files:

find <path> ! ( <permissions_mask_set> ) -exec ls -ld {} ; find <path> ( <permissions_mask_set> ) -o -exec ls -ld {} ; 

Taking the first one of these because that matches your own example, it would become:

find . \! \( -perm -001 -o -perm -002 -o -perm -004 -o -perm -010 -o -perm -020 -o -perm -040 \) -exec ls -ld {} \; 

For systems with GNU find the permissions mask can be much simplified:

find . \! -perm /077 -exec ls -ld {} \; 

The reason your own attempt is showing everything, including your excluded files, is because one of the values passed to the -exec is a command that looks (partially) like this

ls -l . 

If you run that at the command line you'll get all the (non-"dot") files/directories in the current directory listed - including any you want excluded. The key is either to use the -d flag to avoid listing the contents of directory, or to qualify the find command either looking only for files or excluding directories.

1

On Solaris (which BTW is no longer BSD-based), you should be able to use GNU find where -perm accepts a /bitset which evaluates to true if any of the file permission bits are in bitset.

/usr/gnu/bin/find . ! -perm /77 -ls 

Or

/usr/gnu/bin/find . ! -perm /go=rwx -ls 

Would return the files where none of the ---rwxrwx bits are set.

Or you could use zsh which should be installed by default on Solaris as long as you're not using a stripped down flavour:

ls -ld -- **/*(Df00) 

Or:

ls -ld -- **/*(Df-77) 

Or:

ls -ld -- **/*(Df[go=]) 

Or:

ls -ld -- **/*(D^AIERWX) 

From another shell:

zsh -c 'ls -ld -- **/*(Df[go=])' 

For instance.

2
  • I don't think /77 is an argument in Solaris 10. Only leading -. How is it accomplished? Commented Jan 24 at 15:43
  • /77 would work with GNU find as I said. See Solaris default install (user tools) if GNU findutils are not installed on your system. Commented Jan 24 at 16:28

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.