Peter Ahn
Peter의 우아한 프로그래밍
Peter Ahn
전체 방문자
오늘
어제
  • 전체 (115)
    • 영어공부 (15)
    • 물생활 (9)
    • 독서 (2)
    • 일상 (1)
    • IT 소식 (15)
      • 최신 기술 소식 (10)
      • 보안 소식 (5)
    • 인공지능 (AI) (2)
    • OS (19)
      • Linux (19)
    • Web (0)
      • Vue.js (0)
    • 객체지향 (1)
      • 개념과 원리 (1)
      • 디자인패턴 (0)
    • 프로그래밍 (37)
      • C (14)
      • C++ (3)
      • C# (1)
      • Python (3)
      • Javascript (2)
      • Bash (4)
      • Vim (9)
    • Database (3)
      • DB2 (3)
      • MongoDB (0)
    • 프레임워크 (1)
      • Ionic (1)
    • Cloud (1)
      • AWS (1)
    • 3D Printer (2)
    • 기록보관소 (5)
    • 초대장 (2)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

  • 프로그래밍의 세계에 오신 것을 환영합니다.

인기 글

태그

  • 물생활
  • 하프블랙구피
  • call by reference
  • 리눅스
  • 영어 회화
  • 딥러닝
  • 리눅스 명령어
  • bash
  • c강좌
  • C언어
  • 구피
  • C++
  • AI
  • C언어 강좌
  • 4차 산업혁명
  • bash shell
  • vim 비주얼모드
  • 어항
  • Linux
  • typescript
  • 영어표현
  • 인공지능
  • vim
  • 구피어항
  • 랜섬웨어
  • 영어 표현
  • 생활영어
  • vim 강좌
  • 프로그래밍
  • 쉘 프로그래밍

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
Peter Ahn

Peter의 우아한 프로그래밍

[Linux] touch 파일 생성하기
OS/Linux

[Linux] touch 파일 생성하기

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의 우아한 프로그래밍

반응형
저작자표시 비영리 동일조건 (새창열림)

'OS > Linux' 카테고리의 다른 글

[Linux] 파이프(pipe)에 대한 이해  (7) 2018.05.15
[Linux] gcc, g++ 설치하기 (CentOS)  (1) 2018.05.10
[Linux] mv 파일 및 디렉토리 이동  (0) 2018.05.09
[Linux] find 로 파일 찾아서 지우기  (5) 2018.05.08
[Linux] find 로 파일 검색하기  (0) 2018.05.05
    'OS/Linux' 카테고리의 다른 글
    • [Linux] 파이프(pipe)에 대한 이해
    • [Linux] gcc, g++ 설치하기 (CentOS)
    • [Linux] mv 파일 및 디렉토리 이동
    • [Linux] find 로 파일 찾아서 지우기
    Peter Ahn
    Peter Ahn
    IT 정보 공유, 프로그래밍 지식 공유

    티스토리툴바