0

I have searched one file "portmap" inside root directory as follows:

find -name "portmap"

It gives location of file

.init/...somepath./portmap

Now I want to change my current directory to the location of portmap file and print the present working directory.

So I am thinking of pipeline the above location to cd. But how can I do it with one command?

Please help

techfun
  • 417
  • 3
  • 7
  • 10

2 Answers2

1

Presuming that find finds one and only one match for the search pattern, you can use

cd "$( dirname "$( find -name "portmap" )" )"

If at any time you want to do to the directory enumerated in the output of the previous command, you can use

cd "$( dirname "$(!!)" )"
slhck
  • 223,558
  • 70
  • 607
  • 592
DopeGhoti
  • 593
  • 2
  • 7
  • Hello, Thanks for the reply. But it gives me error basedir command not found. I just copy pasted above line. Does this work in mint command prompt? Thanks – techfun Jan 23 '14 at 19:42
  • 1
    @techfun I think DopeGhoti meant `dirname`. Also, you should double-quote the substitutions to prevent paths with spaces from breaking the command. – slhck Jan 23 '14 at 20:56
  • Yes, I did, thanks @slhck. For some reason, I get the `basename` and `dirname` wires crossed in my head every time. – DopeGhoti Jan 23 '14 at 21:43
  • Happy to have helped. – DopeGhoti Jan 23 '14 at 22:09
-1

The xargs command is perfect for this, but the cd command in the shell doesn't play well with it. So I'd use back ticks:

cd `find . -type d -name "portmap"`
joe
  • 24
  • 1
  • The OP wants to go to the directory where the file `portmap` is contained. Your command does not achieve that. Also, double quotes should be used when substituting file paths. – slhck Jan 24 '14 at 18:30