![디렉터리 및 하위 디렉터리에서 stat 명령을 실행하고 최신 파일만 인쇄하는 스크립트를 작성하는 방법](https://linux55.com/image/128459/%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%20%EB%B0%8F%20%ED%95%98%EC%9C%84%20%EB%94%94%EB%A0%89%ED%84%B0%EB%A6%AC%EC%97%90%EC%84%9C%20stat%20%EB%AA%85%EB%A0%B9%EC%9D%84%20%EC%8B%A4%ED%96%89%ED%95%98%EA%B3%A0%20%EC%B5%9C%EC%8B%A0%20%ED%8C%8C%EC%9D%BC%EB%A7%8C%20%EC%9D%B8%EC%87%84%ED%95%98%EB%8A%94%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EB%A5%BC%20%EC%9E%91%EC%84%B1%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95.png)
디렉토리에서 최신 파일을 찾는 방법은 무엇입니까? 내 스크립트는 디렉토리의 최신 파일에 대한 추가 출력을 제공합니다.
#!/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 타임스탬프 형식의 수정 타임스탬프를 사용하여 이를 계산합니다. 그런 다음 이 타임스탬프 필드를 기준으로 정렬합니다. 마지막에는 마지막 결과(가장 최근에 업데이트된 파일)만 인쇄됩니다.