Reputation: 1
I am having some trouble in doing some comands on shell.
My problem is that I want to change directories more specifically to a directory which I don't know but that contains the file named xxx.
How can I change directly to that directory that contains that file?
If I knew the names of the directories that contained that file would be easier because I only had to use cd ~/Name of directory.
Can anyone help me?
thanks
Upvotes: 0
Views: 255
Reputation: 359855
If you have GNU find
:
cd "$(find /startdir -name 'filename' -printf %h -quit)"
You can replace "/startdir" with any valid directory, for example /
, .
or `~/.
If you want to cd
to a directory which is in the $PATH
that contains an executable file:
cd "$(dirname "$(type -P "filename")")" # Bash
or
cd "$(f=$(type -P "ksh"); echo "${f%/*}")" # Bash
or
cd "$(dirname "$(which "filename")")"
Upvotes: 2
Reputation: 19482
BASH
cd `find . -name "*filename*" | head -1`
This is kind of a variation to Qiau answer. Finds the first file which contains the string filename and then change the current directory to its location.
* is a wild card, there may be something before and/or after filename.
Upvotes: 0
Reputation: 6165
In several linux systems you could do:
$ cd `find . -name "filename" | xargs dirname`
But change "filename" to the file you want to find.
Upvotes: 1
Reputation: 93410
If you don't know where a file is, go to the root of the system and find it:
cd /
find . -iname filename
Upvotes: 1