You are not logged in.
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
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
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
cp .* * /somewhere/else
cp -r . /somewhere/elseOffline
Uhm... wouldn't .* match . and .. as well?
cp .??* /somewhere/elsemay be less prone to surprising outcomes?
Offline
The OP specified this was bash, so it's much easier than all that:
shopt -s dotglobAdd 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
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
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
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 ![]()
Offline
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 twoEDIT: 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
Bash resolves . and .. for .* - zsh does not (but what you wanted)
This doesn't relate to the dotglob shopt.
Offline
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
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
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