How to delete files owned by a specific user in Linux/Ubuntu


Linux/Ubuntu is a multiple user based operating system, i.e., it allows one or more unique users to log in and perform tasks without affecting the other user account. There might be a case when a user account is compromised. In that situation, the security and privacy of other users in that system might get affected. Linux offers a powerful command line that enables a user to filter out files owned by a specific user and groups.

This article will demonstrate different ways of deleting files owned by a specific user in Linux/Ubuntu. This article is organized as follows:

  • How to Delete Files Owned by a Specific/Particular User in Linux/Ubuntu?
  • How to Delete a Single File Owned by a Specific/Particular User in Linux/Ubuntu?
  • How to Delete Files Owned by Specific Users and a Specific/Particular Group in Linux/Ubuntu?
  • How to Delete Files Owned by a Specific/Particular Group in Linux/Ubuntu?
  • Bonus Tip 1: How to Remove a User Along With all Related Files?
  • Bonus Tip 2: How to Delete Directories Owned by Specific/Particular User in Linux/Ubuntu?

How to Delete Files Owned by a Specific/Particular User in Linux/Ubuntu?

If a user needs to remove multiple files owned by a specific user, such as linuxgenie, run the following command:

$ sudo find / -user linuxgenie -type f -delete

Where:

  • find: it is a keyword
  • user: it specifies the user name
  • type f: finds files only
  • delete: it deletes found files.

The above output indicates that all the files owned by user linuxgenie, i.e., File1.txt, File2.txt, and File3.txt are deleted.

How to Delete a Single File Owned by a Specific/Particular User in Linux/Ubuntu?

If a user needs to remove a single file, such as File1.txt owned by user linuxgenie. To do so, first, find all files of the user linuxgenie via the following command:


The find command will list all the files of the user linuxgenie. Next, remove the file File1.txt by the rm command as follows:

The above output indicates that File1.txt owned by user linuxgenie is deleted.

How to Delete Files Owned by Specific Users and a Specific/Particular Group in Linux/Ubuntu?

To delete all files owned by the user linuxgenie and group root, run the following command:

$ sudo find / -user linuxgenie -group root -type f -delete

The above output indicates that all the files owned by user linuxgenie of group root, i.e., File2.txt and File3.txt are deleted.

How to Delete Files Owned by a Specific/Particular Group in Linux/Ubuntu?

To delete files owned by a group linuxgenie, run the following command:

$ sudo find / -group linuxgenie -type f -delete

The above output indicates that all the files of group linuxgenie, i.e., File1.txt, File2.txt, and File3.txt are deleted.

Bonus Tip 1: How to Remove a User Along With all Related Files?

In case, a user is compromised, a user can be deleted, resulting in the removal of all the files associated with that user. To delete a user linuxgenie, run the following command:

$ sudo deluser –remove-all-files linuxgenie

Where:

  • deluser: Command line utility
  • –remove-all-files: removes all the files from the system of user linuxgenie.

The system lists all files that are being deleted at run time:

The above output indicates that the owner is removed. Hence, all the files owned by user linuxgenie are also deleted.

Bonus Tip 2: How to Delete Directories Owned by Specific/Particular User in Linux/Ubuntu?

To delete all directories owned by the user linuxgenie, run the following command:

$ sudo find / -user linuxgenie -type d -delete

Where:

  • find: it is a keyword
  • user: it specifies the user name
  • type d: finds directories only
  • delete: it deletes found directories.

The above output indicates that all the directories owned by user linuxgenie, i.e., Directory1, Directory2, and Directory3 are deleted.

Conclusion

To delete files owned by a specific user, execute the “sudo find / -user <user> -type f -delete” command. On the other hand, to delete files owned by specific users and a specific group, execute the “sudo find / -user <user> -group <group> -type f -delete” command. This article demonstrated different ways of deleting files owned by a specific user in Linux/Ubuntu.

Print Friendly, PDF & Email