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*
neither deleted directories matching that name within sub-directories.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
I ran into problems using find with -delete due to the intentional behavior of find (i.e. refusing to delete if the path starts with ./, which they do in my case) as stated in its man page:
-deleteDelete found files and/or directories. Always returns true. This executes from the current working directory as find recurses down the tree. It will not attempt to delete a filename with a “/” character in its pathname relative to “.” for security reasons.
Depth-first traversal processing is implied by this option.
Following symlinks is incompatible with this option.
Instead, I was able to just do
find . -type d -name 'received_*_output' -exec rm -r {} +
For your case, it seems that quoting the glob (asterisk, *) was the solution, but I wanted to provide my answer in case anyone else had the similar problem.
NOTE: Previously, my answer was to do the following, but @Wildcard pointed out the security flaws in doing that in the comments.
find . -type d -name 'received_*_output' | xargs rm -r
Method 3
Try:
shopt -s dotglob # using Bash printf '%sn' ./.Apple* # test #rm -rf ./.Apple*
Method 4
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.
Method 5
Simple command:
rm `find ./ -name '.Apple*'` -rf
Good luck!
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0