Suppose I have two different directories with the same folder names:/usr/myDir/ and /home/myDir/If I run a command such as: cd $(find / -type d -name myDir) it navigates to /usr/myDir. Why is that? Where does the find command look first when it comes to special instances like this?
Where does the find command look first if there are directories with the same name?
linux;find;directory
find traverses files in whatever order the filesystem returns. This order is not predictable; creating, removing or renaming a file can change the order of other files in the same directory.It's a toss-up whether find / -type d -name myDir returns /usr/myDir or /home/myDir first, and it could change at any time. (In this specific example, it probably won't change often, because it's rare to write to the root directory, but in general don't count on it.)In some shells (including bash), the cd command ignores all but its first arguments, so your command silently changes the working directory to whatever find returns first. (Your command also wouldn't work for a path containing spaces.) In other shells (such as ksh and zsh), you'd get an error message.