5

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)

user126154
  • 253
  • 4
  • 15

4 Answers4

11

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
  • 88,010
  • For me it does not, and it is not supposed to do so. "find" will match the glob for the basename only. It is straightforward use of find. Can you indicate which OS version you use? For me, the option -A for ls you mention does not work. Perhaps working under a different shell? – vanadium Feb 27 '21 at 13:45
  • Difficult to see from here what exactly might be going on on your system. – vanadium Feb 27 '21 at 13:49
  • Perhaps also try '\.*. This "escapes" the dot, and prevents it from being interpreted as a wildcard for one character. Not needed for me, but I added it to the question nevertheless. – vanadium Feb 27 '21 at 13:52
  • Forget about my comment on the -A option: I forgot I have exa installed, an alternative ls command that does not support the option. – vanadium Feb 27 '21 at 14:03
  • My mistake: the files that seemed "nonhidden" where just file with a long path and the "dot" was at the breakline! :( sorry. But now I created a test directory and seems to work... too much! that is to say, do not show nonhydden files in hidden directories – user126154 Feb 27 '21 at 14:08
  • Please note your question was explicitly to "list only hidden files": see your title. In the body of your question, you indicate: " I would like to ave only the recursive list of files and directory starting with . (but not the . directory itself!)". That is what this answer does. Also, please update your question for the comment on my answer. – vanadium Feb 27 '21 at 14:13
  • update the question and deleted erroneous comments. Sorry about that, and thanks a lot for your help – user126154 Feb 27 '21 at 14:15
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
  • 141
  • 5
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
  • 189