Reputation: 131
find . -name "*.php" | xargs grep -i -n "searchstring" >output.txt
Here I am trying to write data into a file which is not happening...
Upvotes: 10
Views: 66090
Reputation: 1295
Check free disk space by
$ df -Th
It could be not enough free space on your disk.
Upvotes: 0
Reputation: 450
I always use the following command. It displays the output on a console and also creates the file
grep -r "string to be searched" . 2>&1 | tee /your/path/to/file/filename.txt
Upvotes: 0
Reputation: 93636
If you're searching trees of source code, please consider using ack. To do what you're doing in ack, regardless of there being spaces in filenames, you'd do:
ack --php -i searchstring > output.txt
Upvotes: 0
Reputation: 4617
Try using line-buffered
grep --line-buffered
[edit]
I ran your original command on my box and it seems to work fine, so I'm not sure anymore.
Upvotes: 1
Reputation: 11788
I guess that you have spaces in the php filenames. If you hand them to grep
through xargs
in the way that you do, the names get split into parts and grep
interprets those parts as filenames which it then cannot find.
There is a solution for that. find
has a -print0
option that instructs find
to separate results by a NUL byte and xargs
has a -0
option that instructs xargs
to expect a NUL byte as separator. Using those you get:
find . -name "*.php" -print0 | xargs -0 grep -i -n "searchstring" > output.txt
Upvotes: 1
Reputation: 47183
How about appending results using >>
?
find . -name "*.php" | xargs grep -i -n "searchstring" >> output.txt
I haven't got a Linux box with me right now, so I'll try to improvize.
the xargs grep -i -n "searchstring"
bothers me a bit.
Perhaps you meant xargs -I {} grep -i "searchstring" {}
, or just xargs grep -i "searchstring"
?
Since -n
as grep's argument will give you only number lines, I doubt this is what you needed.
This way, your final code would be
find . -name "*.php" | xargs grep -i "searchstring" >> output.txt
Upvotes: 11
Reputation: 43024
find . -name "*.php" -exec grep -i -n "function" {} \; >output.txt
But you won't know what file it came from. You might want:
find . -name "*.php" -exec grep -i -Hn "function" {} \; >output.txt
instead.
Upvotes: 2