Search code examples
bashshellglob

bash: getting rid of '.' and '..' when looping over files that begin with '.' in a folder


I want to loop over the files that begin with '.' in directory x (x might be any path):

$ for file in x/.*
> do
>     echo -n $file" "
> done
x/. x/.. x/..a x/.b

What is the best way to get rid of x/., and x/.. ?


Solution

  • with bash, you can use GLOBIGNORE

    $ ls -1tra
    ..
    .test1
    .test
    test
    .
    
    $ for i in .*; do echo $i;done
    .
    ..
    .test
    .test1
    
    $ GLOBIGNORE=".:.."
    $ for i in .*; do echo $i;done
    .test
    .test1