![쉘 스크립트에서 패턴과 일치하는 파일 이동](https://linux55.com/image/85348/%EC%89%98%20%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C%20%ED%8C%A8%ED%84%B4%EA%B3%BC%20%EC%9D%BC%EC%B9%98%ED%95%98%EB%8A%94%20%ED%8C%8C%EC%9D%BC%20%EC%9D%B4%EB%8F%99.png)
최신 파일을 "ServicesWebApp" 명명 규칙과 일치하는 파일 이름을 가진 디렉터리로 이동해야 합니다.
예: 비슷한 이름을 가진 5개의 파일이 있는 디렉터리가 있습니다.
ServicesWebApp-1005.war created on 3/10/2016
ServicesWebApp-1004.war created on 3/09/2016
ServicesWebApp-1003.war created on 3/08/2016
ServicesWebApp-1002.war created on 3/07/2016
ServicesWebApp-1001.war created on 3/06/2016
최신 디렉터리를 다른 디렉터리로 이동해야 하는데, 이 경우에는 그렇게 합니다. ServicesWebApp-1005.war이 2016년 3월 10일에 생성되었습니다.
답변1
타임스탬프를 신뢰한다면 oneliner를 사용할 수도 있습니다.
mv $(ls -tr ServicesWebApp* | tail -1) /tmp/
또는 파일 이름에 의존하려는 경우.
mv $(ls ServicesWebApp* | sort -n | tail -1) /tmp/
답변2
다음을 시도해 볼 수도 있습니다.
mv $(find . -type f -name "ServicesWebApp*" -printf "%T@ %f\n" | sort -n | awk '{print $2}' | tail -1 ) /new/file/path/
답변3
tstamp=0
file=
for f in ServicesWebApp*
do
y=$(stat -c "%Y" "$f")
if [ $y -gt $tstamp ]
then
file="$f"
tstamp=$y
fi
done
echo cp "$file" /somewhere/else
답변4
단일 행 실행을 사용해 볼 수 있습니다명령 대체
$ mv $(ls -t /location/path/ServicesWebApp* | head -n1) /to/new/destination/path
앞으로 움직이다
$ ls -t /location/path/ServicesWebApp* | head -n1
/location/path/ServicesWebApp-1005.war
따라서 명령은 다음과 같이 해석되어야 합니다.
$ mv /location/path/ServicesWebApp-1005.war /to/new/destination/path