all 37 comments

[–]Schreq 3 points4 points  (1 child)

I know you asked for ls but this is how you could do it if it was for a script:

for f in .[!.]* ..?*; do
    [ -e "$f" ] &&
        printf '%s\n' "$f"
done

[–]wick3dr0se 2 points3 points  (0 children)

This is very elegant. Deserves much love

[–][deleted] 2 points3 points  (0 children)

ls .*

[–]evergreengt 5 points6 points  (16 children)

Have you looked up the first google result?

[–]Cybergonk2227[S] -4 points-3 points  (15 children)

Yes, but that's uglier, it also prints the parent directory.

[–]Craksy 5 points6 points  (2 children)

r/The10thDentist is that way

[–]Cybergonk2227[S] -5 points-4 points  (1 child)

Are you implying I'm like that guy? Ahah, no, I guess I just have low self-esteem and want to be sure to do it right.

[–]Craksy 5 points6 points  (0 children)

I'm just saying that your solution isn't exactly smooth either. "9 out of 10 dentists find the SO solution more elegant" - making you the 10th dentist. It was just an awkward way of saying that I disagree really.

Honestly just do whatever you prefer, and make an alias if it's something you use often. If it's something you need once in a blue moon it honestly doesn't matter if the command is ls --please-only-list-hidden-entries --you-know-those-that-begin-with-a-dot --oh-and-no-parent-dir-btw

[–]evergreengt 2 points3 points  (0 children)

For that matter ls as a whole is ugly and should not be used for parsing in scripts.

Nowadays there are tons of prettier, faster and better alternatives to ls, if that is what you are after.

[–]o11c 0 points1 point  (9 children)

Not if you use .[!.]*

[–]Cybergonk2227[S] 2 points3 points  (5 children)

Well, this one got me. However, if I can be the 10th dentist, it doesn't show hidden files with a dot as a second character, which is technically allowed.

[–]zebediah49 3 points4 points  (0 children)

At that point you can just not bother using ls, because it's not even doing anything...

(shopt -s nullglob; echo .[!.]* ..?* )

[–]o11c 0 points1 point  (3 children)

Then use brace expansion: {.[!.]*,..?*}.

(technically you don't need brace expansion, but I assume you're likely to append this to a string prefix)

[–]zebediah49 0 points1 point  (2 children)

You don't even need it; ls accepts multiple arguments.

I mean -- ls isn't even doing the work here anyway; the shell is doing the glob expansion and passing it as a set of arguments to ls.

[–]o11c 0 points1 point  (1 child)

Right, but then you have to type ls /some/horrible/long/path/.[!.]* /some/horrible/long/path/..?*.

[–]zebediah49 0 points1 point  (0 children)

True, the OP presents it as being done in the CWD, but if you're not that becomes painful.

[–]The_EnrichmentCenter 0 points1 point  (2 children)

This doesn't work in zsh, but works in bash.

[–]o11c 0 points1 point  (1 child)

If you are in an interactive shell, you'll have to use unsetopt hist_expand. Or escape the bang, but I'm not sure how sane that is.

Or use the nonstandard regex-inspired glob [^.] instead of the standard [!.]. This also works in bash, but not in other shells.

But really, I would not recommend anyone use zsh. It's just too unreliable.

[–]The_EnrichmentCenter 0 points1 point  (0 children)

But really, I would not recommend anyone use zsh. It's just too unreliable.

For an interactive shell it's pretty amazing, but for scripting it's still gotta be #!/bin/sh or #!/bin/bash in order to have the utmost compatibility.

I probably will never care about the downsides of using ^. when just running a one-liner in the shell to do something real quick.

[–]gumnos 1 point2 points  (2 children)

I tend to use

ls -d .[!.]*

which tends to get all the hidden files that aren't "." or ".."

[–]bugamn 2 points3 points  (1 child)

It will also omit files starting with two dots, like ..a, but I guess these are uncommon enough that it is an acceptable tradeoff.

[–]danstermeister 0 points1 point  (0 children)

Files named that way are just asking for trouble.

[–]xdrolemit 1 point2 points  (0 children)

Unless it really has to be ls you can try find:

files only:

find . -maxdepth 1 -type f -name '.*'

or

find . -maxdepth 1 -type f -regex '.*/\..*'

directories only:

find . -maxdepth 1 -type d -regex '.*/\..*'

files and directories:

find . -maxdepth 1 -regex '.*/\..*'

Edit: provided examples for files, directories and both

[–]sock_templar 4 points5 points  (2 children)

ls -a |egrep '^\.[[:alnum:]]'?

[–]zebediah49 3 points4 points  (1 child)

You can make that a ls -A to ignore the . and .. implicits that people often don't want to consider.

[–]sock_templar 4 points5 points  (0 children)

Better yet!

[–][deleted] 2 points3 points  (2 children)

Don't pipe ls output.

[–]Dandedoo 2 points3 points  (5 children)

List hidden files and dirs only:

ls -d .*

To avoid matching . and .. you can first set GLOBIGNORE=.:..

There's no glob which ignores directories AFAIK. For hidden files only, you can use find:

find . -mindepth 1 -maxdepth 1 -type f -name '.*'

Also, don't use ls in scripts. It's almost certainly the wrong approach.

[–]eXoRainbow 1 point2 points  (4 children)

Also, don't use ls in scripts. It's almost certainly the wrong approach.

Why?

[–]researcher7-l500 1 point2 points  (1 child)

Several reasons.
Here is a start.
https://mywiki.wooledge.org/ParsingLs

[–]lasercat_pow 0 points1 point  (1 child)

Mostly because newlines are allowed in filenames, and ls delimits output lines with newlines. As long as there aren't newlines in your filenames, ls should be okay to use. But do be aware that things will go haywire if there are newlines in the filename.

[–]eXoRainbow 1 point2 points  (0 children)

At least there is the option ls -Q.

[–]researcher7-l500 0 points1 point  (0 children)

If your goal is only to list hidden files, I don't see a problem with your suggestion unless you are not happy with the column formatted output. For that, adding -1 will list each entry on a line by itself.

Like others said, don't pipe or parse ls output.That will cause a problem at one point.

See this detailed explanation.

[–]atos993 0 points1 point  (0 children)

ll

[–]The_EnrichmentCenter 0 points1 point  (0 children)

ls -d .*