2012-11-07 90 views
0

我必須在Linux中編寫一個shell腳本,在該腳本中,我必須拉出給定路徑的倒數第二個文件夾的名稱。例如: -如何獲取Linux中第二個文件夾的名稱

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

我怎樣才能得到第二個最後的文件夾名稱「folder2」使用命令?

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

回答

1

用awk:

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

或者,撥打basenamedirname

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

你可以用sed來得到它:

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