brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: find -user lists everything one account owns

Find
~/tips/tip-find-user-owned-files

find / -xdev -user olduser -ls 2>/dev/null

Lists the files that belong to one user, for a cleanup or before you remove the account.

Before you remove a user account or hand a folder over to someone else, it helps to know what that user owns. find can filter on the owner.

find / -xdev -user olduser -ls 2>/dev/null

-user takes a name or a numeric user id. -ls prints each hit with owner, size and date, a bit like ls -l, and 2>/dev/null hides the “Permission denied” lines when you’re not root. -group works the same way for groups.

A common use is fixing ownership after copying files with the wrong user, for example a web folder that should belong to www-data:

find /var/www/example -user root -exec chown www-data: {} +

I’d run it without the -exec part first and read the list. chown on the wrong files is a pain to undo, because find doesn’t remember who owned them before.

Numbers are handy in containers. A file created inside a container often belongs to a user id that doesn’t exist on the host, like 1001. find /srv/appdata -user 1001 still finds it, even though ls shows only the number. Once the account is deleted, its files are orphans, and you need -nouser to find them instead.

Further reading: find(1) manual page

More Find tips find / -maxdepth 1 -fstype proc find /backup -name '*.gz' ! -exec gzip -t {} \; -print 2>/dev/null find /srv/downloads -type f -size +100M -size -1G -exec ls -lh {} + find /srv/data -depth -print find /srv -xdev -samefile /srv/downloads/movie.mkv All Find tips (46) All topics

Link