13

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.

2
  • 3
    why not to use find?? why to limit self w/ ls and grep??
    – zaufi
    Dec 29, 2015 at 15:18
  • 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
    – zaufi
    Dec 29, 2015 at 15:22

7 Answers 7

16

To list only hidden files:

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

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 | grep "^\..*/$"  

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!

4
6

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
1
  • echo .[!.]* or echo .[^.]*will list the hidden files and directories, excluding . and .. Jan 20, 2021 at 19:30
2

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 .*(.)
0

Try this command:

find -maxdepth 1 -type f -name '.*' | sed 's/^..//'

This will find only files (-type f) in the current directory (-maxdepth 1), starting with a '.', the sed command gets rid of './' in every line.

0

To list hidden files (long list extensive format)

find -maxdepth 1 -type f -name '.*' -ls | sed 's/^..//'

To list directories (a long format)

find -maxdepth 1 -type d -name '.*' -ls | sed 's/^..//'
0

List files:

tree --prune -aiL 1 -P .[^.]\* path/to/dir

List directories:

tree -daiL 1 -I [^.]\* path/to/dir

The first line displays the checked directory, and the last line displays the report. However, the report can be disabled with adding the --noreport flag.
path/do/dir - If this parameter is omitted, the current directory . is used.

-2

This command works:

ls -a | grep ".*"
  • Any hidden file starts with .

  • * matches all characters

4
  • 2
    In this context, .* doesn't mean what you think it does.
    – muru
    Nov 5, 2019 at 7:15
  • The regular expression .* matches any string, not just string starting with a dot.
    – Kusalananda
    Nov 5, 2019 at 7:27
  • "^.*" matches all the patterns that start with .
    – madD7
    Jul 5, 2020 at 9:37
  • @madD7 No, it matches any string that contains zero or more characters. To match a string that starts with a dot, use either ^\. or ^[.].
    – Kusalananda
    Feb 20 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.