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.
你不想要这个吗
cd $(dirname $(which ssh));
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.
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?
find / -name ssh|grep bin|xargs dirname
您可以使用xargs获取命令的目录部分。
find / | filter1 | xargs -I_VAR_ dirname _VAR_
find / -name ssh | grep bin|sed "s/ssh//g"
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.