linux檔案相關命令 透過檔案獲取父資料夾名稱
不說廢話, 先上命令
透過檔案獲取父資料夾名稱
dirname $(realpath `your_filename`)
透過檔案路徑得到檔名稱
basename `your_filename`
相關命令
basename
dirname
realpath
reallink
ls
pwd
透過檔案獲取父資料夾名稱
命令是這個:
dirname $(realpath `your_filename`)
為什麼需要這樣寫: 直接用
dirname
不行嗎? 請看示例:aquawius@QU-openSuse-wsl ~/.c/fish> pwd /home/aquawius/.config/fish aquawius@QU-openSuse-wsl ~/.c/fish> ls completions/ conf.d/ config.fish fish_variables functions/ aquawius@QU-openSuse-wsl ~/.c/fish> dirname . . aquawius@QU-openSuse-wsl ~/.c/fish> dirname config.fish . aquawius@QU-openSuse-wsl ~/.c/fish> realpath config.fish /home/aquawius/.config/fish/config.fish aquawius@QU-openSuse-wsl ~/.c/fish> dirname $(realpath config.fish) /home/aquawius/.config/fish
似乎dirname需要得到一個絕對路徑的名稱才能得到這個檔案的父資料夾路徑. 將兩個命令合用即可.
透過檔案路徑得到檔名稱
命令是這個:
basename `your_filename`
請看示例:
aquawius@QU-openSuse-wsl ~/.c/fish> basename ./config.fish config.fish aquawius@QU-openSuse-wsl ~/.c/fish> basename /home/aquawius/.config/fish/config.fish config.fish