Searching for files in a target directory is a frequent things in your daily work for a Linux admin or linux newbie. Often, you must have in mind the linux command find firstly, then how to use this command to find all files and directories owned by a specific user or group? How to remove all files and directories owned by a particular user or group? How to find files which belong to a specific user or group? How to use find files by user id or group id in linux/unix operating system? This guide will provide you to solve all above questions.
The syntax is as followss:
find [options] <target path> -name [pattern] find [options] <target path> -user <username> find [options] < target path> -group <group name>
Find All Files And Directories Owned By A User
You should use find command with “-user” option to find all files which belong to a user under your target directory.
locate all the files which belong to “dtest” user under “home” directory, issue the following command:
find /home -user dtest
Output:
[root@osetc~]# find /home -user dtest /home/dtest /home/dtest/.bashrc /home/dtest/.bash_history /home/dtest/.mozilla /home/dtest/.mozilla/plugins /home/dtest/.mozilla/extensions /home/dtest/foo.txt /home/dtest/aaa /home/dtest/.bash_profile /home/dtest/.bash_logout
Find All Files And Directories Owned By A group
You can use the following command to find all files which belong to a group “dtest” under “/home” target directory, issue the following command:
find /home -group dtest
See Output:
[root@osetc~]# find /home -group dtest /home/dtest /home/dtest/.bashrc /home/dtest/.bash_history /home/dtest/.mozilla /home/dtest/.mozilla/plugins /home/dtest/.mozilla/extensions /home/dtest/foo.txt /home/dtest/aaa /home/dtest/.bash_profile /home/dtest/.bash_logout
Remove All Files Owned By User
issue the following command to remove all files owned by a user “dtest” under “/home” target directory, see follows:
#find /home -user dtest -exec rm -rf {} \;
Remove All Files Owned By Group
Similar with above command to remove all file owned by a group “dtest” under “home” directory, still use find command with “-group” and “-exec” options, see below command:
#find /home -group dtest -exec rm -rf {} \;
There is another way to remove all files owned by a user with “deluser” command, this command is trying to delete a user, if you give a “–remove-all-files” options to “deluser” command, then system not only delete the user, but only remove all of files and directories owned by that user. issue the following command:
#deluser --remove-all-file dtest
done…..