Find directories that contains more than n matches of a certain filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find directories that contains more than n matches of a certain filename
# 1  
Old 12-04-2010
Find directories that contains more than n matches of a certain filename

I need to construct a command that finds directories which contains more than n matches of a certain filename.

E.g. I have many directories at different locations and want to find all directories that has 2 or more .dat-files.

I thought of using find and maybe the exec parameter to issue an ls|wc -l, but that would just return the number of files, not the directory itself (that i need).

Any suggestions?
# 2  
Old 12-04-2010
Hi, welcome to the forum. You could use an awk command to cut off after the last / and count the find output, for example:
Code:
find . -name '*.dat' | awk -F/ '{NF--}++A[$0]==n' OFS=/ n=2


Last edited by Scrutinizer; 12-05-2010 at 05:51 AM..
# 3  
Old 12-05-2010
Hi Scrutinizer,

I little new to UNIX.. Could you please explain the below awk command posted by you in detail ???
Quote:
find . -name '*.dat' | awk -F/ '{NF-=1}++A[$0]==n' OFS=/ n=2
Thanks in advance!
Vasanth.
# 4  
Old 12-05-2010
Sure:
-F/Use / as the field separator
{NF--} Reduced the field separator by one, so that the last field (the filename) gets discarded
++A[$0]==nRaise the number of occurrence (stored in Array A) by one and if the sum equals n then perform the default action which is to print the line (record)
OFS=/Set the output field separator to /
n=2set variable n to 2
# 5  
Old 12-05-2010
(Remove the wrong statement.)

Below code will be easy for beginner for understanding.
Code:
find . -name '*.dat' -type f -exec dirname {} \; |sort |uniq -c |awk '$1>n' n=2


Last edited by rdcwayx; 12-06-2010 at 05:50 AM.. Reason: (Remove the wrong statement.)
# 6  
Old 12-06-2010
Quote:
Originally Posted by rdcwayx
The poster asked to find 2 or more.

Code:
find . -name '*.dat' | awk -F/ '{NF-=1}++A[$0]>n' OFS=/ n=2

..
No, I think my code is correct. It needs to be == , otherwise the same directory gets printed multiple times. Put in another way, once the number of occurrences reaches 2, it answers the criterion of 2 or more .dat files and the directory should be printed, no matter how many more .dat files are found in the lines that follow..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Work on multiple directories, same filename

Can this be done and if yes how? I have 10 directories that end with the name 'cuff' eg dir1-cuff , dir2-cuff All these directories have a file named xyz.gtf How do I run the same code for each directory and print an output inside the directory something like for dir in *cuff... (3 Replies)
Discussion started by: newbie83
3 Replies

2. Shell Programming and Scripting

reading filename in nested directories

Hi all, I have some directory structure as $ls -ltr drwxr-xr-x 1 mscprod us_msc 512 May 10 08:34 650 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 652 drwxr-xr-x 1 mscprod us_msc 512 May 10 08:51 640 drwxr-xr-x 1 mscprod us_msc512 May 10 09:13 654 $ cd 650/ $ls fileabc.root and within... (7 Replies)
Discussion started by: emily
7 Replies

3. Shell Programming and Scripting

Find file that matches today's date in filename and move to /tmp in bash

I'm having problems with my bash script. I would like to find a file matching today's date in the filename, i.e. my_file_20120902.txt and then move it to a different directory, i.e. /tmp. Thanks. (1 Reply)
Discussion started by: jamesi
1 Replies

4. Shell Programming and Scripting

zgrep cannot find all matches

$ cat 1.csv abc in csv $ cat 1.mnf abc in mnf $ zip 1.zip 1.csv 1.mnf adding: 1.csv (stored 0%) adding: 1.mnf (stored 0%) $ zgrep abc 1.zip abc in csv How come zgrep cannot find "abc in mnf"? Thanks in advance. (5 Replies)
Discussion started by: carloszhang
5 Replies

5. Shell Programming and Scripting

finding matches between multiple files from different directories

Hi all... Can somebody pls help me with this... I have a directory (dir1) which has many subdirectories(vr001,vr002,vr003..) with each subdir containing similar text file(say ras.txt). I have another directory(dir2) which has again got some subdir(vr001c,vr002c,vr003c..) with each subdir... (0 Replies)
Discussion started by: bramya07
0 Replies

6. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

7. Shell Programming and Scripting

How to find 777 permisson is there or not for Directories and sub-directories

Hi All, I am Oracle Apps Tech guy, I have a requirement to find 777 permission is there or not for all Folders and Sub-folders Under APPL_TOP (Folder/directory) with below conditions i) the directory names should start with xx..... (like xxau,xxcfi,xxcca...etc) and exclude the directory... (11 Replies)
Discussion started by: gagan4599
11 Replies

8. Shell Programming and Scripting

want to move files in a dir into different directories based on the filename

I want to move the files in a dir to different dirs based on their file names. Ex: i have 4 different files with name - CTS_NONE_10476031_MRL_PFT20081215a.txt CTS_NONE_10633009_MRL_PFT20091020a.txt CTS_NONE_10345673_MRL_PFT20081215a.txt CTS_NONE_10872456_MRL_PFT20091020a.txt and the 1st... (4 Replies)
Discussion started by: Sriranga
4 Replies

9. Shell Programming and Scripting

find matches in file

Hi, im have log file ~100000 lines, 192.168.29.1 at 10/08/09 13:58:55 192.168.60.1 at 10/08/09 14:11:28 192.168.58.171 at 10/08/09 14:12:45 192.168.61.12 at 10/08/09 14:15:44 192.168.60.1 at 10/08/09 14:16:36 192.168.60.1 at 10/08/09 14:17:43 192.168.61.12 at 10/08/09 14:18:08... (9 Replies)
Discussion started by: Trump
9 Replies

10. Shell Programming and Scripting

searching for a filename in ALL directories

Hi, I am searching for a file named "Backup.txt" but I don't know in which directory it is. Can someone tell me, how I can search recursiv in all directories and subdirectories? thanks (2 Replies)
Discussion started by: ABE2202
2 Replies
Login or Register to Ask a Question