Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files.

For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided.

`find . \( ! -name ".*" -prune \) -mtime +${n_days} -type f -print`

regex and depth options with find command aren't working.

Appreciate any help.

find . \( -type d -name ".?*" -prune \) -o -mtime +${n_days} -type f \! -name ".*" -print

The -prune primary in find ignores everything except directories, but the combination ! -name ".*" -prune does not prune directories with names starting with a period. A rough equivalent of -maxdepth 1 for use in versions of find that don't have the -maxdepth primary is \( ! -name . -prune \) .

If there is no -exec primary, no -ok primary, and no -print primary in the expression given to find , the -print primary is supplied by default.

The following is a slightly simpler command than MadeInGermany's suggestion and should produce the same results:

find . \(  ! -name . -prune \) ! -name ".*" -type f -mtime +${n_days}
1 Like

Don, your find does not visit any subdirectories,
while my find visits non-hidden subdirectories.

One precision:
-maxdepth 1 can be replaced by \( ! -name <basestartdir> -prune \)
where <basestartdir> is . when the start directory is . .
Therefore, I would append /. to a startdir, and add -type d for clarity:

find /startdir/. \( -type d ! -name . -prune \)

And perhaps add -print or -o -print (there is a difference!) for even more clarity.

Thanks guys.. I will test both of them..

Also How about this command?

find . \( ! -name ".*" -prune \) -mtime +2 -type f -print | grep -v "[^.]/"
  1. please use the code tags (at the top of the Wiki editor)!
  2. .* matches . so it will prune at the start directory i.e. not do anything. Therefore the trick .?* that does not match . .
2 Likes

Yes.

The original request (Find all files in the current directory only excluding hidden directories and files.) is ambiguous. With the reference to -maxdepth , I thought the intent was to "find all files in the current directory only (excluding hidden directories and (hidden) files)". But, I guess it could also be read as "find all files in the current directory (only excluding hidden directories) and files (in non-hidden directories)".

Maybe ksailesh1 will give us a better description with a small sample file hierarchy and the desired output.

Apologise if my question is not clear.

Basically i need to find all non-hidden files in the current directory only.. It shouldn't traverse through hidden and non-hidden sub directories.

For this i think, below command is fine..

find . \( ! -name ".*" -prune \) -mtime +0 -type f -print | grep -v "[^.]/"

Don, Your command is also giving the same results

find . \(  ! -name . -prune \) ! -name ".*" -type f -mtime +${n_days}