Reputation: 105
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
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
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
Reputation: 3037
You can use xargs to obtain directory portion of the command.
find / | filter1 | xargs -I_VAR_ dirname _VAR_
Upvotes: 0
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