List all dotfiles current directory using zsh

10,311

Solution 1

The classic way we used to do it back in the day was:

ls .??*

This would list all dotfiles but not include . or ... Of course, whomever came up with this assumed that all dotfiles would be named with two or more characters after the ..

The other weakness is if you have directories that start with . then that command will list the contents of it. That aspect can be fixed by using the -d flag for ls. That tells ls to just show any directories specified on the command line without delving into them.

Nowadays, most versions of ls include the -A flag which will list all dotfiles in the directory but excluding . or ... And you don't have to worry about putting a -d flag on there.

Solution 2

Not only :

printf '%s\n' .*

(though beware that some shells other than zsh will include . and .. in that list)

With zsh syntax, restricting to regular files only:

printf '%s\n' .*(.)

To list all files including hidden ones:

printf '%s\n' *(D)

D for dot (hidden) files.

Solution 3

zsh comes shipped with ls -a and ls -A that are of great help!

ls -a lists all items (files and directories) in a directory ls -A does the same, but does not list .and ...

You can go to your .zshrc config file and create aliases to make these commands easier to run. I aliased mine this way:

alias l='ls -l'         # ls with items and directory details
alias la='ls -a'        # ls all items and directories within cd
alias lA='ls -A'        # ls all items and directories within cd, EXCEPT "." and ".."
alias lla='ls -la'      # combines "ls -l" and "ls -a"
alias llA='ls -lA'      # combines "ls -l" and "ls -A"

And the output is the following:

la and lA aliases in function


Also, if you want to display files icons you can simply install the following plugin, ls deluxe, from here. Many use colorls, but ls deluxe is written in Rust and is sensibly faster.

I added this alias in my .zshrc so that anytime I run ls I am instead running lsd.

alias ls='lsd'
Share:
10,311

Related videos on Youtube

jethar
Author by

jethar

Updated on September 18, 2022

Comments

  • jethar
    jethar over 1 year

    How can I list all dotfiles in a directory using zsh.?

    • Admin
      Admin over 10 years
      ls -a , it's not related to zsh , -a show dot files.
  • evilsoup
    evilsoup over 10 years
    This will include . and .., which will be fine for just listing things but might cause issues in scripts.
  • Gilles Quenot
    Gilles Quenot over 10 years
    Not for me. And with the added command ?
  • evilsoup
    evilsoup over 10 years
    I'm using bash; I've just checked with zsh and it seems you are correct (both of your commands work fine there). It still seems worth mentioning that different shells will handle this in different ways.
  • Jeff Hewitt
    Jeff Hewitt over 10 years
    And you can correct the first aspect with something like ls .[^.]*
  • jethar
    jethar over 10 years
    Somehow when I use these, I am getting internal listing of the dot-directories as well, which I want to avoid. I would like to avoid that. the printf '%s\n' .*(.) syntax seems to avoid that.
  • jethar
    jethar over 10 years
    This seems to work. However if I do ls .*(.) I seem to get a very truncated list of dotfiles. Why would that be?
  • evilsoup
    evilsoup over 10 years
    @nobuzz you could use ls -d .[^.]*; at least with GNU ls, the -d option means 'don't go into directories'.
  • kurtm
    kurtm over 10 years
    @nobuzz As I mentioned, the -d option stops that. (evilsoup mentioned it again just above.)
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    @evilsoup .* includes . and .. in all shells except zsh, where they are always omitted from globs.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    In zsh, .* excludes . and .., so no special care is necessary. In other shells, you would need also .[!.] for dot files with just two characters.
  • Stéphane Chazelas
    Stéphane Chazelas almost 4 years
    Note that ls has nothing to do with zsh, zsh doesn't have a ls builtin.