Score:0

Linux find command to return files AND owner of files NOT owned by specified user

ro flag

I have the following terminal command:

find /home/not_this/ \! -user not_this_user_account

The goal is to find all the files and directories not owned by the specified user and this version of the find command works great.

However I'd really like to return the owner of the returned files and directories as well.

So:

/home/not_this/test1.html
/home/not_this/test2.html

Would return this:

someone_else /home/not_this/test1.html
someone_else /home/not_this/test2.html

Or alternatively return this:

/home/not_this/test1.html someone_else
/home/not_this/test2.html someone_else

How do I use the find command to return both the files/directories not owned by the specified user as well as the owner of those files/directories?

As I don't consider myself well versed in the Linux terminal I'm agreeable to using a different command so long as it returns the desired results and doesn't require some sort of special condition (e.g. downloading and installing something that isn't on literally every Linux distribution).

Score:2
la flag

Most find implementations allow you to format their output with the printf function.
Consult your own systems man find for what formatting options are actually supported.

Using -printf '%u\t%p\n' gives me a TAB (\t) separated list with username %u (or UID number when that can't be resolved to a username) and the full filename including path %p. The newline \n is needed because otherwise you don't see one item per line.

find /home/not_this/ \! -user not_this_user_account -printf '%u\t%p\n' 

Gives a list such as:

diya    /home/bla
diya    /home/bla/file-1
diya    /home/blaa
diya    /home/blaa/file-2
diya    /home/blaa/images & screenshots
unbound /home/blaa/images & screenshots/Screenshot 2022-11-03 11:02.PNG
diya    /home/blaa/images & screenshots/image-2.jpg
diya    /home/blaa/images & screenshots/image-1.jpg
diya    /home/blaa/file-3
diya    /home/blaa/1
diya    /home/blaa/1/file.txt
John avatar
ro flag
I hate tabs, what is the syntax for spaces?
diya avatar
la flag
A space is not a special character in the `-printf format` section of [`man find`](https://man7.org/linux/man-pages/man1/find.1.html) so simply replace `\t` by one or more literal space characters `-printf '%u %p\n'`....
John avatar
ro flag
I think the first time I replaced it with a literal space I somehow screwed up the syntax though it's working perfectly now, thank you!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.