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