-3

This question already has an answer here:

rm -rf ./* deleted all content of current directory only partially.

Input:

MN> cd ~/mydir
MN> rm -rf ./*
MN> ll

Output:

-rw-r--r--  1 MN MN 1025 Aug  1 15:50 .file1
-rw-r--r--  1 MN MN 1025 Aug  1 15:50 .file2

Some files weren't deleted

marked as duplicate by jimmij, JigglyNaga, user88036, Stephen Kitt bash Oct 12 '18 at 9:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

4
rm -rf ./*

usually means “delete all the non-hidden files and directories in the current directory, recursively, without prompting, ignoring any nonexistent files”, it doesn’t mean “delete everything in the current directory”. (Its exact meaning depends on the shell’s globbing behaviour, not on rm.)

The simplest way to delete all the contents of a directory, without deleting the current directory itself, and without having to take into account the shell’s globbing settings, is to use find:

find -maxdepth 1 . -exec rm -rf {} +

or, if you have GNU find:

find . -delete

See also Proper way to delete contents of directory.

2

As the files were hidden ones (as they start with a dot), and rm (with -rf arguments) doesn't cover hidden files in Bash 4.x.x naturally, the files remained after the rm -rf ./* operation.

What I did eventually is to use the shpot Bash builtin.

shopt -s dotglob # Globb leading dots as well.
rm -rf ./*
shopt -u dotglob # Stop globbing leading dots.

As you can see, the command shopt -u exits this optional ENVIRONMENTAL state which is built in the Bash application, of globbing files with a dot in their start as well.

Then just for testing, when I replicated my starting state with touch .x, rm -rf ./* didn't delete .x unless I reused my solution so testing works fine.

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