Search for files owned by particular owner and group

I am searchingfor files owned by particular owner and group in a particular directory including its sub-directories. I use

find <dir> -user <user> -group <group> -exec ls -l {} \;

It does not work completely. In the sense is a subdirectory is owned by 'user' and group 'group' then all files under that directory are also displayed even though they are owned by different user/group.

Any help?

find <dir> -user <user> -group <group> -exec ls -ld {} +

I think I did not make it clear. ld will show me only directory/sub-directories. I want all directory, sub-directories and files under the given directory have specified user/group.

if the (sub-) directory satisfies your conditions, your above command will exec ute an ls -l on it, which in turn does not discriminate against them. Does your find version provide the -ls action?

Rudic,

Yes, find version provides ls version. If I give just find command, I get

/export/home> find . -user root -group other
./sparcs_1
./sparcs_2

Now under two directories sparcs_1 and sparcs_2 there are few files which are owned by root/other and few which are not. But my earlier command lists all the files under sparcs_1 and sparcs_2.

find . -type f  -user root -group other  -ls

Will return only files meeting the selection criteria. Is that what you need? [-ls] is optional.

No, this does not work because none of the files/sub-directories under current directory is owned by root/other. Some of the files under sub-dirctory are owned by root/other. Consider following

MY_SERVER:/export/home/sdesai/tools> ls -lrt
total 2
-rwxr-xr-x   1 sdesai   mis           94 Mar  2 08:53 dif
drwxr-xr-x   2 sdesai   mis           96 Mar  9 14:41 sparcs_1
drwxr-xr-x   2 sdesai   mis           96 Mar  9 14:41 sparcs_2
MY_SERVER:/export/home/sdesai/tools> cd sparcs_1
MY_SERVER:/export/home/sdesai/tools/sparcs_1> ls -l
 -rwxrwxrwx  1 oracle  other       1925 Feb 26 04:58 File1.txt
-rwxrwxrwx   1 roor    other       1900 Feb 27 04:58 File2.txt

I want only File2.txt as it is owned by root/other

But you rejected the command that might do that on post #3.

ls -ld soham/
drwxr-xr-x  4 nobody  nobody  136 Mar  9 13:24 soham/
$ ls -l soham/
total 0
-rw-r--r--  1 root    staff     0 Mar  9 13:20 file1.txt
-rw-r--r--  1 daemon  operator  0 Mar  9 13:20 file2.txt

Looking for root/staff and displaying only that. Same scenario that you posted.

$ find . -user root -group staff -exec ls -ld {} +
-rw-r--r--  1 root  staff  0 Mar  9 13:20 ./soham/file1.txt

And I can go a bit deeper

$ ls -l
total 0
drwxr-xr-x  2 nobody  nobody   68 Mar  9 13:43 dir1
drwxr-xr-x  3 nobody  nobody  102 Mar  9 13:43 dir2
drwxr-xr-x  4 nobody  nobody  136 Mar  9 13:24 soham
$ find . -user root -group staff -exec ls -ld {} +
drwxr-xr-x  3 root  staff  102 Mar  9 13:44 ./dir2/subdir2
-rw-r--r--  1 root  staff    0 Mar  9 13:44 ./dir2/subdir2/file2.txt
-rw-r--r--  1 root  staff    0 Mar  9 13:20 ./soham/file1.txt

Notice that it did not display dir1 or dir2 but it found that the subdirectory subdir2 matches the request and also that the file ./dir2/subdir2/file2.txt matches the request. No other entries beside what you asked, is showing.

Thanks. It works if you go to that directory and start find command with fine .
But if it is find <dir> (i.e. instead of . give directory name) it does not work. But that's fine.

Thanks to all

It should not make a difference if you give it a dot for this directory or if you are outside of it and you give it the directory that you want to scan.

One outside test:

find test -user root -group staff -exec ls -ld {} +
drwxr-xr-x  3 root  staff  102 Mar  9 13:44 test/soham/dir2/subdir2
-rw-r--r--  1 root  staff    0 Mar  9 13:44 test/soham/dir2/subdir2/file2.txt
-rw-r--r--  1 root  staff    0 Mar  9 13:20 test/soham/soham/file1.txt

Or absolute path without concern for pwd.

$ find ~/playground/unix/test -user root -group staff -exec ls -ld {} +
drwxr-xr-x  3 root  staff  102 Mar  9 13:44 /Users/user/playground/unix/test/soham/dir2/subdir2
-rw-r--r--  1 root  staff    0 Mar  9 13:44 /Users/user/playground/unix/test/soham/dir2/subdir2/file2.txt
-rw-r--r--  1 root  staff    0 Mar  9 13:20 /Users/user/playground/unix/test/soham/soham/file1.txt