Results 1 to 8 of 8
Why won't find cd to a dir I find with find?
Is it because cd is a builtin command?
How does one get around this as 'cd'ing to a obscure ...
- 04-04-2010 #1
Find won't -exec cd {} \;
Why won't find cd to a dir I find with find?
Is it because cd is a builtin command?
How does one get around this as 'cd'ing to a obscure dir would be much easier using -exec rather than copy and past or worse yet, having to type and TAB my through.
An example:
One can see that find returns the right value and cd seems to work right. Why can't they play together nicely?Code:red@wallunit 1004~>find ~ -maxdepth 1 -type d -name Desktop /home/red/Desktop red@wallunit 1005~>find ~ -maxdepth 1 -type d -name Desktop -exec cd {} \; find: `cd': No such file or directory red@wallunit 1006~>cd $(find ~ -maxdepth 1 -type d -name Desktop) red@wallunit 1007~/Desktop>
Thanks.
- 04-04-2010 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The current directory is always context sensitive. That is to say when the executing program (such as a shell, or a find command) terminates, then the current directory reverts to whatever was in effect when you started the program. So, in your case, the effects of 'cd' will not be seen once that -exec (which starts a shell) is complete.
However, in your case, as you can see from the error message emitted by 'find', it is not finding the 'cd' command, which as you thought is a shell built-in command, otherwise the shell would never be able to cd to another directory.
All that aside, perhaps you should let us know what exactly you are trying to accomplish?Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-04-2010 #3
So if I could, say, use the -exec option to open another bash window with the prompt in the dir I want that may work. But using the example I provided above, I open a subshell, cd to the dir find found and then that subshell closes without a new window, and then I am back to the same prompt. So the command works but not like I think it should. That is why opening a separate instance of bash may do what I want.
Am I on the right track?
What I want to do is go to a dir I know the name of but is long and nested in the tree.
For instance,
I have a dir named:
That's quite a bit of "type -n-tabbing" to cd there from ~. It would be nice to simply find then cd to the dir in one quick shot.Code:/home/red/grateful_dead_project/gd_1980_project/gd_1980_project_01/gd80-04-01.sbd.bertha-ashley.26178.sbeok.shnf
Like this:
But the above doesn't work.Code:find ~ -type d -iname '*gd80-04-01*' -exec cd {} \;
I came across this as I was browsing the find man page and started to play with various examples on different web pages that give examples. rm works. chmod does too. That's all good but I thought I could put the -exec option to work for me by cd'ing to a nested dir. It's not critical and there should be other ways to do the same thing but I love getting better with bash and the cli so I found this to be a curious exception. What else wouldn't work as one might expect with the -exec option?
I suppose I am just not real clear as to why it doesn't work.
- 04-05-2010 #4
How about something like
--- rod.Code:cd `find -type d -iname '*gd80-04-01*'`
Stuff happens. Then stays happened.
- 04-05-2010 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Not quite there yet. I think you need to use -exec to output what you want to a script file, and then execute that. Alternatively, you can to this to cd to the directory:
This requires that 'find' returns only one item. The backquotes around the 'find' command will pass the returned data to the cd command.Code:cd `find . -type f -name '*gd80-04-01*'`
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-05-2010 #6
That works, as long as the dir name is unique. In my case it is not. I have several dirs with that portion (gd-04-01) included in the dir name. Once I make the name unique it works like a charm!
I like, or I am used to, the $(command here) format over the `command here` format but both work well.
I suppose my question is why -exec cd {} \; doesn't work even if I spell out the full path in the find command:
***Note the use of -path in place of -name
I feel that understanding this will help me to understand the way bash works so that future problems can be interpreted and solved with reason.Code:find ~ -type d -path '/home/red/grateful_dead_project/gd_1980_project/gd_1980_project_01/gd80-04-01.sbd.bertha-ashley.26178.sbeok.shnf' -exec cd {} \; find: `cd': No such file or directory
Thanks
- 04-05-2010 #7
Thanks Rubberman..
I'll have to mull your responses over in my head for a bit. Especially subshells and redirecting find results to a script variable.
I think I enjoy the puzzle of things more than a quick answer. That is the 'why' is more fun than the 'what do I do?'.Last edited by mehorter; 04-05-2010 at 12:59 AM. Reason: spelling error
- 04-05-2010 #8


Reply With Quote
