find restricted search to some directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find restricted search to some directories
# 8  
Old 01-03-2011
In:
Quote:
Originally Posted by radoulov
<snip>
Code:
find $(find . -name '2[0-9][0-9][0-9][01][0-9]' -type d) -type f

you may run into problems if the number of arguments for the outer find exceeds your shell's limits. You can get around that by:
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -exec find {} -type f -print \;

Now if you want to get fancy Smilie, taken from an example from xargs:
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -print | xargs sh -c 'find "$@" -type f -print'

(xargs is your friend Smilie)
# 9  
Old 01-03-2011
Quote:
Originally Posted by m.d.ludwig
In:
you may run into problems if the number of arguments for the outer find exceeds your shell's limits.
Those are not shell limits, see above Smilie

Quote:
You can get around that by:
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -exec find {} -type f -print \;

I would avoid this one because of its inefficiency.

Quote:
Now if you want to get fancy Smilie, taken from an example from xargs:
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -print | xargs sh -c ’find "$@" -type f -print'

(xargs is your friend Smilie)
Hm,
I believe you mean:

Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d | 
  xargs -I{} find {} -type f

Or this:

Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d \
  -exec sh -c 'find "$@" -type f' - {} +

Otherwise you'll skip $0.

Last edited by radoulov; 01-03-2011 at 08:40 AM..
This User Gave Thanks to radoulov For This Post:
# 10  
Old 01-03-2011
Quote:
Originally Posted by m69w
This works indeed (with a '.' after the first find though).

May be i'm being picky but isn't just 1 single call to find necessary ?
try
Code:
find -name "2[0-9][0-9][0-9][0,1][0-9]" -type d -print -exec ls -1 {} \;

more try
Code:
find -name "2[0-9][0-9][0-9][0,1][0-9]" -type d -print -exec ls -1 {} \;| sed -e :a -e 'N;s/^\(\..*\)\n\([^\.]*\)/\1 \2/;s/\(\..*\)\(\..*\)/\1\n\2/;ba'|sed '/^$/d;s/ / -> /1'

This User Gave Thanks to ygemici For This Post:
# 11  
Old 01-03-2011
@radoulov:
Quote:
Originally Posted by radoulov
Those are not shell limits, see above
You are quite correct.

But for:
Quote:
Originally Posted by m.d.ludwig
Code:
find . -name  '2[0-9][0-9][0-9][01][0-9]' -type d -print | xargs sh -c ’find "$@"  -type f -print'

I did not mean:
Quote:
Originally Posted by radoulov
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d | xargs -I{} find {} -type f

The -I option implies the -L1 option, so this would be functionally equivalent to:
Quote:
Originally Posted by m.d.ludwig
Code:
find . -name  '2[0-9][0-9][0-9][01][0-9]' -type d -exec find {} -type f -print  \;

because as per xargs:
Code:
-L max-lines
    Use at most max-lines nonblank input lines per command line.
    Trailing blanks cause an input line to be logically continued
    on the next input line.  Implies -x.

You are correct about skipping $0, so I'd like to change my answer to:
Code:
find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -print | xargs sh  -c ’find "$@" -type f -print' --

(xargs is still your friend)
This User Gave Thanks to m.d.ludwig For This Post:
# 12  
Old 01-03-2011
Quote:
Originally Posted by m.d.ludwig
@radoulov:

You are quite correct.

But for:

I did not mean:

The -I option implies the -L1 option, so this would be functionally equivalent to:
[...]
Didn't know this quite important detail,
thanks for pointing it out!
# 13  
Old 01-03-2011
And thanks for pointing out the "skipping of $0".
# 14  
Old 01-03-2011
From a performance perspective, did some benchmark tests: M.D.Ludwig's solution seems faster.

In the competition was xargs, the filter suggestion from ygemici (using egrep instead of the sed expression), and a while read loop:

1) find . -name '2[0-9][0-9][0-9][01][0-9]' -type d | xargs -I{} find {} -type f
2) find . -name '2[0-9][0-9][0-9][01][0-9]' -type d -print | xargs sh -c 'find "$@" -type f -print' (with incomplete results)
3) find . -name 2[0-9][0-9][0-9][0,1][0-9] -type d|while read d; do find $d -type f; done
4) find . -name "2[0-9][0-9][0-9][0,1][0-9]" -type d -print -exec ls -1 {} \;|egrep -v '^\.'

xargs is the way to go, thanks for the suggestion M.D : )

Last edited by radoulov; 01-03-2011 at 11:23 AM.. Reason: Code tags ...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. Shell Programming and Scripting

Search: find current line, then search back

Hello. I want to find a line that has "new = 0" in it, then search back based on field $4 () in the current line, and find the first line that has field $4 and "last fetch" Grep or Awk preferred. Here is what the data looks like: 2013-12-12 12:10:30,117 TRACE last fetch: Thu Dec 12... (7 Replies)
Discussion started by: JimBurns
7 Replies

3. Shell Programming and Scripting

Search for file extensions in the given directories

Hey guys, I'm lost... I need to make a script that will work in this way: ./script.sh -e sh /usr/bin /home/student this script will result in this output: amuFormat.sh /usr/bin gettext.sh /urs/bin perfect.sh /home/student the parameter -e <ext> gives you which... (2 Replies)
Discussion started by: Miki1579
2 Replies

4. UNIX for Advanced & Expert Users

Search file in all directories.

Hi colleagues, I need to search one file in all dierctories, i have O.S. AIX 5.3, my file began with cc, the others caracters i unknow. Then i can to search one string in file in all dierctories. Thank you for advanced. (8 Replies)
Discussion started by: systemoper
8 Replies

5. 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

6. 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

7. UNIX for Dummies Questions & Answers

Using "find" in restricted directories

Hi, I would like to know is there any way to find/search filenames or directories inside the directories which has resticted permission. When we use normal "find" command it returns "permission denied" message for the root directories or directories with restricted permissions. Thanks (2 Replies)
Discussion started by: forstudy3
2 Replies

8. Shell Programming and Scripting

How to search through directories and sub dir

Im working on a project that basically imitates the find and whereis commands. The program will take in a file name or regular expression and, starting with the current directory search downwards and match any files with that pattern and prints the path name. I don't understand how to do this... (5 Replies)
Discussion started by: new2C
5 Replies

9. Shell Programming and Scripting

how to search directories

Hello everybody, i'm dummy for unix but i want to learn something. i want to search the working directory and its subdirectories( all ) to find the files which are more than 1024 bytes. So which commands must i learn? Thanks to all. (13 Replies)
Discussion started by: redbeard_06
13 Replies

10. Shell Programming and Scripting

script to search all the directories

Hi there, Is there any command or script to search all the directories for duplicated files? Thanks, Abrahim (3 Replies)
Discussion started by: abk
3 Replies
Login or Register to Ask a Question