Programming

Bash에서 파일의 마지막 수정 날짜를 인쇄하십시오.

procodes 2020. 7. 16. 20:02
반응형

Bash에서 파일의 마지막 수정 날짜를 인쇄하십시오.


파일 날짜를 인쇄하는 방법을 찾지 못하는 것 같습니다. 지금까지 디렉토리의 모든 파일을 인쇄 할 수 있었지만 날짜를 인쇄해야합니다.

항목의 에코와 함께 날짜 형식을 첨부해야하지만 올바른 형식을 찾을 수 없습니다.

echo "Please type in the directory you want all the files to be listed"

read directory 

for entry in "$directory"/*
do
  echo "$entry"
done

당신은 stat명령을 사용할 수 있습니다

stat -c %y "$entry"

더 많은 정보

사람이 읽을 수있는 마지막 수정 시간 % y

'date'명령이 훨씬 간단하지 않습니까? awk, stat 등 불필요

date -r <filename>

또한 날짜 형식 에 대해서는 매뉴얼 페이지참조하십시오 . 예를 들어 일반적인 날짜 및 시간 형식의 경우 :

date -r <filename> "+%m-%d-%Y %H:%M:%S"

OS X에서 날짜 YYYY-MM-DD HH:MM는 파일 출력 형식으로 되어 있습니다.

따라서 파일을 지정하려면 다음을 사용하십시오.

stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename]

다양한 파일에서 실행하려면 다음과 같이 할 수 있습니다.

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
done

이 예제는 sudo periodic daily weekly monthly로그 파일을 참조하여 명령을 마지막으로 실행할 때 인쇄 합니다.


각 날짜 아래에 파일 이름을 추가하려면 대신 다음을 실행하십시오.

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
  echo "$i"
done

결과는 다음과 같습니다.

2016-40-01 16:40
/var/log/daily.out
2016-40-01 16:40
/var/log/monthly.out
2016-40-01 16:40
/var/log/weekly.out

불행히도 줄 바꿈을 방지하고 스크립트에 줄을 추가하지 않고 파일 이름을 날짜 끝에 추가하는 방법을 모르겠습니다.


추신-나는 #!/usr/bin/env bash매일 파이썬 사용자로 사용하고 bash대신 시스템에 다른 버전의 시스템 설치했습니다.#!/bin/bash


최고 date -r filename +"%Y-%m-%d %H:%M:%S"


@StevePenny 답변에 추가하여 사람이 읽을 수없는 부분을 잘라 내고 싶을 수도 있습니다.

stat -c%y Localizable.strings | cut -d'.' -f1

파일의 수정 날짜를 YYYYMMDDHHMMSS형식 으로 얻으려고했습니다 . 내가 한 방법은 다음과 같습니다.

date -d @$( stat -c %Y myfile.css ) +%Y%m%d%H%M%S

설명. 다음 명령의 조합입니다.

stat -c %Y myfile.css # Get the modification date as a timestamp
date -d @1503989421 +%Y%m%d%H%M%S # Convert the date (from timestamp)

EDITED: turns out that I had forgotten the quotes needed for $entry in order to print correctly and not give the "no such file or directory" error. Thank you all so much for helping me!

Here is my final code:

    echo "Please type in the directory you want all the files to be listed with last modified dates" #bash can't find file creation dates

read directory

for entry in "$directory"/*

do
modDate=$(stat -c %y "$entry") #%y = last modified. Qoutes are needed otherwise spaces in file name with give error of "no such file"
modDate=${modDate%% *} #%% takes off everything off the string after the date to make it look pretty
echo $entry:$modDate

Prints out like this:

/home/joanne/Dropbox/cheat sheet.docx:2012-03-14
/home/joanne/Dropbox/Comp:2013-05-05
/home/joanne/Dropbox/Comp 150 java.zip:2013-02-11
/home/joanne/Dropbox/Comp 151 Java 2.zip:2013-02-11
/home/joanne/Dropbox/Comp 162 Assembly Language.zip:2013-02-11
/home/joanne/Dropbox/Comp 262 Comp Architecture.zip:2012-12-12
/home/joanne/Dropbox/Comp 345 Image Processing.zip:2013-02-11
/home/joanne/Dropbox/Comp 362 Operating Systems:2013-05-05
/home/joanne/Dropbox/Comp 447 Societal Issues.zip:2013-02-11

For the line breaks i edited your code to get something with no line breaks.

    #!/bin/bash
    for i in /Users/anthonykiggundu/Sites/rku-it/*; do
       t=$(stat -f "%Sm"  -t "%Y-%m-%d %H:%M" "$i")
       echo $t : "${i##*/}"  # t only contains date last modified, then only filename 'grokked'- else $i alone is abs. path           
    done

If file name has no spaces:

ls -l <dir> | awk '{print $6, " ", $7, " ", $8, " ", $9 }'

This prints as the following format:

 Dec   21   20:03   a1.out
 Dec   21   20:04   a.cpp

If file names have space (you can use the following command for file names with no spaces too, just it looks complicated/ugly than the former):

 ls -l <dir> | awk '{printf ("%s %s %s ",  $6,  $7, $8); for (i=9;   i<=NF; i++){ printf ("%s ", $i)}; printf ("\n")}'

You can use:

ls -lrt filename |awk '{print "%02d",$7}'

This will display the date in 2 digits.

If between 1 to 9 it adds "0" prefix to it and converts to 01 - 09.

Hope this meets the expectation.

참고URL : https://stackoverflow.com/questions/16391208/print-a-files-last-modified-date-in-bash

반응형