4

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)

0
9

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.

7
  • 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 at 13:45
  • Difficult to see from here what exactly might be going on on your system. – vanadium Feb 27 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 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 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 at 14:08
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
3
  • That seems to work! – user126154 Feb 27 at 14:03
  • puh... will make some description ... later... – bac0n Feb 27 at 14:04
  • I accepted the other question because it is more coherent with the title of the question, so for other users it may be better like that. But I was in fact interested in your answer! Thank you! – user126154 Feb 27 at 14:17

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.