Find directories by regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find directories by regex
# 1  
Old 04-11-2011
Find directories by regex

Hello,
I want to check if directories exist with a regex expression

dir1=/temp/local/*/home (exists on file system)

dir2=/temp/server/*/logs (does not exist on file system)

I want to check if there are any directories with the above regex

Code:
Code:
if [[ -d ${dir} ]];then
   echo "Directory exist"
else 
   echo "Directory does not exist"
fi

For both dir1 and dir2 "Directory does not exist" is printed.

Any way to make this check.

Thanks
# 2  
Old 04-11-2011
if [ `find ${dir} -type d -prune` ]; then
echo "Directory exist"
else
echo "Directory does not exist"
fi

Last edited by pbillast; 04-11-2011 at 12:11 PM..
# 3  
Old 04-11-2011
Using only basic sh and printing a line for each match found:
Code:
for d in /temp/local/*/home/ /temp/server/*/logs/; do
        if [ -d "$d" ]; then
                echo "$d: exists"
        else
                echo "$d: no match"
        fi
done

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 04-11-2011
Or just
Code:
ls -d /temp/local/*/home /temp/server/*/logs

... but ok i know what you guys are going to say :
.... argument list too long & MAX_ARGS constraints ... Smilie

Stick with alister's suggestion, that's the best one (... the suggestion Smilie )
# 5  
Old 04-11-2011
Quote:
Originally Posted by ctsgnb
Stick with alister's suggestion, that's the best one (... the suggestion Smilie )
Sticking too many args in a for statement doesn't make too many args not be too many args. Neither does sticking 'find' in backticks make too many args not be too many args.

Just searching for directory names though, I don't think is likely to cause too many arguments.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find command and regex

Hi All, We have to copy some files from a source directory to a destination directory. We only have to copy the file if the filename is in a list of values. We can use find command: find . -type f -name '*_111.txt' -o -name '*_115.txt' ... -exec cp {} /tmp \; But the list contains... (3 Replies)
Discussion started by: bartleby
3 Replies

2. Shell Programming and Scripting

what's wrong with my regex using find

#ls json-* json-lexer.c json-lexer.h json-parser.c json-parser.h json-streamer.c json-streamer.h #find . -regex '^(json-)+.' return nothing (3 Replies)
Discussion started by: yanglei_fage
3 Replies

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

4. Shell Programming and Scripting

find -regex option

I'm trying to use regular expression arguments as variables. I have to surround the regular expression with double quotes or else it automatically expands that regular expression to whatever is in that directory. Unfortunately when I run 'find' it further surrounds the double quotes with single... (6 Replies)
Discussion started by: stevensw
6 Replies

5. UNIX for Dummies Questions & Answers

Using regex in find command

I don't understand why the following simple command is not working: find assign{1,2}Variations don't work, either: find assign+ find assign? ls assignAll I am trying to do is make an alias for running a C++ program of the name assign# or assign##. But the regular expressions aren't working. @_@... (27 Replies)
Discussion started by: Zucriy Amsuna
27 Replies

6. Shell Programming and Scripting

regex help with 'find'

How to do alternation using regular expressions in the 'find' command? Like say you want to find all files that do not match the names specifically "this" or "that" within a directory using regular expressions? (10 Replies)
Discussion started by: stevensw
10 Replies

7. Shell Programming and Scripting

find regex and remove #

hi , how do i remove # from a line where i found regex.. don't need to remove all the line.. only remove comment.. (3 Replies)
Discussion started by: Poki
3 Replies

8. Shell Programming and Scripting

Find regex

Hi There, Can anybody help me out for searching this regular expression? xxxxx.yyy.zzzz.From-ABCD.To-XYZ.xxxxxx I would like the ID1 and ID2 (knowing which one is Id1 and id2) .From-<ID1>. and .To-<ID2>. Thanks in advance!! Regards, Bhaskar (4 Replies)
Discussion started by: bhaskar_m
4 Replies

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

10. Shell Programming and Scripting

Find with RegEx

I have some files in unix ls -1 TMH.backend.tar.421E-03.Z TMH.backend.tar.421E-04.Z TMH.backend.tar.421E-05.Z TMH.backend.tar.421E-06.Z TMH.backend.tar.421E-07.Z TMH.backend.tar.421E-08.Z TMH.backend.tar.421E-08.Z.bak20081223164844 TMH.backend.tar.421E-09.Z... (1 Reply)
Discussion started by: on9west
1 Replies
Login or Register to Ask a Question