디렉터리 및 하위 디렉터리에서 stat 명령을 실행하고 최신 파일만 인쇄하는 스크립트를 작성하는 방법

디렉터리 및 하위 디렉터리에서 stat 명령을 실행하고 최신 파일만 인쇄하는 스크립트를 작성하는 방법

디렉토리에서 최신 파일을 찾는 방법은 무엇입니까? 내 스크립트는 디렉토리의 최신 파일에 대한 추가 출력을 제공합니다.

#!/bin/bash
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
 (stat -c %y  "$directory"/* | tail -n 1)
done
 for D in "$entry"
 do
 (ls -ltr "$D" | tail -n 1)
done

현재 출력:

2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
2018-02-19 12:24:19.842748830 -0500
-rw-r--r-- 1 root root 0 Feb 19 12:19 test3.xml

내 디렉토리 구조는 다음과 같습니다

$ pwd
/nfs/test_library/myfolder/test

$ ls -ltr test
1.0  2.0  3.0

$ ls -ltr 1.0
test1.xml
$ ls -ltr 2.0
test2.xml
$ ls -ltr 3.0
test3.xml(which is the most recent file)

그래서 인쇄용으로만 스크립트를 만들어야 하는데test3.xml

답변1

나는 다음을 통해 당신이 원하는 것을 성취할 수 있었습니다.

암소 비슷한 일종의 영양stat

read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
    find "$directory" -type f -exec stat --printf='%Y\t%n\n' {} \; | sort -n -k1,1 | tail -1
else
    echo "Error, please only specify a directory"
fi

BSDstat

read -rp "Please type in the directory you want all the files to be listed" directory
if [ -d "$directory" ]; then
    find "$directory" -type f -exec stat -F -t '%s' {} \; | sort -n -k6,6 | tail -1
else
    echo "Error, please only specify a directory"
fi

지정된 디렉터리의 모든 파일을 재귀적으로 찾습니다. 그런 다음 UNIX EPOCH 타임스탬프 형식의 수정 타임스탬프를 사용하여 이를 계산합니다. 그런 다음 이 타임스탬프 필드를 기준으로 정렬합니다. 마지막에는 마지막 결과(가장 최근에 업데이트된 파일)만 인쇄됩니다.

관련 정보