bash 从文件路径获取文件目录路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6121091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 20:33:27  来源:igfitidea点击:

Get file directory path from file path

bash

提问by Talespin_Kit

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

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

回答by paxdiablo

dirnameand basenameare the tools you're looking for for extracting path components:

dirname并且basename是您正在寻找的用于提取路径组件的工具:

$ VAR=/home/me/mydir/file.c

$ DIR=$(dirname "${VAR}")

$ echo "${DIR}"
/home/me/mydir

$ basename "${VAR}"
file.c

They're not internalBash commands but they're part of the POSIX standard (see dirname, basename) and so should be available on the vast majority of systems that will be running Bash.

它们不是内部Bash 命令,但它们是 POSIX 标准的一部分(请参阅dirname, basename),因此应该在将运行 Bash 的绝大多数系统上可用。

回答by Emmanuel Devaux

$ export VAR=/home/me/mydir/file.c
$ export DIR=${VAR%/*}
$ echo "${DIR}"
/home/me/mydir

$ echo "${VAR##*/}"
file.c

To avoid dependency with basenameand dirname

避免依赖basenamedirname

回答by jerblack

On a related note, if you only have the filename or relative path, dirnameon its own won't help. For me, the answer ended up being readlink.

在相关说明中,如果您只有文件名或相对路径,dirname则其本身无济于事。对我来说,答案最终是readlink

fname='txtfile'    
echo $(dirname "$fname")                # output: .
echo $(readlink -f "$fname")            # output: /home/me/work/txtfile

You can then combine the two to get just the directory.

然后,您可以将两者结合起来以获取目录。

echo $(dirname $(readlink -f "$fname")) # output: /home/me/work

回答by Tahsin Turkoz

If you care target files to be symbolic link, firstly you can check it and get the original file. The if clause below may help you.

如果你关心目标文件是符号链接,首先你可以检查它并获取原始文件。下面的 if 子句可能对您有所帮助。

if [ -h $file ]
then
 base=$(dirname $(readlink $file))
else
 base=$(dirname $file)
fi

回答by Eurospoofer

I was playing with this and came up with an alternative.

我正在玩这个并想出了一个替代方案。

$ VAR=/home/me/mydir/file.c

$ DIR=`echo $VAR |xargs dirname`

$ echo $DIR
/home/me/mydir

The part I liked is it was easy to extend backup the tree:

我喜欢的部分是扩展备份树很容易:

$ DIR=`echo $VAR |xargs dirname |xargs dirname |xargs dirname`

$ echo $DIR
/home

回答by kmchen

Here is a script I used for recursive trimming. Replace $1 with the directory you want, of course.

这是我用于递归修剪的脚本。当然,将 $1 替换为您想要的目录。

BASEDIR=""
IFS=$'\n'
cd $BASEDIR
 for f in $(find . -type f -name ' *')
 do 
    DIR=$(dirname "$f")
    DIR=${DIR:1}
    cd $BASEDIR$DIR
    rename 's/^ *//' *
 done

回答by alper

First, I have replaced /with empty space (). Then, I deleted all the characters before any empty space (). At the end, I removed the first character that is empty space ().

首先,我已替换/为空格 ( )。然后,我删除了任何空格 ( )之前的所有字符。最后,我删除了第一个空格 ( )字符。

$ VAR="/home/me/mydir/file.c"

$ echo $VAR | tr '/' ' ' | sed 's/^.* / /' | cut -c2-
file.c

回答by aerijman

HERE=$(cd $(dirname $BASH_SOURCE) && pwd)

where you get the full path with new_path=$(dirname ${BASH_SOURCE[0]}). You change current directory with cdnew_pathand then run pwdto get the full path to the current directory.

在那里您可以使用new_path=获得完整路径$(dirname ${BASH_SOURCE[0]})。您使用cdnew_path更改当前目录,然后运行pwd以获取当前目录的完整路径。