From terminal window:
When I use the rm
command it can only remove files.
When I use the rmdir
command it only removes empty folders.
If I have a directory nested with files and folders within folders with files and so on, is there any way to delete all the files and folders without all the strenuous command typing?
If it makes a difference, I am using the mac bash shell from terminal, not Microsoft DOS or linux.
Would remove everything (folders & files) in the current directory.
But be careful! Only execute this command if you are absolutely sure, that you are in the right directory.
Yes, there is. The
-r
option tellsrm
to be recursive, and remove the entire file hierarchy rooted at its arguments; in other words, if given a directory, it will remove all of its contents and then perform what is effectively anrmdir
.The other two options you should know are
-i
and-f
.-i
stands for interactive; it makesrm
prompt you before deleting each and every file.-f
stands for force; it goes ahead and deletes everything without asking.-i
is safer, but-f
is faster; only use it if you're absolutely sure you're deleting the right thing. You can specify these with-r
or not; it's an independent setting.And as usual, you can combine switches:
rm -r -i
is justrm -ri
, andrm -r -f
isrm -rf
.Also note that what you're learning applies to
bash
on every Unix OS: OS X, Linux, FreeBSD, etc. In fact,rm
's syntax is the same in pretty much every shell on every Unix OS. OS X, under the hood, is really a BSD Unix system.-r "recursive" -f "force" (suppress confirmation messages)
Be careful!
So I was looking all over for a way to remove all files in a directory except for some directories, and files, I wanted to keep around. After much searching I devised a way to do it using find.
Essentially it uses regex to select the directories to exclude from the results then removes the remaining files. Just wanted to put it out here in case someone else needed it.