I was wondering why I get the following output when searching for files with the command:

$ grep -l prac *
grep: Desktop: Is a directory
grep: Documents: Is a directory
grep: Downloads: Is a directory
grep: Music: Is a directory
grep: Pictures: Is a directory

I was wondering why it shows everything else besides the obvious allprac? A couple of the listed directories are empty.

  • Thanks for the reply, but I was wondering why the command doesn't only return with the file named allprac. Your command worked..In your command, is this redirecting the errors to /dev/null? – user120161 Dec 11 '14 at 5:54
  • i've answered below – Малъ Скрылевъ Dec 11 '14 at 5:55

The * forces grep to scan inside all files in the current folder instead of grepping the standard input, but since grep drive not into subfolders it just reports folder ocurriences to stderrm and to disable reporting just redirect stderr to /dev/null (POSIX):

$ grep -l prac * 2>/dev/null

then additional way to avoid reporting of folders is (non-POSIX):

$ grep -l Build --exclude-dir=* *

to scan including subfolders (POSIX):

$ grep -l prac * -r

to grep standard input (POSIX):

$ cat file | grep -l prac

There are many implementation of grep so it may differ to each other in set of keys and default behaviour, but there is a standard POSIX that describes minimum set of key to be implemented by grep (and other soft). However I know nothing about wheither the POSIX define the default search dehaviour for grep. It seems tha unix implementation do nearly to grep -l Build --exclude-dir=* * of linux.

  • Okay, I understand this. Is there a way to modify a start-up file to where I can only use my original command without sending the errors elsewhere? I am curious about this because my school's unix box doesn't require the extra command to not list the errors. – user120161 Dec 11 '14 at 6:00
  • When I log in to my school's unix server, I only need to type grep -l prac * and it works fine. I was confused as to why I would have to type a different command on my linux machine. – user120161 Dec 11 '14 at 6:04
  • Good call on the -r, I didn't even look for that in the man page... guess I should learn to RTFM before using find =O – Chuck R Dec 11 '14 at 8:03
  • @Githlar find is need sometimes when scan over specific filter it requires. however it can be do with duplicate grep also =) – Малъ Скрылевъ Dec 11 '14 at 8:53

@Cremefraiche's answer is right in that it's due to the wildcard. However, his solution doesn't seem to work. Since the '*' shell wildcard matches all files and folders (except dot files), bash ends up sending the names of all files and folders in the current directory as arguments to grep. Since it's not possible to "read in" a directory (similar to how you can't cat a directory), you should exclude the directories. The best way to do this would be to use the find tool.

grep -l prac $(find . -maxdepth 1 -type f)

This would search all regular files (-type f) in the directory. If you want to do a recursive search in the same way, just remove the -maxdepth option. Note, this will not work for files with spaces in their names... still working on a version for that.

  • I was curious as to why I only need to type grep -l prac * on my school's unix box and it works fine. What makes the command different on my machine? – user120161 Dec 11 '14 at 6:08
  • If you mean real Unix, then it's possible that it has to do with some kind of difference between the GNU (grep in Ubuntu is GNU grep) and pure POSIX version, possibly. But, I don't really know the answer. – Chuck R Dec 11 '14 at 8:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.