OS/Linux

[Linux] touch 파일 생성하기

Peter Ahn 2018. 5. 9. 15:14
반응형

개요

touch 명령어는 리눅스에서 파일을 생성하거나 갱신하는 명령어입니다.

새로운 파일을 만들 때는 존재하지 않는 파일명을 지정하면 지정된 파일명으로 파일이 생성되며 이미 존재하는 파일을 지정하면 파일의 수정시간이 업데이트 됩니다.


사용법

리눅스에서 사용되는 명령어는 모두 대소문자를 구분하니 주의해주세요.

touch 명령어에 대한 자세한 설명은 touch --help 명령어를 통해 볼 수 있습니다.


Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.

A FILE argument that does not exist is created empty, unless -c or -h
is supplied.

A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.

Mandatory arguments to long options are mandatory for short options too.
  -a                     change only the access time
  -c, --no-create        do not create any files
  -d, --date=STRING      parse STRING and use it instead of current time
  -f                     (ignored)
  -h, --no-dereference   affect each symbolic link instead of any referenced
                         file (useful only on systems that can change the
                         timestamps of a symlink)
  -m                     change only the modification time
  -r, --reference=FILE   use this file's times instead of current time
  -t STAMP               use [[CC]YY]MMDDhhmm[.ss] instead of current time
      --time=WORD        change the specified time:
                           WORD is access, atime, or use: equivalent to -a
                           WORD is modify or mtime: equivalent to -m
      --help     display this help and exit
      --version  output version information and exit

Note that the -d and -t options accept different time-date formats.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'touch invocation'

기본 사용법은 아래와 같습니다.

touch [OPTION] filename


touch 로 기존에 존재하지 않는 파일명을 지정하면 크기가 0 byte인 파일을 생성합니다.

[root@peterdev test]# touch new_file
[root@peterdev test]# ls -l | grep new_file
-rw-r--r-- 1 root root    0 May  9 06:06 new_file
[root@peterdev test]#



이미 존재하는 파일을 touch 하게 되면 수정시간만 현재 시간으로 변경됩니다.

drwxr-xr-x 2 root root 4096 May  3 14:27 john
-rw-r--r-- 1 root root    0 May  8 14:40 mv_test.dat
drwxr-xr-x 2 root root 4096 May  3 14:27 peter
-rw-r--r-- 1 root root   33 May  3 14:48 test1.txt
-rw-r--r-- 1 root root   33 May  8 14:56 test2.txt
[root@peterdev test]#
[root@peterdev test]#
[root@peterdev test]#
[root@peterdev test]# touch test1.txt
[root@peterdev test]# ls -l
total 16
drwxr-xr-x 2 root root 4096 May  3 14:27 john
-rw-r--r-- 1 root root    0 May  8 14:40 mv_test.dat
drwxr-xr-x 2 root root 4096 May  3 14:27 peter
-rw-r--r-- 1 root root   33 May  9 05:59 test1.txt
-rw-r--r-- 1 root root   33 May  8 14:56 test2.txt

예제

ex) teacher.txt 라는 파일을 생성

touch teacher.txt



ex) 파일의 수정 시간을 지정한 timestamp 로 변경

[root@peterdev peter]# touch -t 201801010000 language.c
[root@peterdev peter]# ls -l
total 0
-rw-r--r-- 1 root root 0 May 3 14:27 language.bash
-rw-r--r-- 1 root root 0 Jan 1 00:00 language.c
-rw-r--r-- 1 root root 0 May 3 14:26 language.cpp
-rw-r--r-- 1 root root 0 May 3 14:27 language.delphi
-rw-r--r-- 1 root root 0 May 3 14:26 language.english
-rw-r--r-- 1 root root 0 May 3 14:27 language.java
-rw-r--r-- 1 root root 0 May 3 14:26 language.javascript
-rw-r--r-- 1 root root 0 May 3 14:27 language.korean
-rw-r--r-- 1 root root 0 May 3 14:26 language.python


번외

touch 명령어 외에도 아래와 같이 파일을 생성할 수 있습니다.


아래는 I/O 재지향으로 파일을 생성하고 내용 입력하는 방법입니다.

[root@peterdev test]# echo "test" > new.txt
[root@peterdev test]# ls -l | grep new.txt
-rw-r--r-- 1 root root    5 May  9 06:09 new.txt
[root@peterdev test]# cat new.txt
test
[root@peterdev test]#

만약에 이미 존재하는 파일에 내용을 추가(append)하고 싶을 때는

재지향 연산자 > 대신에 >>를 사용하면 됩니다.


 

 

-Peter의 우아한 프로그래밍

반응형