Mac Terminal "find" command : What does a double slash in the result output mean?

I use the find-command for searching in large and nested directory-structures.

In the result-output is sometimes a double-slash ( // ) included.

Example (after "Downloads"):

#> find ~/Downloads/ -iregex ".*some.*"
/Users/michael/Downloads//subDirectory/some_file.pdf

I first thought it would mark the current working-directory. But that isn't the case.

What's the meaning of this double-slash?

Usually I copy the result into the clipboard, change to the finder. Then "shift" + "command" + "g" and pasting the path (until the file) into the box. So that the containing directory is opened.

Works fine. But the double-slash I have to remove manually.

Therefore: How can I avoid it?


Solution 1:

find is rather literal. When you tell it to search within "~/Downloads/", it uses that (including the trailing slash) as the prefix for whatever it finds. Since there's an unnecessary and irrelevant slash at the end of the path you gave it, you wind up with an unnecessary and irrelevant extra slash in the output.

Solution: remove the trailing "/" from the search directory:

#> find ~/Downloads/ -iregex ".*some.*"
/Users/michael/Downloads//subDirectory/some_file.pdf
#> find ~/Downloads -iregex ".*some.*"
/Users/michael/Downloads/subDirectory/some_file.pdf

Solution 2:

What's the meaning of this double-slash?

Means you're using strange old BSD find.

How can I avoid it?

You can indeed omit the trailing slash in your original command, but since it's added automatically via tab completion (which you should definitely make as much use of as possible), & since the trailing slash is actually used in myriad places elsewhere to be sure you're dealing with a directory and not a filename, omitting it is counterintuitive & silly.

Instead, I suggest doing one of these other things:

a) Make a wrapper for find that pipes to sed: | sed 's@//@/@'

b) Install GNU's findutils with 'brew install findutils' and then either use 'gfind' directly, alias 'find' to 'gfind', or add /usr/local/opt/findutils/libexec/gnubin to your path (which also adds GNU findutils' 'locate', 'updatedb', & 'xargs').

c) Switch to a proper GNU/Linux OS that wouldn't have had this problem (& a thousand others) in the first place. :p