You are not logged in.

#1 2008-05-17 02:40:41

tesjo
Member
Registered: 2007-11-30
Posts: 164

bash wildcard question

Hopefully someone can explain to me exactly how the wildcard works before I hose my system.
Consider these case as user not root.

Ok if I am cleaning a directory I may run this

rm -rf *

and as my finger comes off the enter key I fear it will see the .. directory and flush my entire home directory, but of course it doesn't. In fact it doesn't remove any hidden files, those starting with a dot.

So I think maybe the wildcard cannot start with a dot, but this isn't the case because if were to run

rm -rf .*

It would wipe out  the .. directory. I haven't actually run that but I know it tries with 'rm .*' but cant because it is a directory.

So could someone please clarify how wildcard is working?

Offline

#2 2008-05-17 02:56:22

bender02
Member
From: UK
Registered: 2007-02-04
Posts: 1,328

Re: bash wildcard question

Not that I can give an explanation, but 'rm -rf .*' would only delete all the files and dirs which start with a dot; but would do nothing to ../ (I just tried wink

Offline

#3 2008-05-17 03:08:13

tesjo
Member
Registered: 2007-11-30
Posts: 164

Re: bash wildcard question

Cool that seems to be explained by man rm.
--preserve-root being the default setting, that is nice to know. But I still am wondering about the dot for wildcard.

Offline

#4 2008-05-17 03:44:04

tesjo
Member
Registered: 2007-11-30
Posts: 164

Re: bash wildcard question

Well enough googling got the answer wouldn't you know.

The default shell setting does not expand to files begining with a dot,

$ shopt dotglob
dotglob            off

but the shell option can be changed

$ shopt -s dotglob
$ shopt dotglob
dotglob            on

Now 'rm *' or 'cp *' or whatever will expand * to regular files and .* files.

Offline

#5 2008-05-17 22:59:10

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: bash wildcard question

$ shopt dotglob
dotglob         off
$ rm -rfv .*
rm: cannot remove directory `.'
rm: cannot remove directory `..'
$ shopt -s dotglob
$ shopt dotglob
dotglob         on
$ rm -rfv .*
rm: cannot remove directory `.'
rm: cannot remove directory `..'
$ rm -rfv *
removed `file3'

I'm not sure why it can't delete '.' or '..' I guess that it's a protection code inside rm or something, because I can remove the dir using other shell, so the first shell will be in a non existent directory

you should use rm -rf .[^.]*

$ ls -a
.  ..  .a  ...a  .b
$ rm -rfv .[^.]*
removed `.a'
removed `.b'

or if you have files with several dots

$ rm -rfv .*[^.]*
removed `.a'
removed `...a'
removed `.b'

the [^.] thing mach all character that aren't a dot

Last edited by noalwin (2008-05-17 23:05:03)

Offline

#6 2008-05-18 14:28:01

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: bash wildcard question

I for one am always confused with these things. (one reason I use mc)
A bit more typing, but 'find -mindepth 1 -maxdepth 1' will match what you want.

find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf

Offline

#7 2008-05-18 14:44:37

noalwin
Member
From: Spain
Registered: 2007-06-08
Posts: 115

Re: bash wildcard question

Procyon wrote:

I for one am always confused with these things. (one reason I use mc)
A bit more typing, but 'find -mindepth 1 -maxdepth 1' will match what you want.

find . -mindepth 1 -maxdepth 1 -print0 | xargs -0 rm -rf

I think that

find . -mindepth 1 -maxdepth 1 -delete

it's simpler and more efficient wink

Offline

Board footer

Powered by FluxBB