searching content of files in the current and sub directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting searching content of files in the current and sub directories
# 1  
Old 01-23-2008
searching content of files in the current and sub directories

Hi
I was wondering why command 2 doesn't work like command 1 below.

1.
find . -exec grep "test" '{}' \; -print

2.
ls -R | grep "test"

I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2?

Thanks in advance for your help
# 2  
Old 01-23-2008
Quote:
Originally Posted by tiger99
Hi
I was wondering why command 2 doesn't work like command 1 below.

1.
find . -exec grep "test" '{}' \; -print

2.
ls -R | grep "test"

I am trying to search "test" from all the files in the current and sub directories. What's wrong with my command 2?

Thanks in advance for your help
command 2 searches for filenames that have the string "test" as part of their name while command 1 searches for the string "test" inside each of the files.

Code:
ls -R | xargs grep "test"  #this makes command 2 like command 1

# 3  
Old 01-23-2008
Hi shamrock
Thanks for your help

However the code you suggested
ls -R | xargs grep "test"

gives me a huge list with ending "No such file or directory".
i.e.
grep: expr.sh: No such file or direcotory

Is it possible to only display the directory and file name when the text that I am searching is found?

Thanks
# 4  
Old 01-23-2008
try this :

find . | xargs grep -l "test"
# 5  
Old 01-23-2008
2>/dev/null

Hi,

For the "No such file or directory", it is becuase there are error messages print to the stand output, you can ignore them by adding

Code:
2>/dev/null

at the end of the command,which will stop printing all the error message to stand output which is our screen.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Searching for a files based on current date directory

Hi All, I've been trying to do some recursive searching but not been very successful. Can someone please help. Scenario: I have directory structure /dir1/dir2/dir3/ 2019/ 11/ 17 18 19 20 so what I want to do is run a script and as its 2019/11/18/ today it would go and only search... (3 Replies)
Discussion started by: israr75
3 Replies

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

3. Red Hat

Moving of file content to another two files after searching with specific pattern

Hello, Please help me with this!! Thanks in advance!! I have a file named file.gc with the content: 1-- Mon Sep 10 08:53:09 CDT 2012 2revoke connect from FR2261; 3delete from mkt_allow where grantee = 'FR2261'; 4grant connect to FR2261 with '******'; 5alter user FR2261 comment... (0 Replies)
Discussion started by: raosr020
0 Replies

4. Shell Programming and Scripting

Find specific files only in current directory...not sub directories AIX

Hi, I have to find specific 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 am using the below command. And i am... (2 Replies)
Discussion started by: aakishore
2 Replies

5. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

6. Shell Programming and Scripting

mget * (obtein files from current directory but not the files form sub-directories)

Hello, Using the instruction mget (within ftp) and with "Interactive mode off", I want to get all files from directory (DirAA), but not the files in sub-directories. The files names don't follow any defined rule, so they can be just letters without (.) period Directory structure example: ... (0 Replies)
Discussion started by: Peter321
0 Replies

7. UNIX for Dummies Questions & Answers

List files that are not directories from current directory

I can't manage to list all files that are not directories from current directory. (2 Replies)
Discussion started by: beni22sof
2 Replies

8. Shell Programming and Scripting

searching files through all subdirectories beneath the current directory

i want to make a bash script that searches a specific pattern in files through all subdirectories beneath the current directory..without using the command grep-R but only the command grep.. e.g for i in * do grep "pattern" $i ..... ... done using the character (*) the script... (5 Replies)
Discussion started by: milagros
5 Replies

9. UNIX for Dummies Questions & Answers

searching for content of files

Hi, This question may be quite newbish. I've stored a few files on my Unix system and am wondering how to search for their contents (i.e. I input the keyword and get a list of files with this keyword) I'd then like to put it on my website (php). I thought of find and grep, but am not... (19 Replies)
Discussion started by: Aretai
19 Replies

10. Shell Programming and Scripting

Searching for files over 30 days old in current directory

May be a simple question for experts here.... I need to get the list of files older than 30 days in the current folder. I tried "find", but it searches recursively in all the sub directories. Can I restrict the recursive search and extract the files only from current directory ? (18 Replies)
Discussion started by: cxredd4
18 Replies
Login or Register to Ask a Question