Find command to search files in a directory excluding subdirectories

Hi Forum,

I am using the below command to find files older than x days in a directory excluding subdirectories. From the previous forums I got to know that prune command helps us not to descend in subdirectories. Though I am using it here, not getting the desired result.

 
cd $dir
files2del=$(find . -type f -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' )

The output is :

 
./fgh.dat
./ggg.dat
./sss.dat
./test_2/dtms3.dat
./test_2/dtms4.dat
./test_2/mop.dat
 

I do not want the search to descend inside test_2 directory.
Also if I give the command

 
cd $dir
        files2del=$(find . -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' )
 

I get the output as:

 
./fgh.dat
./ggg.dat
./sss.dat
./test_2
 

Here I do not want the test_2 directory to be listed out. I want only the .dat files in the current directory. Please advise where am I going wrong.

I am using # ! /bin/sh

You can just pipe your find command with

grep -v filename

. The filename will be the one which you dont want to see in the output.

test_2 is a directory and I do not want it in the output, Also, what if there are 15 such subdirectories which I do not want to be listed. How can I generalize the script so that it prints only .dat files in the current directory and also does not list out any subdirectory name

Okay I got it.You can use

find . -maxdepth 1

which will search for your files to the first branch itself means it will not go into any subdirectory to check for the file.

maxdepth is not supported. I am using /bin/sh. Any other option?

Hello Jhilmil

You might want to try this

find . -path './directoryname' -prune -o -path './directoryname' -prune -o -mtime days -name \*.sh -print

In

prune

option you need to write the sub directories name so that it can ignore that.

This works fine. But what if I have a 100 subdirectories and I cant keep on listing all their names here. I need a generalized script which could be reused anywhere and which would not list out any subdirectory name.

Could anyone please advise.

"find . -maxdepth 1 " to search to current dir only

As I told previously, my system doesnt support maxdepth. Any other option?
I am using # ! /bin/sh