Wednesday, May 1, 2024
22
rated 0 times [  22] [ 0]  / answers: 1 / hits: 7655  / 2 Years ago, fri, march 4, 2022, 4:17:06

I want to carry out some action (say chown) on all the hidden files in a directory.



I know that this .* is not a good idea because it will also find the current . and parent .. directories (I know that rm will fail to operate on . and .. but other commands, including chown and chmod, will happily take effect)



But all my hidden files have different names!



How should I glob for all hidden files while excluding . and .. ?


More From » command-line

 Answers
2

You can use the following extglob pattern:



.@(!(.|))



  • . matches a literal . at first


  • @() is a extglob pattern, will match one of the patterns inside, as we have only one pattern inside it, it will pick that


  • !(.|) is another extglob pattern (nested), which matches any file with no or one .; As we have matched . at start already, this whole pattern will match all files starting with . except . and ...




extglob is enabled on interactive sessions of bash by default in Ubuntu. If not, enable it first:



shopt -s extglob


Example:



$ echo .@(!(.|))
.bar .foo .spam

[#13581] Sunday, March 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
puccop

Total Points: 66
Total Questions: 99
Total Answers: 93

Location: Aruba
Member since Sun, Nov 27, 2022
1 Year ago
;