Find and Delete Folder in Linux [non root user]

Posted on

Problem :

I would like to search within my /home/user for a specific folder name and delete it and all its contents. There is a posibility that we would find multiple occurences of the same folder across many folders within /home/user

How do I go about this:
Note: Using PuTTY.

Solution :

Try it like this:

find /home/user -type d -iname "searchdir" -exec rm -ir "{}" ;

The find will search /home/user for all directories containing searchdir and execute rm -ir for all of them. It will prompt you for every directory whether it should remove it or not (the -i after rm does that).

Oh…and you might want to add -d 1 to find if it should only search in the upmost hierarchy level.

The command to find the folder with certain name is :-

find -type d -name "YOUR_NAME" -print0 | xargs -r0 rm -rf

The above command can avoid argument list too long :-

Lastly, if you have non-root user access, you likely getting permission denied

use the command “find”, search for a tutorial on it. a very powerful tool you should know.

Well, using

find /home/user -name 'dir_name' -type d

will bring all directories matching dir_name. So you can use xargs and delete it recursively.

find /home/user -name 'dir_name' -type d | xargs rm -r

But, before running above command, check if find command returns all results properly. If you allows me a tip: instead of using rm -r, use mv and move your files to another folder, so nothing will be lost if problems occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *