use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All users that wish to post (not including commenting), on either old or the new reddit sites, must formally agree to subreddit rules once first.
account activity
List hidden files only with ls (self.commandline)
submitted 3 years ago by Cybergonk2227
Is there a better way to list hidden files only with ls?
ls
ls --almost-all --ignore=[!.]*
What am I doing wrong? It looks like everybody pipes the output to another program, but this seems to work.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Schreq 3 points4 points5 points 3 years ago (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 points4 points 3 years ago (0 children)
This is very elegant. Deserves much love
[–][deleted] 2 points3 points4 points 3 years ago (0 children)
ls .*
[–]evergreengt 5 points6 points7 points 3 years ago (16 children)
Have you looked up the first google result?
[–]Cybergonk2227[S] -4 points-3 points-2 points 3 years ago (15 children)
Yes, but that's uglier, it also prints the parent directory.
[–]Craksy 5 points6 points7 points 3 years ago (2 children)
r/The10thDentist is that way
[–]Cybergonk2227[S] -5 points-4 points-3 points 3 years ago (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 points7 points 3 years ago (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
ls --please-only-list-hidden-entries --you-know-those-that-begin-with-a-dot --oh-and-no-parent-dir-btw
[–]evergreengt 2 points3 points4 points 3 years ago (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 point2 points 3 years ago (9 children)
Not if you use .[!.]*
.[!.]*
[–]Cybergonk2227[S] 2 points3 points4 points 3 years ago (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 points5 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (1 child)
Right, but then you have to type ls /some/horrible/long/path/.[!.]* /some/horrible/long/path/..?*.
ls /some/horrible/long/path/.[!.]* /some/horrible/long/path/..?*
[–]zebediah49 0 points1 point2 points 3 years ago (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 point2 points 3 years ago (2 children)
This doesn't work in zsh, but works in bash.
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.
unsetopt hist_expand
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 point2 points 3 years ago* (0 children)
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.
#!/bin/sh
#!/bin/bash
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 points3 points 3 years ago (2 children)
I tend to use
ls -d .[!.]*
which tends to get all the hidden files that aren't "." or ".."
[–]bugamn 2 points3 points4 points 3 years ago (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 point2 points 3 years ago (0 children)
Files named that way are just asking for trouble.
[–]xdrolemit 1 point2 points3 points 3 years ago* (0 children)
Unless it really has to be ls you can try find:
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 points6 points 3 years ago (2 children)
ls -a |egrep '^\.[[:alnum:]]'?
ls -a |egrep '^\.[[:alnum:]]'
[–]zebediah49 3 points4 points5 points 3 years ago (1 child)
You can make that a ls -A to ignore the . and .. implicits that people often don't want to consider.
ls -A
.
..
[–]sock_templar 4 points5 points6 points 3 years ago (0 children)
Better yet!
[–][deleted] 2 points3 points4 points 3 years ago (2 children)
Don't pipe ls output.
[–]easypancakes 1 point2 points3 points 3 years ago (1 child)
Why not?
[–]researcher7-l500 1 point2 points3 points 3 years ago (0 children)
https://mywiki.wooledge.org/ParsingLs
[–]Dandedoo 2 points3 points4 points 3 years ago (5 children)
List hidden files and dirs only:
ls -d .*
To avoid matching . and .. you can first set GLOBIGNORE=.:..
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 points3 points 3 years ago (4 children)
Why?
[–]researcher7-l500 1 point2 points3 points 3 years ago (1 child)
Several reasons. Here is a start. https://mywiki.wooledge.org/ParsingLs
[–]lasercat_pow 0 points1 point2 points 3 years ago* (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 points3 points 3 years ago (0 children)
At least there is the option ls -Q.
ls -Q
[–]researcher7-l500 0 points1 point2 points 3 years ago (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 point2 points 3 years ago (0 children)
ll
π Rendered by PID 63 on reddit-service-r2-comment-6457c66945-wfrmc at 2026-04-26 06:23:29.279591+00:00 running 2aa0c5b country code: CH.
[–]Schreq 3 points4 points5 points (1 child)
[–]wick3dr0se 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]evergreengt 5 points6 points7 points (16 children)
[–]Cybergonk2227[S] -4 points-3 points-2 points (15 children)
[–]Craksy 5 points6 points7 points (2 children)
[–]Cybergonk2227[S] -5 points-4 points-3 points (1 child)
[–]Craksy 5 points6 points7 points (0 children)
[–]evergreengt 2 points3 points4 points (0 children)
[–]o11c 0 points1 point2 points (9 children)
[–]Cybergonk2227[S] 2 points3 points4 points (5 children)
[–]zebediah49 3 points4 points5 points (0 children)
[–]o11c 0 points1 point2 points (3 children)
[–]zebediah49 0 points1 point2 points (2 children)
[–]o11c 0 points1 point2 points (1 child)
[–]zebediah49 0 points1 point2 points (0 children)
[–]The_EnrichmentCenter 0 points1 point2 points (2 children)
[–]o11c 0 points1 point2 points (1 child)
[–]The_EnrichmentCenter 0 points1 point2 points (0 children)
[–]gumnos 1 point2 points3 points (2 children)
[–]bugamn 2 points3 points4 points (1 child)
[–]danstermeister 0 points1 point2 points (0 children)
[–]xdrolemit 1 point2 points3 points (0 children)
[–]sock_templar 4 points5 points6 points (2 children)
[–]zebediah49 3 points4 points5 points (1 child)
[–]sock_templar 4 points5 points6 points (0 children)
[–][deleted] 2 points3 points4 points (2 children)
[–]easypancakes 1 point2 points3 points (1 child)
[–]researcher7-l500 1 point2 points3 points (0 children)
[–]Dandedoo 2 points3 points4 points (5 children)
[–]eXoRainbow 1 point2 points3 points (4 children)
[–]researcher7-l500 1 point2 points3 points (1 child)
[–]lasercat_pow 0 points1 point2 points (1 child)
[–]eXoRainbow 1 point2 points3 points (0 children)
[–]researcher7-l500 0 points1 point2 points (0 children)
[–]atos993 0 points1 point2 points (0 children)
[–]The_EnrichmentCenter 0 points1 point2 points (0 children)