I just ran grep -ri foo someDir/

I got back

someDir//foo/bar/baz: onDelete() {

Does the double forward slash just mean that everything to the left of it was in the dir argument you provided to grep?

  • I think this problem will not occur if you do this: grep -ri delete someDir ...? – Luciano Andress Martini Jun 9 '16 at 17:55

When you run the command:

grep -r <pattern> <directory>

it prints all the filenames as <directory>/<filename>. If you put a slash at the end of <directory>, it will be included in that, so you end up with a double slash.

The code could have been smart enough to notice when the original directory name ends with / and omit it when printing filenames, but they didn't bother. I think the only special case it makes is for the root directory, e.g.

grep -r pattern /

will print /foo/bar/baz rather than //foo/bar/baz. This is because POSIX specifies that a sequence of slashes is treated equivalently to a single slash except that two slashes at the beginning have implementation-dependent meaning (this is because some network file system mechanisms made use of syntax like //server/pathname). See How does Linux handle multiple consecutive path separators (/home////username///file)?

| improve this answer | |
  • +1. worth noting is that (with the possible exception of some badly written scripts) unix doesn't care if a pathname has extra /s in it. /path/to/filename is functionally the same as ////path//to/////////////filename. This is extremely useful when writing scripts that work with directory and filenames in variables, just append them to each other with an extra / and it'll Just Work. also worth noting is that most URI types require protocolname:// at the start....the double-/ matters there. – cas Jun 10 '16 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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