Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash for loop with wildcards and hidden files

Just witting a simple shell script and little confused:

Here is my script:

% for f in $FILES; do echo "Processing $f file.."; done 

The Command:

ls -la | grep bash  

produces:

% ls -a | grep bash .bash_from_cshrc .bash_history .bash_profile .bashrc 

When

FILES=".bash*" 

I get the same results (different formatting) as ls -a. However when

FILES="*bash*" 

I get this output:

Processing *bash* file.. 

This is not the expected output and not what I expect. Am I not allowed to have a wild card at the beginning of the file name? Is the . at the beginning of the file name "special" somehow?

Setting

FILES="bash*" 

Also does not work.

like image 533
sixtyfootersdude Avatar asked Jan 25 '10 21:01

sixtyfootersdude


People also ask

Does wildcard match hidden files?

Wildcards do not match hidden files except on UNIX-type platforms when the wildcard pattern starts with a dot character (.).

How do I iterate through a file in bash?

The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).

What are the three types of loops supported by bash?

There are three basic loop constructs in Bash scripting, for loop, while loop , and until loop .


1 Answers

The default globbing in bash does not include filenames starting with a . (aka hidden files).

You can change that with

shopt -s dotglob

$ ls -a .  ..  .a  .b  .c  d  e  f $ ls * d  e  f $ shopt -s dotglob $ ls * .a  .b  .c  d  e  f $  

To disable it again, run shopt -u dotglob.

like image 168
nos Avatar answered Oct 17 '22 02:10

nos