shade917
shade917

Reputation: 105

Find command in linux

Okay so I am running a simple command like this:

find / -name ssh | grep bin

the output of that is this:

/usr/bin/ssh

Now I want to make it look like this for when I cd to it

/usr/bin/

I can't figure out how to make it smart because I can brute force it to work only for this one, but what about when I want to run this same code on a different machine and the location of ssh is elsewhere.

Upvotes: 0

Views: 399

Answers (7)

Michael Besteck
Michael Besteck

Reputation: 2423

The dirname program, part of the coreutils, may be used to strip off the last part in the pathname sequence:

dirname `find / -name ssh | grep bin`

will output

/usr/bin

then.

Upvotes: 0

Levon
Levon

Reputation: 143152

If you are trying to find where the ssh command lives you have several options:

  which ssh

and

  whereis ssh

will give you this information (which will give you a single path, while whereis will contain paths with all references to ssh)

As for the find command, change the (starting) directory you specify in the find command:

  find /usr/bin -name ssh

will start its search in the /usr/bin directory.

Is this what you are trying to do? I'm not 100% sure I understood the last part of your post. If not, could you consider rephrasing it please?

Upvotes: 0

nav_jan
nav_jan

Reputation: 2553

find / -name ssh | grep bin|sed "s/ssh//g"

Upvotes: 0

tuxuday
tuxuday

Reputation: 3037

You can use xargs to obtain directory portion of the command.

find / | filter1 | xargs -I_VAR_ dirname _VAR_

Upvotes: 0

Kenneth Hoste
Kenneth Hoste

Reputation: 2971

Is dirname what you're looking for?

$ dirname `find / -name ssh | grep bin | head -1`
/usr/bin

The head -1 part is only to make sure only 1 thing gets passed to dirname, otherwise it will fail.

Upvotes: 2

Marc B
Marc B

Reputation: 360872

find / -name ssh|grep bin|xargs dirname

Upvotes: 0

Wrikken
Wrikken

Reputation: 70540

Don't you want this?

cd $(dirname $(which ssh));

Upvotes: 3

Related Questions