1

我必须在 linux 中编写一个 shell 脚本,我必须在其中提取给定路径的倒数第二个文件夹的名称。例如:-

/var/www/html/folder1/folder2/folder3

如何使用命令仅获取倒数第二个文件夹“folder2”的名称?

Note: My shell script is placed at root (/var/www/html)
4

2 回答 2

2

使用 awk:

awk -F/ '{print $(NF-1)}' <<< "/var/www/html/folder1/folder2/folder3"

或者,调用basename.dirname

basename "$(dirname /var/www/html/folder1/folder2/folder3)"
于 2012-11-07T09:51:12.267 回答
1

您可以使用 sed 来获取它:

export some_path="/var/www/html/folder1/folder2/folder3"
export folder_place2=`echo $some_path  | sed -e "s/.*\/\([^/]*\)\/[^/]*/\1/"`
echo $folder_place2
于 2012-11-07T09:57:20.653 回答