0

Using:

for eachfile in /mnt/thara/*

Which iterates through hidden files as well. I don't need hidden files.

  • 4
    shopt -u dotglob ? – Jeff Schaller Jun 10 '16 at 16:14
  • Another way around it is: ls /mnt/thara | while read i; do ... done – Peschke Jun 10 '16 at 16:29
  • 3
    Which shell are you using? – heemayl Jun 10 '16 at 16:44
4

The normal behavior of bash is to do not look to the hidden files when using for. But this behavior can be changed with shopt command.

To enable the scanning for hidden files with "*":

shopt -s dotglob

To disable the scanning for hidden files with "*" (default behavior)

shopt -u dotglob

So try a script like this :

shopt -u dotglob
for eachfile in /mnt/thara/*

And now the hidden files must go away.

2

Other way is

for eachfile in /mnt/thara/[^.]*
1

I agree with the comment about the dotglob shell option. If it is unset, the behavior of the for loop is the one expected:

utente@computer:/tmp/test$ shopt | grep dotglob
dotglob         off

Let a, b, and c are normal files; .hidden1 and .hidden2 hidden files:

utente@computer:/tmp/test$ touch a b c .hidden1 .hidden2

utente@computer:/tmp/test$ ls -al
totale 8
drwxrwxr-x  2 utente utente 4096 giu 10 18:28 .
drwxrwxrwt 13 root   root   4096 giu 10 18:28 ..
-rw-rw-r--  1 utente utente    0 giu 10 18:28 a
-rw-rw-r--  1 utente utente    0 giu 10 18:28 b
-rw-rw-r--  1 utente utente    0 giu 10 18:28 c
-rw-rw-r--  1 utente utente    0 giu 10 18:28 .hidden1
-rw-rw-r--  1 utente utente    0 giu 10 18:28 .hidden2

For loop:

utente@computer:/tmp/test$ for eachfile in * ; do ls $eachfile ; done

a
b
c

Another way, independent from shell options: let's instruct find to filter out all pathnames whose initial charachters match the dot .:

utente@computer:/tmp/test$ find . \( ! -path '*/.*' \) -type f  -exec ls {} \;

./c
./b
./a

See also this question on superuser.com

0

you can use find command like this :

for file in $(find /mnt/thara/ -type f \( ! -iname ".*" \)) ; do ---something $file--- ; done`
0

Or very simply..

ls -l | egrep -v "^\."

or to iterate all directories showing filenames:

ls -R | egrep -v "^\."
  • That will simply list the files - not iterate through them. – garethTheRed Jun 10 '16 at 19:38
  • If you want to iterate through just file names without hidden files use -R – Michael Steinfeld Jun 13 '16 at 19:19
  • @garethTheRed is right; also "iterate through" != "recursively list." – Wildcard Jun 13 '16 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.