6

I would like to do something like

ls -RA .?* >> LSRA.list

but with this command and some other variants I tried, I always get also all non-hidden files in the directory where I am.

Namely, if the directory contains files

.hiddenfile foo

with above command I get ther recursive list of both, while I would like to ave only the recursive list of files and directory starting with . (but not the . directory itself!)

I checked answers to this question but I did't find the solution to my problem.

Update: best options found so far:

 ls -RA .!(|.)*

and

 find -path './.*' -name '.*' -empty -printf %P\\n

the latter recursively list all hidden files in all hidden directory (so if am hidden directory contains a non-hidden files, it does not show that file).

Further update. both answers of bac0n and vanadium work: I cannot accept both! (first one recursively shows nonhidden files in hidden directory, latter one recursively shows only hidden files)

4 Answers4

12

To recursively list only hidden files from a terminal, you can use the tool find with the -type f option:

find ~ -type f -name '.*'

This will find all files in the user's home directory for which the basename starts with a dot, i.e., a hidden file or folder. Remove -type f to list both hidden files and folders, or specify type d to list only hidden directories. Specify any other directory by replacing ~ with a valid pathname. Specify . to list hidden files in the current working directory and below.

vanadium
  • 97,564
3

It may be hard to match every corner-case:

find \( -path './.*' -type d -empty -printf %P/\\n \) -o -type f -path './.*' -printf %P\\n
0

Here's a function that handles no command arguments, doesn't throw errors on directories without hidden files, appends / on listed directories, and outputs to a single column.


lsa () {
# Exit codes with/without argument are 0 if no hidden files present
EXIT=$(ls -1d .!(|.) |& grep -q "No such file"; echo $?)
EXIT_ARG=$(cd $1; ls -1d .!(|.) |& grep -q "No such file"; echo $?)

# If no argument
if [ $# -eq 0 ]; then
    if [ $EXIT -eq 0 ]; then
        printf ""
    else
        ls -1dp .!(|.)
    fi

# If argument
else
    if [ $EXIT_ARG -eq 0 ]; then
        printf ""
    else
        (cd $1; ls -1dp .!(|.))
    fi
fi

}

Jeff_V
  • 150
  • 6
0

Kindly run below commands to list only hidden items from any location(location can change on below commands) recursively and suppress error in order to show only expected result.

#to list every type of file/folder/data/objects... which are hidden in linux

sudo find / -iname '.*' 2> /dev/null|nl   

#to list only hidden files recursively in linux

sudo find / -type f -iname '.*' 2> /dev/null|nl

#to list only hidden directories recursively in linux

sudo find / -type d -iname '.*' 2> /dev/null|nl 
Mr. Linux
  • 195
  • 5