Using find and regular expressions


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Using find and regular expressions
# 1  
Old 12-27-2012
Using find and regular expressions

Hi

Could you please advise how can one extract from the output of
Code:
    find . -name "*.c" -print

only filenames in the current direcotry and not in its subdirectories?

I tried using (on Linux x86_64)
Code:
   find . -name "*.c" -prune

but it is not giving correct output.

Whereas I am getting following output

Code:
./file1.c
./child_dir/file1.c
./child_dir/file4.c
./child_dir/file2.c
./child_dir/file3.c
./file4.c
./file2.c
./file3.c


Other than prune, is it possible to pipe GREP with find to get desired output?

Last edited by Scrutinizer; 01-09-2013 at 05:12 PM.. Reason: code tags
# 2  
Old 12-27-2012
I think you can use "maxdepth" flag Smilie
# 3  
Old 12-27-2012
Code:
find ./* -prune -name "*.c" -type f

# 4  
Old 12-27-2012
Dear Bipinajith,

I think -type f may not be required as following is also working

Code:
find ./* -prune -name "*.c"

./file1.c
./file2.c
./file3.c
./file4.c

Kindly correct if I am wrong.
# 5  
Old 12-27-2012
You are right.

-type f will make sure that find command returns only regular files in the output. I think it is a good practice to specify this option if you are looking only for regular files.
# 6  
Old 12-27-2012
Any idea how to we do same job by using GREP?
Reason for request: Just wana check how grep behave?

Thanks in advance,
# 7  
Old 12-27-2012
Using grep:
Code:
find . -name "*.c" | grep -E '^\.\/[a-zA-Z0-9_-]+\.c'

Using awk:
Code:
find . -name "*.c" | awk -F"/" 'NF==2'

But find without -prune option is going to be slower because it has to descend into each sub-directories and search files.

So I don't recommend using these commands.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

2. UNIX for Dummies Questions & Answers

Regular Expressions -- Find spaces outside

Hello, I need help with using grep and regular expressions.... I have a long list of about 1000 lines of Chinese flashcards. Here's a small excerpt: 意文 yìwén (given name) 貴姓 guìxìng (honorable surname) 貴 guì (honorable) 姓 xìng (one's surname is; to be surnamed; surname) 呢 ne (interrogative... (2 Replies)
Discussion started by: arduino411
2 Replies

3. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

4. Shell Programming and Scripting

Regular Expressions

what elements does " /^/ " match? I did the test which indicates that it matches single lowercase character like 'a','b' etc. and '1','2' etc. But I really confused with that. Because, "/^abc/" matches strings like "abcedf" or "abcddddee". So, what does caret ^ really mean? Any response... (2 Replies)
Discussion started by: DavidHe
2 Replies

5. UNIX for Dummies Questions & Answers

Regular expressions

In regular expressions with grep(or egrep), ^ works if we want something in starting of line..but what if we write ^^^ or ^ for pattern matching??..Hope u all r familiar with regular expressions for pattern matching.. (1 Reply)
Discussion started by: aadi_uni
1 Replies

6. UNIX for Advanced & Expert Users

regular expressions

I have a flat file with the following drug names Nutropin AQ 20mg PEN Cart 2ml Norditropin Cart 15mg/1.5ml I have to extract digits that are before mg i.e 20 and 15 ; how to do this using regular expressions Thanks ram (1 Reply)
Discussion started by: ramky79
1 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Shell Programming and Scripting

regular expressions

Hi, can anyone advise me how to shorten this: if || ; then I tried but it dosent seem to work, whats the correct way. Cheers (4 Replies)
Discussion started by: jack1981
4 Replies

10. Shell Programming and Scripting

Regular Expressions

How can i create a regular expression which can detect a new line charcter followed by a special character say * and replace these both by a string of zero length? Eg: Input File san.txt hello hi ... (6 Replies)
Discussion started by: sandeep_hi
6 Replies
Login or Register to Ask a Question