3
for f in ~/common/.*; do
    echo $f
done

The entries listed are,

/home/sk/common/.           #undesired
/home/sk/common/..          #undesired
/home/sk/common/.aliasrc

And i am putting a ugly hack to skip processing . and .. to avoid this,

if [[ $f  == '/home/sk/common/.' || $f  == '/home/sk/common/..' ]]; then
  true 
else
  --do the ops
fi

Is there a shell option that will hide the dotted folders? I am facing this problem with ls -a as well.

0
4

Here is a method using bash's extglob:

shopt -s extglob
for f in .!(|.); do
  echo "$f"
done

With extglob the pattern !(pattern-list) matches anything except for the given pattern. The pattern in the example says match everything that starts with . and is not followed by nothing or another single ..

1
  • hi jordan, this is the shell option, i was looking for... ., more notes on what does glob pattern .!(|.) do, will help....
    – user93868
    Jul 12 '15 at 15:25
2

Using find

find ~/common -type f -name ".*" -print0 | \
    while read -d $'\0' file; do \
        echo $file; \
    done

or with find and IFS

find ~/common -type f -name ".*" -print0 | \
    while IFS= read -rd '' file; \
        do echo $file; \
    done
2

Did you mean:

ls -A

also you can use

ls -Al

This wont list the . and .. (its the capital A that does that). a will list all, and A will list almost all, almost all cause . and .. arent listed.

EDIT: The above is aimed at his second question, where he states he has the same issue with ls -a

2
  • 1
    Not very helpful in a script
    – A.B.
    Jul 12 '15 at 14:51
  • true, however I mention this as he indicated that he had trouble with ls -a which is the second part of his question that no one else has answered. Its the very last sentence.
    – dakka
    Jul 12 '15 at 15:24
1

You need to modify your glob pattern (currently *), probably you are looking for {[^.],.?}*, so

for f in ~/common/.{[^.],.?}*; do
    echo "$f"
done
7
  • gives the entry ..?* in the output in bash
    – A.B.
    Jul 12 '15 at 14:10
  • the same result
    – A.B.
    Jul 12 '15 at 14:33
  • @A.B. You probably don't have files which start from two dots as ..abc in that directory. For example if you do echo ???* in empty directory bash prints ???* instead of nothing. To overcome this set shopt -s nullglob.
    – jimmij
    Jul 12 '15 at 14:44
  • I have two files: .test and foo and OP needs .test
    – A.B.
    Jul 12 '15 at 14:46
  • No, OP needs all files which start from dot including two dots as ..abc.
    – jimmij
    Jul 12 '15 at 14:53
1

The portable way to do this is with a case statement - and it doesn't hurt to trim the glob a little.

for f in ~/common/.?*; do
    case $f in (*/..)  ;; (*)
    : do something w/ "$f"
    esac
done
0

.*[!.]

for f in ~/common/.*[!.]; do
    echo $f
done

Will give you all .[...]foo but not . and ..

Addition: Here's for excluding symlink files:

for f in ~/common/.*[!.]; do
    [[ -L $f ]] || echo $f
done

.note: change || to && if you want to echo only symlink files.

1
  • That won't match file ..abc either.
    – jimmij
    Jul 12 '15 at 14:28
-2
find . -maxdepth 1 -type f | while read f
do
ls -la "$f"
done
4
  • thanks Jodka, i am looking for the shell option that will help me to disable the listing of dotted folders, once for all...
    – user93868
    Jul 12 '15 at 13:12
  • I don't think that's possible. Jul 12 '15 at 13:13
  • 1
    That won't work if any of the filenames contain whitespace. (Though you could probably work around that by using the IFS (input field separator) variable and find's -print0 option.)
    – n.st
    Jul 12 '15 at 13:35
  • you are right. Same problem here? (not my solution) Jul 12 '15 at 14:57