Pass output of one command as input to other

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

Posted 2014-01-23T19:09:53.283

Reputation: 327

Answers

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 "$(!!)" )"

DopeGhoti

Posted 2014-01-23T19:09:53.283

Reputation: 543

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 – 2014-01-23T19:42:15.537

1@techfun I think DopeGhoti meant dirname. Also, you should double-quote the substitutions to prevent paths with spaces from breaking the command. – slhck – 2014-01-23T20:56:23.403

Yes, I did, thanks @slhck. For some reason, I get the basename and dirname wires crossed in my head every time. – DopeGhoti – 2014-01-23T21:43:15.257

Happy to have helped. – DopeGhoti – 2014-01-23T22:09:35.517

-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

Posted 2014-01-23T19:09:53.283

Reputation: 24

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 – 2014-01-24T18:30:34.223