Find and Delete Folder in Linux [non root user]

3

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.

Kenyana

Posted 2011-11-28T16:21:49.313

Reputation:

Answers

6

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.

Till Helge

Posted 2011-11-28T16:21:49.313

Reputation: 176

1

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 :- https://stackoverflow.com/questions/7037618/how-much-should-i-worry-about-argument-list-too-long/7037640#7037640

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

ajreal

Posted 2011-11-28T16:21:49.313

Reputation: 438

0

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

Oz123

Posted 2011-11-28T16:21:49.313

Reputation: 583

Could you provide an explanation on how to use it? – Simon Sheehan – 2011-12-05T00:30:28.843

0

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.

gustavotkg

Posted 2011-11-28T16:21:49.313

Reputation: