Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am working through SSH on a WD My Book World Edition. Basically I would like to start at a particular directory level, and recursively remove all sub-directories matching .Apple* - how would I go about that? I tried

rm -rf .Apple* and rm -fR .Apple*

both not deleting directories matching that name within sub-directories.

Any help would be greatly appreciated.

Thanks, Dany.

share|improve this question
add comment

4 Answers

up vote 16 down vote accepted

find is very useful for selectively performing actions on a whole tree.

find . -type f -name ".Apple*" -delete

Here, the -type f makes sure it's a file, not a directory, and may not be exactly what you want since it will also skip symlinks, sockets and other things. You can use ! -type d, which literally means not directories, but then you might also delete character and block devices. I'd suggest looking at the -type predicate on the man page for find.

To do it strictly with a wildcard, you need advanced shell support. Bash v4 has the globstar option, which lets you recursively match subdirectories using **. zsh and ksh also support this pattern. Using that, you can do rm -rf **/.Apple*. This is not POSIX-standard, and not very portable, so I would avoid using it in a script, but for a one-time interactive shell action, it's fine.

share|improve this answer
    
I can get find . -type d -name .Apple* to work - it lists all the folders. However, it fails when I add -delete at the end. It just comes back with the usage summary. It's running on BusyBox v1.1.1. Does that make a difference? –  DanyW Oct 30 '11 at 1:33
    
Make sure to either quote or escape the *, otherwise the shell will expand it, and find will not see it. And as I understood the question, you don't want to get the directories, so make sure to include ! before -type d to tell find not to match directories. –  Shawn J. Goff Oct 30 '11 at 4:54
    
Whoops... I was a bit ambiguous. I do want to delete just the directories. I will try quoting the wildcard name. –  DanyW Oct 30 '11 at 8:04
add comment

What is possible is that there is nothing wrong with your rm/find command, but that the user you are logged in with doesn't actually have delete permissions. Use ls -l to list things with their permissions, and you can identify who you are with the id and groups commands (either should be available). If you are the "wrong user" to do this, you would need to either change the permissions/ownership of the files, or switch to another user.

share|improve this answer
add comment

Try:

shopt -s dotglob           # using Bash
printf '%s\n' ./.Apple*    # test
#rm -rf ./.Apple*    
share|improve this answer
    
dotglob is useless here. See this page for an example of what dotglob does. –  amphetamachine Nov 21 '11 at 2:34
add comment

Simple command:

rm `find ./ -name '.Apple*'` -rf

Good luck!

share|improve this answer
3  
This is extremely error-prone, find has -exec key. –  Anton Barkovsky Nov 20 '11 at 14:46
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.