如何从文件路径获取文件目录路径? [英] How to get a file directory path from file path?

查看:62
本文介绍了如何从文件路径获取文件目录路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Bash中,如果 VAR ="/home/me/mydir/file.c" ,如何获得"/home/me/mydir" ?

In Bash, if VAR="/home/me/mydir/file.c", how do I get "/home/me/mydir"?

推荐答案

dirname basename 是您正在寻找的提取路径分量的工具:

dirname and basename are the tools you're looking for for extracting path components:

$ export VAR='/home/pax/file.c'
$ echo "$(dirname "${VAR}")" ; echo "$(basename "${VAR}")"
/home/pax
file.c

它们不是内部Bash命令,但它们是POSIX标准的一部分-请参见 basename .因此,它们可能在大多数能够运行 bash 的平台上可用,或者可以在大多数平台上获得.

They're not internal Bash commands but they're part of the POSIX standard - see dirname and basename. Hence, they're probably available on, or can be obtained for, most platforms that are capable of running bash.

这篇关于如何从文件路径获取文件目录路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆