Table of Contents
In this article, I will take you through 5 Easy Examples to delete Files and Directories Owned by a Specific User and Group in Linux. In Linux based Systems, all the Files and directories are owned by Specific User and belong to some Group. If you are using Linux based Systems from long time, then you might have faced a situation where you need to delete all files and directories based on their ownership.
This task can be easily achieved by using Find command in Linux. You can find this tool to be freely available on almost all the Linux based Systems. I will explain the usage of this command through various examples to delete all the files and directories based on their ownership.
Delete Files and Directories Owned By Specific User and Group in Linux
Also Read: 6 Popular Methods to List All Running Services Under Systemd in Linux
Example 1: How to Delete Files Owned by Specific User in Linux
If you want to delete files owned by Specific User in Linux then you need to use below find command. In this example, we are deleting all the files owned by User centos
using find / -user centos -type f -exec rm -rf {} \;
command.
[root@localhost ~]# find / -user centos -type f -exec rm -rf {} \;
-user : File is owned by user. More information can be checked on find command Man Page.
-type : File is of type block, character, directory, regular file etc. More information can be checked on find command Man Page.
-exec : Execute command; true if 0 status is returned. More information can be checked on find command Man Page.
NOTE:
root
user to run all the below commands.You can use any user with sudo
access to run all these commands. For more information Please check Step by Step: How to Add User to Sudoers to provide sudo
access to the User.Example 2: How to Delete Files which belongs to Specific Group in Linux
If you want to delete all the files which belongs to a Particular Group then you need to use below find command. In this example, we are deleting all the files which belongs to group centos
using find / -group centos -type f -exec rm -rf {} \;
command.
[root@localhost ~]# find / -group centos -type f -exec rm -rm {} \;
-group : File belongs to group.
Example 3: How to Delete Files owned by Specific User and Specific Group in Linux
If you want to delete files owned by specific user and specific group in Linux then you need to use below find command. In this example we are deleting files owned by User centos
and group root
using find / -user centos -group root -type f -exec rm -rf {} \;
command.
[root@localhost ~]# find / -user centos -group root -type f -exec rm -rf {} \;
Example 4: How to Delete Directories owned by Specific User and Specific Group in Linux
If you want to delete directories owned by Specific User and Specific Group in Linux then you need to use below find command in Linux. In this example we are deleting directories owned by user centos
and group root
using find / -type d -user centos -group root -exec rm -rf {} \;
command.
[root@localhost ~]# find / -type d -user centos -group root -exec rm -rf {} \;
Example 5: How to Delete Directories which belongs to Specific Group in Linux
If you want to delete all the directories which belongs to Specific Group then you need to use below find command in Linux. In this example, we are deleting all the directories which belongs to group centos
using find / -type d -group centos -exec rm -rf {} \;
command.
[root@localhost ~]# find / -type d -group centos -exec rm -rf {} \;
Popular Recommendations:-
How to Install VIM Editor on Linux (RHEL/CentOS 7/8) Using 6 Easy Steps
6 Popular Examples to Find Files owned by Group(s) in Linux/Unix
Unix/Linux Find Files/Directories owned by a Particular User(5 Useful Examples)
Unix/Linux md5sum Command Examples to Verify Checksum
5 Best Methods to Extract .gz File in Linux using gunzip, gzip and tar tool
12 Popular Unix/Linux uname command examples(How to Get Kernel Version)
How to Transfer Files to AWS EC2 Instance Using WinSCP in 3 Easy Steps
6 Easy Steps to Install Sendmail command in Linux (RHEL/CentOS 7/8)
Easy Steps to Install GCC(C and C++ Compiler) on CentOS 7
How to Transfer Files to an AWS EC2 Instance Using WinSCP in 3 Easy Steps