Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Dec 4 '11 at 22:55

This question came from our site for professional and enthusiast programmers.

4 Answers 4

up vote 5 down vote accepted

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.

share|improve this answer

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 :- how much should I worry about argument list too long?

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

share|improve this answer

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

share|improve this answer
    
Could you provide an explanation on how to use it? –  Simon Sheehan Dec 5 '11 at 0:30

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.