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.

0

4 Answers 4

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.

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

0

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

1
  • Could you provide an explanation on how to use it? Dec 5, 2011 at 0:30
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.

You must log in to answer this question.