How to show only hidden directories, and then find hidden files separately

47,042

Solution 1

To list only hidden files:

ls -ap | grep -v / | egrep "^\."  

Note that files here is everything that is not a directory. It's not file in "everything in Linux is a file" ;)

To list only hidden directories:

ls -ap | egrep "^\..*/$"  

Comments:

  • ls -ap lists everything in the current directory, including hidden ones, and puts a / at the end of directories.
  • grep -v / inverts results of grep /, so that no directory is included.
  • "^\..*/$" matches everything that start with . and end in /.
  • If you want to exclude . and .. directories from results of the second part, you can use -A option instead of -a for ls, or if you like to work with regex, you can use "^\.[^.]+/$" instead of "^\..*/$".

Have fun!

Solution 2

To list the hidden files and directories in the current directory, including . and ..:

echo .*

To list the hidden files and directories in the current directory and its subdirectories recursively:

find . -name '.*'

If you want to save the results to a file, use a redirection:

find . -name '.*' >output-file.txt

Solution 3

Switch to zsh (if you haven't already), and run

ls .*(^/)

The part inside parenthesis is so called glob qualifiers and means to select everything but directories.

If you are interested only in plain files, so want to exclude not only directories, but also other special files (named pipes etc) then try

ls .*(.)
Share:
47,042

Related videos on Youtube

Rikg09
Author by

Rikg09

Updated on September 18, 2022

Comments

  • Rikg09
    Rikg09 almost 2 years

    I'm trying to list all the hidden files in a directory, but not other directories, and I am trying to do this using only ls and grep.

    ls -a | egrep  "^\."
    

    This is what I have so far, but the problem is that it also lists hidden directories, when I don't want that.

    Then, completely separately, I want to list the hidden directories.

    • zaufi
      zaufi over 8 years
      why not to use find?? why to limit self w/ ls and grep??
    • zaufi
      zaufi over 8 years
      just for the case (if you don't know about find): 1) find . -maxdepth 1 -name '.*' -type f to find "hidden" files 2) find . -maxdepth 1 -name '.*' -type d to find "hidden" directories
  • Rikg09
    Rikg09 over 8 years
    Whilst the others did answer the question, you were the only one who listened when I said I only wanted to use ls and grep. I know you can do this using find, but I wanted to be able to do this using only grep and ls, thanks a lot
  • muru
    muru over 4 years
    In this context, .* doesn't mean what you think it does.
  • Kusalananda
    Kusalananda over 4 years
    The regular expression .* matches any string, not just string starting with a dot.
  • Pau Coma Ramirez
    Pau Coma Ramirez over 3 years
    echo .[!.]* or echo .[^.]*will list the hidden files and directories, excluding . and ..
  • sequence
    sequence over 2 years
    ls -aF | egrep "^\.[^.]+/$" | tar -zcvf hiddens.tar.gz -T - doesn't seem to do the trick. It creates an empty archive.