2012-10-09 24 views

回答

1
function latest { 
    if [[ $FUNCNAME == ${FUNCNAME[1]} ]]; then 
     unset -v x latest files 
     printf -v "[email protected]" 
    elif (($# > 2)); then 
     printf '%s\n' "Usage: $FUNCNAME <glob> <varname>" 'Error: Takes at most 2 arguments. Glob defaults to *' 
     return 1 
    else 
     if ! shopt -q nullglob; then 
      typeset -f +t "$FUNCNAME" 
      trap 'shopt -u nullglob; trap - RETURN' RETURN 
      shopt -s nullglob 
     fi 

     IFS= typeset -a 'files=(${1:-*})' 
     typeset latest x 

     for x in "${files[@]}"; do 
      [[ -d $x || $x -ot $latest ]] || latest=$x 
     done 

     ${2:+"$FUNCNAME"} "${2:-printf}" -- %s "$latest" 
    fi 
} 


latest '*HNAZXLCOM*' myVar 
0

如果我讀了你的權利

var="$(ls -t1 *HNAZXLCOM*|head -n 1)" 
+0

或者是包含字符串,該文件或文件名的目錄? –

-1

從人LS,-t選項是相關的。另請參閱-1開關。

-t  sort by modification time, newest first 
-1  list one file per line 

的-1開關是不相關的,因爲自動ls每名輸出到管道時只寫入一行。

在命令提示符處,以下函數將列出最近13個文件;例如,xt *HNAZXLCOM*會在其名稱中列出13個最近的文件,而xt本身將列出當前工作目錄中的最新文件(任何模式)。根據您的喜好調整數量。一些版本的頭可能需要-n 13而不是-13。在你的應用中,
H=$(ls -t *HNAZXLCOM* | head -n 1)
是合適的。

xt() 
{ 
    date; 
    ls --color -Glt --color --time-style="+%Y-%m-%d %T" $* | grep -v "/" | head -13 
} 
相關問題