쉘 스크립트에서 패턴과 일치하는 파일 이동

쉘 스크립트에서 패턴과 일치하는 파일 이동

최신 파일을 "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

관련 정보