You are not logged in.

#1 2021-11-11 16:23:35

dawnofman
Member
Registered: 2019-07-26
Posts: 140

[solved] BASH glob * not including hidden files in parent directory ?

Suppose I have the following file hierarchy:

abc.txt
def.txt
.ghi.txt
jkl.txt
one\123.txt
one\456.txt
one\.789.rxr
one\000.txt

As you can see .ghi.txt on parent directory and .789.txt in subdirectory are hidden files.

While at parent directory:

rm --recursive *; ### deletes everything BUT .ghi.txt on parent directory; ie: hidden files on parent directory are not processed by glob * operator while the ones on subdirectories are processed normally. This is valid for any command; eg: chown, chmod, etc.

I find this behavior confusing and was bitten many times looking for files that should have been copied and were not until I figured out what was happening.

Can someone explain why is this behavior normal ?

I mean, I hardly-doubt it is a bug, if so, it should have been fixed eons ago, so there should have be a reason for it.

Last edited by dawnofman (2021-11-14 18:20:29)

Offline

#2 2021-11-11 16:46:31

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,669
Website

Re: [solved] BASH glob * not including hidden files in parent directory ?

The asterisk glob never matches hidden files and folders. The hidden file within the subdirectory is removed, because your told rm to recursively delete that folder and hence all.of its content.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#3 2021-11-11 16:54:22

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] BASH glob * not including hidden files in parent directory ?

so there is no way to say, cp * ./wherever-else/ including hidden files with some command option instead of using rsync and the like ?

Offline

#4 2021-11-11 16:58:50

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,223

Re: [solved] BASH glob * not including hidden files in parent directory ?

cp .* * /somewhere/else
cp -r . /somewhere/else

Offline

#5 2021-11-11 17:14:19

Ferdinand
Member
From: Norway
Registered: 2020-01-02
Posts: 338

Re: [solved] BASH glob * not including hidden files in parent directory ?

Uhm... wouldn't .* match . and .. as well?

cp .??* /somewhere/else

may be less prone to surprising outcomes?

Offline

#6 2021-11-11 17:27:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,496
Website

Re: [solved] BASH glob * not including hidden files in parent directory ?

The OP specified this was bash, so it's much easier than all that:

shopt -s dotglob

Add that to your bashrc, you'll get exactly what you are looking for.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#7 2021-11-11 19:15:23

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] BASH glob * not including hidden files in parent directory ?

I've been reading shopt man page:



-s     If optnames are specified, set those options. If no optnames are specified, list all options that are currently set.

dotglob     If set, bash includes file names beginning with a '.' in the results of path name globbing.



That only means FILES starting with a dot ?

Nothing to do with . and .. directory entries ?

I mean, should I expect "funny things" with directory paths setting this dotglob ?

Offline

#8 2021-11-11 19:36:53

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,496
Website

Re: [solved] BASH glob * not including hidden files in parent directory ?

Yes, otherwise the option would serve no purpose.  Also note that you'd have your answer far sooner by just checking  / confirming for yourself than you would from creating a follow up post and waiting for an answer.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#9 2021-11-11 20:19:03

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,223

Re: [solved] BASH glob * not including hidden files in parent directory ?

Ferdinand wrote:

Uhm... wouldn't .* match . and .. as well?

Just checked, bash actually does that regardless of shopt… I got faaaar too reliant on zsh being the smarter shell tongue

Offline

#10 2021-11-11 20:35:22

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,496
Website

Re: [solved] BASH glob * not including hidden files in parent directory ?

Bash does what?  With dotglob set, the OP will get exactly what they were seeking.  "Hidden" files with names starting with a dot will be included in the regular "*" glob, but the symlinks "." and ".." will not be.

$ touch one two .three
$ ls *
one  two
$ shopt -s dotglob
$ ls *
.three	one  two

EDIT: oh, nvm, I missed the dot in the quote.  Yes any of the ".*" attempts were odd to start with; the fact that the shell gives odd output from odd input is not surprising.

Last edited by Trilby (2021-11-11 20:41:33)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#11 2021-11-11 20:39:37

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,223

Re: [solved] BASH glob * not including hidden files in parent directory ?

Bash resolves . and .. for .* - zsh does not (but what you wanted)
This doesn't relate to the dotglob shopt.

Offline

#12 2021-11-12 08:48:34

schard
Forum Moderator
From: Hannover
Registered: 2016-05-06
Posts: 2,669
Website

Re: [solved] BASH glob * not including hidden files in parent directory ?

Without the dotglob option set, you can also run

rm -rf * .[^.]*

to get that result.
But the dotglob setting seems like the more straighforward solution.


Inofficial first vice president of the Rust Evangelism Strike Force

Offline

#13 2021-11-12 13:34:49

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] BASH glob * not including hidden files in parent directory ?

Fine for a script, but for daily use there is no way I will remember that exact sequence everytime I need to, not to say typing a lot everytime. Anyway thanks for the tip.

Offline

#14 2021-11-12 13:38:41

dawnofman
Member
Registered: 2019-07-26
Posts: 140

Re: [solved] BASH glob * not including hidden files in parent directory ?

Trilby wrote:

Also note that you'd have your answer far sooner by just checking  / confirming for yourself than you would from creating a follow up post and waiting for an answer.

I was starting to reply to this in the line of "sorry, I am not following you on this one" when I realized my mistake: I was replying to myself and not using the quote option to reply to individual answers ... I guess I did not use this forum for a while and forgot about that. Sorry.

Offline

Board footer

Powered by FluxBB