답변:
파일 (또는 루트)의 소유자 인 경우 다음 touch명령을 사용하여 파일의 수정 시간을 변경할 수 있습니다 .
touch filename
기본적으로 파일 수정 시간을 현재 시간으로 설정하지만 -d특정 날짜를 선택하는 플래그 와 같은 여러 플래그가 있습니다. 예를 들어, 현재 2 시간 전에 파일이 수정되도록 설정하려면 다음을 사용할 수 있습니다.
touch -d "2 hours ago" filename
기존 수정 시간을 기준으로 파일을 수정하려면 다음을 수행해야합니다.
touch -d "$(date -R -r filename) - 2 hours" filename
많은 수의 파일을 수정하려는 경우 다음을 사용할 수 있습니다.
find DIRECTORY -print | while read filename; do
# do whatever you want with the file
touch -d "$(date -R -r "$filename") - 2 hours" "$filename"
done
원하는 find파일 만 선택 하도록 인수를 변경할 수 있습니다 . 현재 시간을 기준으로 파일 수정 시간 만 업데이트하려는 경우 다음과 같이 단순화 할 수 있습니다.
find DIRECTORY -exec touch -d "2 hours ago" {} +
이 형식은 셸을 사용하여에 인수를 구성하기 때문에 파일 시간 상대 버전에서는 사용할 수 없습니다 touch.
작성 시간이 지날 때까지 대부분의 Linux 파일 시스템은이 값을 추적하지 않습니다. 거기에있다 ctime파일과 관련된하지만, 파일 메타 데이터가 마지막으로 변경된 경우는 추적합니다. 파일의 사용 권한이 변경되지 않은 경우 생성 시간이 유지 될 수 있지만 이는 우연의 일치입니다. 파일 수정 시간을 명시 적으로 변경하면 메타 데이터 변경으로 간주되므로을 업데이트하면 부작용이 발생합니다 ctime.
ctime표준 방식으로는 변경 이 불가능하다고 덧붙였습니다.
ctime메타 데이터 변경 시간 에 대한 정보 는 POSIX에서 가져온 것입니다. 내 대답의 셸 조각이 엄격한 POSIX 셸 유틸리티에서 작동하는지 여부를 모르겠습니다. 그러나 그들은 분명히이 사이트의 답변에 대한 컨텍스트 인 Ubuntu에서 작동합니다.
gtouch통해 사용할 수 있습니다 brew install coreutils.
도와 주셔서 감사합니다. 이것은 나를 위해 일했다 :
터미널에서 날짜 편집 디렉토리로 이동하십시오. 그런 다음 다음을 입력하십시오.
find -print | while read filename; do
# do whatever you want with the file
touch -t 201203101513 "$filename"
done
Enter 키를 누르면 ">"가 표시되고 마지막으로 실행됩니다-> "완료".
참고 : "201203101513"을 변경할 수 있습니다.
"201203101513"=이 디렉토리의 모든 파일에 대한 날짜입니다.
.ss경우 ss 는 초 수를 사용하십시오. (참고 : 이것은 나노초 나 밀리 초와 같은 초 단위의 타이밍을 제어하지 않는 것 같습니다. 제게는 0으로 나타납니다. 그러나이 차이는 유스 케이스에 허용됩니다.)
가장 쉬운 방법-액세스 및 수정은 동일합니다.
touch -a -m -t 201512180130.09 fileName.ext
어디:
-a = accessed
-m = modified
-t = timestamp - use [[CC]YY]MMDDhhmm[.ss] time format
사용 NOW하려면 -t및 타임 스탬프를 삭제하십시오 .
그것들이 모두 같은지 확인하려면 :
stat fileName.ext
참조 : 터치 맨
touch플래그 중 어느 것도 올바른 결과를 얻지 못했습니다 . 수정 날짜는 조정되었지만 변경 및 액세스 날짜는 실제로 NOW로 변경되었습니다.
changed시간과 modified타임 스탬프 의 차이점은 무엇입니까 ?
Touch는 파일 날짜를 자체적으로 참조 할 수 date있으며 명령 대체 를 호출 하거나 사용할 필요가 없습니다 . 다음은 터치 정보 페이지의 일부입니다.
`-r FILE' `--reference=FILE'
Use the times of the reference FILE instead of the current time.
If this option is combined with the `--date=TIME' (`-d TIME')
option, the reference FILE's time is the origin for any relative
TIMEs given, but is otherwise ignored. For example, `-r foo -d
'-5 seconds'' specifies a time stamp equal to five seconds before
the corresponding time stamp for `foo'. If FILE is a symbolic
link, the reference timestamp is taken from the target of the
symlink, unless `-h' was also in effect.
예를 들어, 파일 날짜에 8 시간을 추가하려면 ( file공백 등의 경우 인용 된 파일 이름 ) :
touch -r "file" -d '+8 hour' "file"
현재 디렉토리의 모든 파일에 루프 사용
for i in *; do touch -r "$i" -d '+8 hour' "$i"; done
나는를 사용하여 들었어요 *및시키는 for자체가 파일 이름 선택 안전을 하지만, 사용 find -print0 | xargs -0 touch ... 처리해야하는 대부분의 파일 이름에서 줄 바꿈, 공백, 따옴표, 백 슬래시와 같은 미친 문자. (PS. 처음에는 파일 이름에 미친 캐릭터를 사용하지 마십시오).
예를 들어 파일 thatdir이름이로 시작하는 모든 파일을 찾고 s해당 파일의 수정 된 타임 스탬프에 하루를 추가하려면 다음을 사용하십시오.
find thatdir -name "s*" -print0 | xargs -0 -I '{}' touch -r '{}' -d '+1 day' '{}'
이 작은 스크립트는 적어도 나를 위해 일합니다.
#!/bin/bash
# find specific files
files=$(find . -type f -name '*.JPG')
# use newline as file separator (handle spaces in filenames)
IFS=$'\n'
for f in ${files}
do
# read file modification date using stat as seconds
# adjust date backwards (1 month) using date and print in correct format
# change file time using touch
touch -t $(date -v -1m -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}"
done
어떤 종류의 유닉스 프로그램을 쓴 지 오래되었지만, 실수로 크리스마스 사진에 잘못 연도를 설정했으며 2015 년에서 2014 년까지 날짜를 변경하지 않으면 나중에 문제가 될 것이라는 것을 알았습니다. .
어쩌면 이것은 쉬운 작업이지만 간단한 방법을 찾지 못했습니다.
여기에서 찾은 스크립트를 수정했습니다. 원래 스크립트를 빼서 한 달 빼기 위해 사용되었습니다.
원본 스크립트는 다음과 같습니다.
#!/bin/bash
# find specific files
files=$(find . -type f -name '*.JPG')
# use newline as file separator (handle spaces in filenames)
IFS=$'\n'
for f in ${files}
do
# read file modification date using stat as seconds
# adjust date backwards (1 month) using date and print in correct format
# change file time using touch
touch -t $(date -v -1m -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}"
done
다음은 날짜를 2014 년으로 강제 수정 한 수정 된 스크립트입니다.
#!/bin/bash
# find specific files
#files=$(find . -type f -name '*.JPG')
# use newline as file separator (handle spaces in filenames)
IFS=$'\n'
for f in $*
do
# read file modification date using stat as seconds
# adjust date backwards (1 month) using date and print in correct format
# change file time using touch
touch -t $(date -v +1y -r $(stat -f %m "${f}") +2014%m%d%H%M.%S) "${f}"
done
이제 더 일반적인 버전을 수행 할 수 있음을 알았습니다.
#!/bin/bash
# find specific files
#files=$(find . -type f -name '*.JPG')
# use newline as file separator (handle spaces in filenames)
IFS=$'\n'
for f in $*
do
# read file modification date using stat as seconds
# adjust date backwards (1 month) using date and print in correct format
# change file time using touch (+1y adds a year "-1y" subtracts a year)
# Below line subtracts a year
touch -t $(date -v -1y -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}"
# Below line adds a year
# touch -t $(date -v +1y -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}"
done
이 파일을 사용하려면 파일을 작성하고
chmod +x fn
실행하다:
./fn files-to-change
fn = 파일 이름, 명령, 명령
예
./fn *.JPG
귀하가있는 디렉토리에서 1 년을 빼고 날짜를 변경합니다.
설정에서 날짜와 시간 만 변경하면됩니다. 파일을 저장하면 자동으로 변경됩니다
touch -d "2 hours ago" /path/*.txt예를 들어, 모든 파일이 같은 폴더에있을 때 더 간단한 경우를 언급하십시오 .