파일 생성 날짜를 지정할 수 없습니다.

파일 생성 날짜를 지정할 수 없습니다.

일부 파일을 자동으로 생성하고 싶어서 스크립트를 만들었습니다. 또한 이 파일의 생성 날짜도 지정하고 싶습니다.

예를 들어파일.txt생성 날짜는 다음과 같습니다.2012년 5월 12일touch아래 그림처럼 하면 되는데 ,

touch -d 20120512 file.txt

문서 확인 날짜를 기재하고,

-rw-rw-r-- 1 lenovo lenovo 0 May  12  2012 file.txt

위의 내용을 스크립트에 적용하면 내가 생성하는 파일은 모두 내가 지정한 시간이 아닌 현재 시간이 생성 시간으로 적용됩니다. 내가 여기서 뭘 잘못하고 있는 걸까?

스크립트

#!/bin/bash

##################################
#Generate dat and snapshot files.#
##################################
srv_dir="/home/lenovo/Source/bash/srv"
main_dir="${srv_dir}/main"
database_dir="${main_dir}/Database"
dat_file="${main_dir}/remote.dat"

if [[ -e ${main_dir} ]]; then
    echo "${main_dir} allready exists."
    echo "Aborting..."
    exit 0
fi

# Create directories.
mkdir -p ${database_dir}

# Create files.
if [[ $1 == "--dat-newer" ]]; then
    # User wants dat file to be the latest modified file.
    
    # Create dat file with date as 'now'.
    touch ${dat_file}

    # Create snapshots with older dates.
    touch -d 20210511 "${database_dir}/snapshot001"
    touch -d 20210510 "${database_dir}/snapshot002"
    touch -d 20210512 "${database_dir}/snapshot004"
    touch -d 20210514 "${database_dir}/snapshot003"
else
    # Create an old dat file.
    touch -d 20210512 "${dat_file}"

    # Create snapshots with older dates.
    touch -d 20210511 "${database_dir}/snapshot001"
    touch -d 20210510 "${database_dir}/snapshot002"
    touch -d 20210512 "${database_dir}/snapshot004"

    # Create snapshot003 with date as 'now'.
    touch "${database_dir}/snapshot003"
fi

# populate dat and snapshot files with data.
echo "Data of ${dat_file}" > "${database_dir}/snapshot001"
echo "Data of snapshot001" > "${database_dir}/snapshot001"
echo "Data of snapshot002" > "${database_dir}/snapshot002"
echo "Data of snapshot003" > "${database_dir}/snapshot003"
echo "Data of snapshot004" > "${database_dir}/snapshot004"

답변1

스크립트의 마지막 부분은 각 파일에 기록되며 파일의 마지막 수정 시간이 모두 현재 시간으로 업데이트됩니다. 시간 사용을 변경하려면 다음과 touch같이 해야 합니다.마지막파일에 수행하는 작업입니다.

생성 시간 은 touch변경할 수 없습니다(추적하는 파일 시스템에서는).파일 생성 시간을 변경하는 방법은 무엇입니까? (터치하면 수정시간만 변경됩니다)더 알아보기.

답변2

"또한 이 파일의 생성 날짜를 지정하고 싶습니다."생성 시간("생성 시간" 또는 )이기 때문에 그렇게 할 수 없습니다 btime. 또한 액세스하기 어렵습니다. 특히 lsor 는 아니지만 실제로 액세스하고 싶은지 touch확인하십시오 . 대신에 표시되는 것은 날짜입니다. stat그리고 마지막 수정 시간.

제가 물어보고 싶은 첫 번째 질문은 $database_dir사용할 때 기대되는 가치가 있는지입니다. $dat_file이름에 공백이 포함될 수 있는 경우 큰따옴표로 묶어야 합니다(어쨌든 이는 좋은 습관입니다). 예를 들어,

touch -d 20210511 "${database_dir}/snapshot001"

이제 전체 스크립트를 확인했으므로 모든 파일에 현재 날짜/시간이 포함되어 있는 이유는 표시되는 값이 생성 날짜가 아니라 마지막으로 수정된 날짜이기 때문입니다. echo스크립트 끝에 있는 5개의 명령문 세트를 사용하여 파일을 수정 했으므로 모두 현재 날짜/시간을 갖게 됩니다.

관련 정보