[英]How to obtain basename in ruby from the given file path in unix or windows format?
[英]Unix Command to get file path without basename
我在unix目錄中有以下文件
/home/files/myfiles/good.txt
我需要單獨提取文件路徑
預期輸出 : /home/files/myfiles
注意 :我不能使用剪切操作,因為文件路徑是動態的。 我將文件名和文件路徑作為shell腳本的單個用戶輸入
嘗試
$ dirname /home/files/myfiles/good.txt
應該做的工作:
echo /home/files/myfiles/good.txt | sed s,/*[^/]*$,,
在不生成其他進程的情況下,您可以使用參數擴展 :
file=/home/files/myfiles/good.txt
echo "${file%/*}"
/home/files/myfiles
FWIW, cut
CAN可以在rev
幫助下動態完成
$ echo "/home/files/myfiles/good.txt" | rev | cut -d/ -f2- | rev
/home/files/myfiles
$ echo "/home/files/myfiles/yourfiles/good.txt" | rev | cut -d/ -f2- | rev
/home/files/myfiles/yourfiles
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.