Your rm -r *.*~
(same as rm -r ./*.*~
) would remove files and directories matching *.*~
that is files and directories whose name contain a dot and ends with a ~
But subdir
doe not contain any dot, so does not fit.
Read glob(7) and remember that it is your shell (not the /bin/rm
program!) which does the globbing.
So what is happening is that you type rm -r ./*.*~
to your shell. Your shell is expanding that to rm -r ./test.txt~
; then it finds the /bin/rm
program in your $PATH
, calls fork(2) to create a child process, and then execve(2) on /bin/rm
program, with an argument array of rm -rf ./test.txt~
so rm
does not even see ./subdir
To have every backup file removed in any direct subdirectory, type rm */*.*~
; with recent bash
(version 4 or later) and zsh
you could remove any backup file even deep in the file tree with rm **/*.*
A possible way to understand what the shell is doing is to replace your command (e.g. rm
) with echo
. That should give you the expansion (but read echo(1), since echo
is understanding some options like -n
). On some good shells, you could even use the tab key for autocompletion. I'm using zsh as my interactive shell because I like its autocompletion features.
At last, rm(1) knows about -i
(asking you interactively before removal). So I'll suggest rm -i **/*~
BTW, if you don't know about version control systems, it is time to learn them (I strongly recommend using git....), especially for "source" code like files (C or Python programs, shell scripts, etc etc etc ...) and you won't need backup files.
rm -r **.*~
didn't remove subdir/test1.txt~ – user4035 Aug 07 '15 at 17:31/
; I am suggestingrm **/*~
– Basile Starynkevitch Aug 07 '15 at 17:35globstar
is azsh
feature adopted bybash4
andksh
. Inbash
andksh
you must turn the feature on. Forbash
:shopt -s globstar
and forksh
:set -o globstar
– fd0 Aug 07 '15 at 17:37