notaorb
notaorb

Reputation: 2180

find command: how to find all such files with a mask?

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?

-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

I want to filter out 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

Upvotes: 0

Views: 52

Answers (1)

jhnc
jhnc

Reputation: 16642

Your command already filters out Untitled.rtf.

However, it may not filter out ..

When . matches, the command ls -l . is run - which lists all the files inside the directory (the first four lines of the output in your question).

If you only want to list normal files (not directories), you can prepend a filter to find such as ! -type d or -type f.

Alternatively, you can add the -d option to ls so that only a single line for each matching directory appears instead of lines for each (non-dot) file inside those directories.

Upvotes: 0

Related Questions