IT 전용글/Linux

Linux, 파일 날짜정보 변경하는 touch

회상형인간 2021. 12. 16. 15:06

touch명령어는 파일의 날짜시간정보(timestamps)를 변경하는 명령어이다. 아무런 옵션없이 사용하면 서버의 현재시간으로 파일의 최근사용한 시간(atime)과 최근변경시간(mtime)을 변경한다. 변경하려하는 파일이 없는경우에는 해당파일을 생성하며, -t옵션을 사용하면 사용자가 원하는 시간으로 파일의 timestamps를 변경한다.

 

우선 touch명령어가 다루는 파일의 timestamps부터 살펴보겠다.

timestamps는 총 3가지가 있다.

ctime = 파일의 속성변경시간(chown, chmod등으로)

atime = 최근에 파일이 최근 읽혀진 시간, 최근 사용시간이라고 함(access time)

mtime = 최근에 파일이 변경된 시간(modification time)

ctime ls-lc명령어를 사용시 나타나는 시간

atime  ls -lu명령어를 사용시 나타나는 시간

mtime ls -l명령어를 사용시 나타나는 시간

 

debugfs명령어를 이용하면 특정파일이나 디렉토리에 대한 모든정보를 확인할수 있다고 한다.

이 명령어는 다음에 공부해야하겠다

 

-. 크기가 0인 파일 생성하기(옵션없이 사용시)

touch newfile

 

-. 파일시간정보를 현재시간으로 변경하기(-c옵션)

touch file1

touch -c file1

둘중 아무거나 써도 file1 timestamps는 변경되지만, 차이점은

-c옵션인자를 썻을경우 file1이 존재하지 않으면 그냥 무시되지만

-c옵션인자를 쓰지 않으면 file1존재 하지 않으면 file1이라는 크기0짜리의 파일을 생성한다.

 

-. 사용자가 원하는 시간으로 timestamps변경하기(-t옵션)

형식 : touch -t [[CC]YY]MMDDhhmm 대상파일

ex)

[root@os1 /work/touch]# ls -l testfile1 #시간변경전

-rw-r--r-- 1 root root 0 2013-01-04 02:18 testfile1

 

위 파일을 각각 다른인자를 줘서 변경하였을경우

[root@os1 /work/touch]# touch -t 198702180405 testfile1 #YYYYMMDDhhmm

[root@os1 /work/touch]# ls -l testfile1

-rw-r--r-- 1 root root 0 1987-02-18 04:05 testfile1

 

[root@os1 /work/touch]# touch -t 8702180405 testfile1 #YYMMDDhhmm

[root@os1 /work/touch]# ls -l testfile1

-rw-r--r-- 1 root root 0 1987-02-18 04:05 testfile1

 

[root@os1 /work/touch]# touch -t 02180405 testfile1 #MMDDhhmm

[root@os1 /work/touch]# ls -l testfile1

-rw-r--r-- 1 root root 0 2013-02-18 04:05 testfile1

 

-. 다른파일 timestamps 동일하게 맞추기(-r옵션)

형식 : touch -r [시간을 가져올파일] [시간을 수정할 파일]

-rw-r--r-- 1 root root 0 2013-02-18 04:05 testfile1

-rw-r--r-- 1 root root 0 2002-06-18 20:22 testfile2

testfile2 timestamps정보를 가져와서 testfile1 timestamps정보를 수정해보도록 하겠다.

[root@os1 /work/edu-touch]# touch -r testfile2 testfile1

[root@os1 /work/edu-touch]# ls -l

-rw-r--r-- 1 root root 0 2002-06-18 20:22 testfile1

-rw-r--r-- 1 root root 0 2002-06-18 20:22 testfile2

 

 

 

 timestamps 포맷 변경하는방법

옵션없을때의 시간출력형태

timestamps의 날짜가 date의 연도가 같을경우 형식 = [MM] [DD] [hh:mm]

timestamps의 날짜가 data의 연도가 다를경우 형식 = [MM] [DD] [YYYY]

[root@os2 ~]# ls -l anaconda-ks.cfg

-rw------- 1 root root 970 Dec 31 08:21 anaconda-ks.cfg #연도가 같을때

-rw------- 1 root root 970 Feb 2 1987 anaconda-ks.cfg #연도가 다를때

 

--time-style=long-iso옵션을 줬을때 시간출력 형태

YYYY-MM-DD hh:mm

[root@os1 ~]# ls -l --time-style=long-iso anaconda-ks.cfg

-rw------- 1 root root 970 2012-12-31 02:33 anaconda-ks.cfg

 

 

참조 : 리눅스 서버관리 실무 바이블 3.0(박성수 저)

'IT 전용글 > Linux' 카테고리의 다른 글

Linux, 파일 날짜정보 변경하는 touch  (0) 2021.11.26
리눅스 서버 시간 확인 및 동기화 명령어~  (0) 2021.01.26
Process 관리  (0) 2008.11.27
파일권한(Permission)  (0) 2008.11.27
사용자 계정 관리  (0) 2008.11.27