简体   繁体   English

内联转到父目录

[英]Go to parent directory inline

I was wondering if it's possible to go to parent directory of a file inline. 我想知道是否可以内联到文件的父目录。 I know I could type cd .. and it would. 我知道我可以输入cd ..它将。 However, what if I wanted do something like echo $(find . -name “xyz.png”).. and it would return the parent directory of the file instead of the path to file. 但是,如果我想做类似echo $(find . -name “xyz.png”).. ,它将返回文件的父目录而不是文件的路径。 Or instead of a file I search for a folder, and want to return path to the parent directory. 或代替文件,我搜索一个文件夹,并想返回到父目录的路径。

You could use dirname to strip off the last part of a path. 您可以使用dirname删除路径的最后一部分。 Combined with find in your examples it would give you just the parent directory of whatever was found. 结合示例中的find ,它将为您提供所发现内容的父目录。 You could use that in cd as in cd $(find -name "xyz.png" | xargs dirname) if that's the sort of thing you're trying to do. 如果这是您要尝试的操作,则可以像在cd $(find -name "xyz.png" | xargs dirname)中那样在cd $(find -name "xyz.png" | xargs dirname)使用它。

You can also use the -type d option to find to have it only find directories if you want to match directory names instead of filenames. 如果要匹配目录名而不是文件名,也可以使用-type d选项进行查找,以使其仅找到目录。

UNTESTED: 未测试:

  function dirs_ofFile
  {
      find -name "$1" | xargs dirname 
  }

then 然后

$ cd $(dirs_ofFile xyz.png | sed 1q)   # in case there is more than one.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM