Find command to search files in a directory excluding subdirectories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find command to search files in a directory excluding subdirectories
# 1  
Old 12-13-2012
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.

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

The output is :

Code:
 
./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

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

I get the output as:

Code:
 
./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
# 2  
Old 12-13-2012
You can just pipe your find command with
Code:
grep -v filename

. The filename will be the one which you dont want to see in the output.
# 3  
Old 12-13-2012
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
# 4  
Old 12-13-2012
Okay I got it.You can use

Code:
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.
# 5  
Old 12-13-2012
maxdepth is not supported. I am using /bin/sh. Any other option?
# 6  
Old 12-13-2012
Hello Jhilmil

You might want to try this

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

In
Code:
prune

option you need to write the sub directories name so that it can ignore that.
# 7  
Old 12-14-2012
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Shell Programming and Scripting

Find/searching files in subdirectories excluding the fiels in Parent Directory

Hi All, requirement is to find and remove the files from sub directories but it should exclude the files from parent directory. At present i am using the below one but it finds and remove files from both parent and sub directories. find ${PATH} -type f \( -name securitas\* -o -name \*gz... (1 Reply)
Discussion started by: Naveenkk
1 Replies

3. UNIX for Advanced & Expert Users

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}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

4. Shell Programming and Scripting

Search subdirectories and find and print total files

Hi All, I have a folder name lets say path/to/folder/CUSTOMER and under this i have several folders and each of these subfolder have serveral subfolders and so on and at some point i will have a folder name called "FTP_FILES" . I need to search for these folders named "FTP_FILES and then... (10 Replies)
Discussion started by: Kevin Tivoli
10 Replies

5. Shell Programming and Scripting

Find files only in current directory...not subdirectories

Hi, I have to find files only in the current directory...not in the sub directories. But when I use Find command ... it searches all the files in the current directory as well as in the subdirectories. I am using AIX-UNIX machine.Please help..I tried to use maxdepth..but it is not working in AIX. (2 Replies)
Discussion started by: vsachan
2 Replies

6. Shell Programming and Scripting

Find all files for a user, excluding a directory

I have been searching, and cannot find an answer for this. I am trying to find all files for a user, lets call him (test001), and I want to exclude a specific directory. Here is the command I run, it finds all files: find / -user test001 I get this result: > find / -user test001 ... (4 Replies)
Discussion started by: steve2x4
4 Replies

7. Shell Programming and Scripting

Find files of specific size excluding search in a subdirectory

Hi All, I was exploring find command and came across -prune option which would exclude search in a mention subdirectory. My quesry is to search all files more that 100 MB size but exclude search in a subdirectory. I am using below command,but somehow it is not working. Can anybody help me... (6 Replies)
Discussion started by: usha rao
6 Replies

8. Shell Programming and Scripting

to parse a directory and its subdirectories and find owner name of files

hi all, i need to capture all the files in a directory and its subdirectories that have owner name different than the root owner. for one file it is " stat -c %U filename " but i need to search for each and every file and record it. thanks in advance (14 Replies)
Discussion started by: vyasa
14 Replies

9. UNIX for Dummies Questions & Answers

Find Files in a Directory Excluding Subdirectories

Hi, I have a filename Location.txt in a directory /abc. Similar name file is present in its subdirectory /abc/xyz. I want to find the file which is present only in /abc and not in /abc/xyz. Please any1 of u can provide a quick suggestion. Its very urgent. Thanks, Amol (2 Replies)
Discussion started by: Amol_Dicholkar
2 Replies

10. Shell Programming and Scripting

search files in a directory and its subdirectories

Hello my friends, I need to write a simple shell bad file :D that search and delete a file it's name 'Microsoft.txt' in the current directory and its subdirectories? So can you help to guide me how i can write this shell, Just give me the beginning :o thank you. (1 Reply)
Discussion started by: Net-Man
1 Replies
Login or Register to Ask a Question