# Find all files owned by a particular user

**URL:** <https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449>\
**Category:** Ansible Project\
**Created:** [July 1, 2021, 7:20pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449 "2021-07-01T19:20:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 1, 2021, 7:20pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/1 "2021-07-01T19:20:39Z")

</div>

I’m trying to use the find module to search a path and return all files owned by a particular user. I can get the list of all files in the folder, but the data returned has both the path and the pw\_name for each file. How can I narrow the search to return only files where pw\_name is a certain user?

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 1, 2021, 9:09pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/2 "2021-07-01T21:09:03Z")

</div>

for instance using selectattr.

"{{ fs.files|selectattr('pw\_name', 'equalto', 'some\_user')|list }}"

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 2, 2021, 12:13pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/3 "2021-07-02T12:13:26Z")

</div>

Thank you for that suggestion! Worked perfectly! I appreciate it very much.

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 2, 2021, 2:17pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/4 "2021-07-02T14:17:35Z")

</div>

One other question on this: How can I use a variable as the third parameter? I’m hoping to pass in a user name and match that in the check, but I’m getting an error when I run it.

vars:

- user\_to\_remove: ‘user1’

The fact is now set as: “{{ fs.files | selectattr(‘pw\_name’,‘equalto’,{{ user\_to\_remove }}) | list }}”

The error is: template error while templating string: expected token ‘:’, got ‘}’

Any ideas?  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 2, 2021, 2:26pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/5 "2021-07-02T14:26:53Z")

</div>

[https://docs.ansible.com/ansible/latest/reference\_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names](https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#when-should-i-use-also-how-to-interpolate-variables-or-dynamic-variable-names)

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 2, 2021, 2:33pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/6 "2021-07-02T14:33:11Z")

</div>

Awesome! Thanks for that. Got it to work using the lookup function:  
“{{ fs.files | selectattr(‘pw\_name’,‘equalto’,lookup(‘vars’, ‘user\_to\_remove’)) | list }}”

Thanks again!  
Harry

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [July 2, 2021, 2:52pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/7 "2021-07-02T14:52:27Z")

</div>

Hii

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [July 2, 2021, 3:33pm UTC](https://forum.ansible.com/t/find-all-files-owned-by-a-particular-user/35449/8 "2021-07-02T15:33:25Z")

</div>

Yes, that also worked. The use of the brackets in these types of situations always trips me up.

Thanks,  
Harry
