我將如何修改此代碼以提供代碼目錄中最后修改檔案的完整檔案路徑,包括嵌套的子目錄?
# Gets the last modified file in the code directory.
get_filename(){
cd "$code_directory" || no_code_directory_error # Stop script if directory doesn't exist.
last_modified=$(ls -t | head -n1)
echo "$last_modified"
}
uj5u.com熱心網友回復:
- 使用
find代替ls,因為使用ls是一種反模式。 - 使用Schwartzian 變換為您的資料添加排序鍵前綴。
- 對資料進行排序。
- 你要什么就拿。
- 洗掉排序鍵。
- 后處理資料。
find "$code_directory" -type f -printf '%T@ %p\n' |
sort -rn |
head -1 |
sed 's/^[0-9.]\ //' |
xargs readlink -f
uj5u.com熱心網友回復:
您可以使用該realpath實用程式。
# Gets the last modified file in the code directory.
get_filename(){
cd "$code_directory" || no_code_directory_error # Stop script if directory doesn't exist.
last_modified=$(ls -t | head -1)
echo "$last_modified"
realpath "$last_modified"
}
輸出:
blah.txt
/full/path/to/blah.txt
uj5u.com熱心網友回復:
ls -t按修改時間排序,如果你想要第一個,你可以添加| head -1,R 幫助您遞回地對檔案進行排序,我認為這里唯一的提示是 ls -tR 不會堆疊所有檔案然后對它們進行排序,因此您可以使用
find . -type f -printf "%T@ %f\n" | sort -rn > out.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/420657.html
標籤:
