如何从文件路径中提取目录路径? [英] How to extract directory path from file path?

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

问题描述

在 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"?

推荐答案

dirnamebasename 是您正在寻找的提取路径组件的工具:

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

$ VAR='/home/pax/file.c'
$ DIR="$(dirname "${VAR}")" ; FILE="$(basename "${VAR}")"
$ echo "[${DIR}] [${FILE}]"
[/home/pax] [file.c]

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

They're not internal bash commands but they are 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天全站免登陆