我必须在linux中编写一个shell脚本,其中我必须提取给定路径的第二个最后一个文件夹的名称。例如: -
/var/www/html/folder1/folder2/folder3
如何使用命令获取第二个最后一个文件夹“folder2”的名称?
Note: My shell script is placed at root (/var/www/html)
答案 0 :(得分:1)
使用awk:
awk -F/ '{print $(NF-1)}' <<< "/var/www/html/folder1/folder2/folder3"
或者,请致电basename
上的dirname
。
basename "$(dirname /var/www/html/folder1/folder2/folder3)"
答案 1 :(得分:1)
你可以用sed来获取它:
export some_path="/var/www/html/folder1/folder2/folder3"
export folder_place2=`echo $some_path | sed -e "s/.*\/\([^/]*\)\/[^/]*/\1/"`
echo $folder_place2