Linux Commands

How do you Delete Files from a User in Linux?

The Terminal or Command Line in Ubuntu makes the process of deleting files more flexible as with the terminal we can filter our search for files and look for specific types of files, or files owned by specific groups or users. Deleting files from a specific user plays a crucial role in the whole security and privacy of the system. If the user account is compromised, the files and directories can be deleted.

This article explains multiple methodologies to Delete a File from a User in Ubuntu.

How do you Delete Files from a User in Linux/Ubuntu?

The “find” command with “delete” is a valuable way to filter the files having the same user or group and then delete those files. Below are some methods explained in different scenarios for deleting a file from a User in Ubuntu.

Method 1: Delete Files from a Specific User in Ubuntu

To delete all the files owned by a specific user in Ubuntu, use the command:

find / -user <userName> -type f -delete

In our case, we have two files: “fileC.txt”, and “fileD.txt”. Both of these files have the owner “bob” and can be verified using the “getfacl” command:

sudo getfacl fileC.txt
sudo getfacl fileD.txt

As you can see both files have the owner “bob”:

To delete the files owned by the user “bob”, the command used will be:

find / -user bob -type f -delete

This will delete the files owned by the user “bob”, in our case, “fileC.txt” and “fileD.txt”

Method 2: Delete Files Owned by a Specific Group in Ubuntu

The find command can be used to delete files that belong to or owned by a specific group in Ubuntu:

find / -group <groupName> -type f -delete

In our case, the files “file1.txt”, “file2.txt”, and “file3.txt” have the same group “genz” and can be checked using the getfacl command:

getfacl file1.txt
getfacl file2.txt
getfacl file3.txt

To delete the files having the same group, in our case, “genz”, the command used will be:

sudo find / -group genz -type f -delete

This will delete the files with the group “genz”:

Method 3: Delete Files Owned by a Specific Group and User in Ubuntu

To delete a file or files owned by a specific group and a specific user in Ubuntu, the below-provided command is used:

find / -user <userName> -group <groupName> -type f -delete

In our case, the file “fileB.txt” and file “fileC.txt” have the user “bob”, group “genz” and can be checked using the command:

sudo ls -lR

This displays all the read/write, owner, and group information about the files in the directory:

To delete the files having the same user and group, in our case “bob” and “genz”, the command used will be:

sudo find / -user bob -group genz -type f -delete

This will delete the fileB.txt and fileC.txt:

Bonus Method: Delete Directory Owned by a Specific User in Ubuntu

The directory owned by a specific user can also be deleted in Ubuntu if the directory is empty using the command:

find / -user <userName> -type d -delete

The “-type d” means the type is a directory. In our case, the “FolderA” is owned by “bob” and can be checked using the “getfacl” command:

getfacl FolderA

The owner is “bob” and the group is “taha”:

The directories “AllTextFiles”, “dirB”, and “MultDir” are also owned by “bob” and can be checked using the “getfacl” command:

Now the command will be used to delete the empty directories having the owner “bob”:

sudo find / -user bob -type d -delete

This will delete the “AllTextFiles” and “dirB” Directories as both directories have no files inside:

Conclusion

Deleting files with specific Users in Ubuntu is easy using the Command Line or Terminal as it provides more flexibility in deleting specific files. Ubuntu uses the “delete” command along with the “find” command to filter files owned by specific users or specific groups and delete them. Directories with specific users can also be deleted using the “delete” with the “find” command. This article explains in detail the steps required to delete certain files with specific users.

Similar Posts