-   Linux - Newbie (
-   -   deleting *all* in current directory (bash) (

jr_bob_dobbs 02-27-2017 04:53 PM

deleting *all* in current directory (bash)
While I am not really a newb any more, this question seems newbie-ish to me so here it goes...

In a directory, if one types, "rm -rf *", everything is removed ... except for directories and files beginning with ".".

If one types "rm -rf ." again, one gets an error message and as before, directories and files beginning with "." are not deleted.

Short of a kludge involving sub-shells and the use of find, is there a good simple safe way to have the effect of "rm -rf" in the current directory *plus* the removal of files beginning with "."?

Thank you.

notKlaatu 02-27-2017 05:03 PM

First of all, I don't recommend the direct use of `rm`. I think it's a dangerous tool that should be avoided in favour of a good command line trash application, like trashy or trash-cli.

That said, you're looking for some expansion. Using `ls` instead of `rm` for copy-pasta safety:


ls {.,}*
That's a brace, dot, comma, brace, asterisk. It means anything with a dot, or anything with nothing.

hydrurga 02-27-2017 05:08 PM

If you're using Bash:

shopt -s dotglob

ensures that the * glob will subsequently match both unhidden and hidden files.

Habitual 02-27-2017 05:21 PM

I have similar trouble with this particular situation muh damn self.
I check my shit with

echo rm ...
b/c one rm is too many only "once".

Just sayin'

r3sistance 02-27-2017 05:30 PM

don't use rm for this, use find


# mkdir -p test/test
# echo "test" > test/.test.txt
# echo "test" > test/test.txt
# echo "test" > test/test/test.txt
# cd test
# find . -type f
# find . -maxdepth 1 -type f
# find . -maxdepth 1 -type f -delete
# find . -type f
# find . -type f -delete
# find . -type f

Find has more predictable behavior here, so I would say it is my preferred option. maxdepth can be used if you don't want to delete anything in any subdirectories. Else it'll catch all files in all subdirectories too.

Jjanel 02-28-2017 04:32 AM

* .??*
Yes, be careful with this (I just accidentally deleted my .bashrc in experimenting with this)!

For quick simplicity, I add/use: .??*
Tho/but that will not get -single_letter- 'hidden' files, like .a
To check if there's any such [1letter dot-files], I then do: ls -d .?

Take a few minutes to look into the -d and how `echo` acts 'like' a `ls -d`

An important point here is to NOT 'hit' [match in `rm`] . and ..
(the current dir and it's 'parent')

Getting a bit more 'theoretical' here, note how .?* works
(-different- than in grep/regex!)
? matches any one character; * any zero or more; . a dot itself
echo .?*

jpollard 02-28-2017 05:53 AM

I tend to use:

rm -rf .[a-zA-Z0-9]*

or just

rm -rf .[a-z]*

pan64 02-28-2017 06:16 AM


cd ..
rm -rf <dir>
mkdir <dir>

Jjanel 02-28-2017 11:18 PM

Oh, it helps to ReallyRead#1:redface:

a good simple safe way to have the effect of "rm -rf" in the current directory *plus* the removal of files beginning with "."?
While I realized #7 was more accurate than my #6, this #8 is *pure perfect GENIUS* :D

(I was about to suggest: cd ..; rm -fir $OLDPWD; mkdir $OLDPWD; cd $OLDPWD
but then realized that 'esoterically theoretical' != 'good simple safe' :D )

OP, can you please use ThreadTools at top, to mark this thread as "[SOLVED]". Thanks!

John VV 03-01-2017 12:40 AM

use " srm" it may or may not be installed but the source is on sourceforge

BUT as always use CAUTION

Habitual 03-01-2017 07:02 AM


alias rm="rm -i"

jr_bob_dobbs 03-01-2017 05:38 PM

Wow, not as simple a question as I had thought. Thank you, everyone, for the replies.

Now at some point, when I feel brave, to pick which method to try first. ;)

jr_bob_dobbs 03-01-2017 05:40 PM


Originally Posted by Jjanel (Post 5677397)
OP, can you please use ThreadTools at top, to mark this thread as "[SOLVED]". Thanks!

Is it solved? :p I have to figure out each of the replies now and then attempt things etc. before such a determination can be made. Why the rush?

rknichols 03-01-2017 05:53 PM


Originally Posted by jr_bob_dobbs (Post 5677800)
Wow, not as simple a question as I had thought.

No, not simple at all unless you make use of the shell's "dotglob" option. Without that, to get everything:

rm -r .[^.]* ..?* *
The first matches every name that begins with a dot followed by any character except a dot. The second matches any name that begins with ".." followed by at least one more character. The final "*" matches all the non-dot files. It's messy enough to make you at least consider something else.

John VV 03-01-2017 09:40 PM

that is why i like " srm "

srm -h
Usage: srm [OPTION]... [FILE]...
Overwrite and remove (unlink) the files. By default use the 35-pass Gutmann
method to overwrite files.

  -d, --directory      ignored (for compatability with rm(1))
  -f, --force          ignore nonexistant files, never prompt
  -i, --interactive    prompt before any removal
  -x, --one-file-system do not cross file system boundaries
  -s, --simple          overwrite with single pass using 0x00 (default)
  -P, --openbsd        overwrite with three passes like OpenBSD rm
  -D, --dod            overwrite with 7 US DoD compliant passes
  -E, --doe            overwrite with 3 US DoE compliant passes
  -G, --gutmann        overwrite with 35-pass Gutmann method
  -C, --rcmp            overwrite with Royal Canadian Mounted Police passes
  -r, -R, --recursive  remove the contents of directories
  -v, --verbose        explain what is being done
  -h, --help            display this help and exit
  -V, --version        display version information and exit


this removes everything in that forled including folders and uses a 1 overwrite of ZEROS


:~> srm -sr *

All times are GMT -5. The time now is 11:01 PM.