如何获得在Linux中倒数第二个文件夹的名称(How to get name of second l

2019-09-27 20:05发布

我必须写在Linux shell脚本中,我不得不拉定路径的倒数第二个文件夹的名称。 例如:-

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

我怎样才能获得倒数第二个文件夹“文件夹2”使用命令只叫什么名字?

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

Answer 1:

用awk:

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

另外,调用basenamedirname

basename "$(dirname /var/www/html/folder1/folder2/folder3)"


Answer 2:

你可以使用sed来得到它:

export some_path="/var/www/html/folder1/folder2/folder3"
export folder_place2=`echo $some_path  | sed -e "s/.*\/\([^/]*\)\/[^/]*/\1/"`
echo $folder_place2


文章来源: How to get name of second last folder in linux
标签: linux command