리눅스 파일 관련 명령어1 (ls, cp, rm, mv, touch)

주영·2023년 10월 25일
0

Linux Master

목록 보기
10/11
post-thumbnail

파일 관련 명령어1 (ls, cp, rm, mv, touch)

1. ls

  • list
  • Usage: ls [옵션][경로명]
  • 현재 디렉터리의 파일 목록 나열
  • -cftuvSUX 또는 --sort가 지정되지 않은 경우 항목을 알파벳순으로 정렬
  • = dir
  • 옵션
    • -a: . 으로 시작하는 히든파일을 포함한 모든 파일과 디렉터리 표시
    • -d: 지정된 디렉터리의 정보 출력
    • -l: 퍼미션, 크기, 사이즈 등의 자세한 정보를 긴 목록 형식으로 표시
    • -h: 크기 출력. -l 또는 -s와 함께 사용
    • -r: 알파벳 역순으로 정보 출력
    • -R: 하위 경로와 그 안에 있는 파일 표시
    • -t: 수정 시간순으로 정렬, 최신순
    • -X: 확장자 기준 알파벳 정렬
# ls --help
[root@worker ~]# ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

  -a, --all                  do not ignore entries starting with .
  -d, --directory            list directories themselves, not their contents
  -h, --human-readable       with -l and -s, print sizes like 1K 234M 2G etc.
  -l                         use a long listing format
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -t                         sort by modification time, newest first
  -X                         sort alphabetically by entry extension
  • 예시

    [root@worker ~]# ls
    anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  juyoung  Music  Pictures  Public  Templates  Videos
     [root@worker ~]# ls -a
    .                .ansible       .bash_profile  .config  Desktop    .esd_auth             juyoung  Pictures  .ssh       Videos
    ..               .bash_history  .bashrc        .cshrc   Documents  .ICEauthority         .local   .pki      .tcshrc    .viminfo
    anaconda-ks.cfg  .bash_logout   .cache         .dbus    Downloads  initial-setup-ks.cfg  Music    Public    Templates  .Xauthority
    [root@worker ~]# ls -l
    total 44
    -rw-------. 1 root root 1985 Sep  5 13:56 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Desktop
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Documents
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Downloads
    -rw-r--r--. 1 root root 2366 Sep  5 14:00 initial-setup-ks.cfg
    drwxr-xr-x. 2 root root 4096 Sep 13 22:02 juyoung
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Music
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Pictures
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Public
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Templates
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Videos
    [root@worker ~]# ls -lh
    total 44K
    -rw-------. 1 root root 2.0K Sep  5 13:56 anaconda-ks.cfg
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Desktop
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Documents
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Downloads
    -rw-r--r--. 1 root root 2.4K Sep  5 14:00 initial-setup-ks.cfg
    drwxr-xr-x. 2 root root 4.0K Sep 13 22:02 juyoung
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Music
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Pictures
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Public
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Templates
    drwxr-xr-x. 2 root root 4.0K Sep  5 14:01 Videos
    
    [root@worker ~]# ls -r
    Videos  Templates  Public  Pictures  Music  juyoung  initial-setup-ks.cfg  Downloads  Documents  Desktop  anaconda-ks.cfg
    [root@worker kubernetes]# ls -R
    .:
    kubeadm-client.conf  kubelet.conf  kubelet-config.yaml  kubelet.env  manifests  pki  ssl
    ./manifests:
    ./ssl:
    ca.crt
    [root@worker ~]# ls -lt
    total 44
    drwxr-xr-x. 2 root root 4096 Sep 13 22:02 juyoung
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Desktop
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Documents
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Downloads
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Music
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Pictures
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Public
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Templates
    drwxr-xr-x. 2 root root 4096 Sep  5 14:01 Videos
    -rw-r--r--. 1 root root 2366 Sep  5 14:00 initial-setup-ks.cfg
    -rw-------. 1 root root 1985 Sep  5 13:56 anaconda-ks.cfg
    [root@worker kubernetes]# ls -lX
    total 24
    drwxr-xr-x. 2 kube root 4096 Sep 19 15:10 manifests
    lrwxrwxrwx. 1 root root   19 Sep 19 15:10 pki -> /etc/kubernetes/ssl
    drwxr-xr-x. 2 kube root 4096 Sep 19 15:11 ssl
    -rw-r--r--. 1 root root  435 Sep 19 15:11 kubeadm-client.conf
    -rw-------. 1 root root 1955 Sep 19 15:11 kubelet.conf
    -rw-r--r--. 1 root root  567 Sep 19 15:11 kubelet.env
    -rw-r--r--. 1 root root  730 Sep 19 15:11 kubelet-config.yaml

2. cp

  • copy
  • Usage: cp [옵션] SOURCE DEST
  • 파일 또는 디렉터리를 복사
  • 옵션
    • -a: 파일의 속성, 링크정보까지 복사
    • -b: 복사 대상 파일이 이미 존재할 경우를 대비하여 백업 파일을 만듦
    • -f: 복사 대상이 이미 존재하면 복사 대상 파일을 강제로 지우고 복사
    • -i: 복사 대상이 이미 존재하면 사용자에게 덮어씌울 것인지 유무 확인
    • -r: 디렉터리를 복사할 경우 하위 디렉터리와 파일을 모두 복사
    • -p: 원본 파일의 소유자, 그룹, 권한 등의 정보까지 모두 복사
    • -v: 복사 진행 상태 출력
# cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

  -a, --archive                same as -dR --preserve=all
      --attributes-only        don't copy the file data, just the attributes
      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
      --copy-contents          copy contents of special files when recursive
  -f, --force                  if an existing destination file cannot be
                                 opened, remove it and try again (this option
                                 is ignored when the -n option is also used)
  -i, --interactive            prompt before overwrite (overrides a previous -n
  -p                           same as --preserve=mode,ownership,timestamps
      --preserve[=ATTR_LIST]   preserve the specified attributes (default:
                                 mode,ownership,timestamps), if possible
                                 additional attributes: context, links, xattr,
                                 all
  -v, --verbose                explain what is being done
  • 예시
    1) 파일 복사

    // 현재 디렉토리에 있는 test1.txt 파일을 test2.txt라는 이름으로 현 위치에 복사
    $ cp test1.txt test2.txt
    
    // /home 경로의 test1.txt 파일을 test2.txt라는 이름으로 현재 위치에 복사
    $ cp /home/test.txt test2.txt
    
    // /home 경로의 test1.txt 파일을 test2.txt라는 이름으로 /root에 복사
    $ cp /home/test1.txt /root/test2.txt

    2) 디렉터리 복사

    // 현재 위치에 있는 test1 디렉토리를 test2 디렉토리라는 이름으로 복사
    $ cp -r test1 test2

    3) 여러 파일을 한 번에 복사

    // 현재 디렉터리에 있는 여러 파일을 /home에 복사
    // $ cp [복사 대상1] [복사 대상2] [복사 대상3] ..... [복사 될 경로]
    $ cp test1.txt test2.txt test3.txt /home
    
    // 현재 위치에 있는 모든 파일을 /home에 복사
    // $ cp * [복사 될 경로]
    $ cp * /home

    4) -v 옵션

    [root@worker juyoung]# cp -v test1.txt test2.txt
    'test1.txt' -> 'test2.txt'

    5) -i 옵션

    [root@worker juyoung]# cp -i test1.txt test2.txt
    cp: overwrite 'test2.txt'? 

3. rm

  • remove
  • Usage: rm [옵션] 파일명/디렉터리명
  • 파일 또는 디렉터리를 삭제
  • 옵션
    • -f: 질의 메시지 없이 강제로 파일이나 디렉터리 삭제
    • -i: 삭제 시 사용자에게 질의
    • -r: 삭제 디렉터리가 포함하고 있는 하위 디렉터리를 포함하여 모든 파일 삭제
    • -d: 빈 디렉터리일 경우에만 삭제
    • -v: 삭제 진행 상태 출력
# rm --help
Usage: rm [OPTION]... [FILE]...
Remove (unlink) the FILE(s).

  -f, --force           ignore nonexistent files and arguments, never prompt
  -i                    prompt before every removal
  -r, -R, --recursive   remove directories and their contents recursively
  -d, --dir             remove empty directories
  -v, --verbose         explain what is being done
  • 예시
    1) -f 옵션 (파일 강제 삭제)

    [root@worker juyoung]# rm -f test1.txt

    2) -r 옵션 (디렉터리 삭제)

    [root@worker juyoung]# rm -r test

    3) -i 옵션

    // 파일
    [root@worker juyoung]# rm -i test1.txt
    rm: remove regular file 'test1.txt'?
    
    // 디렉터리
    [root@worker juyoung]# rm -ir test
    rm: descend into directory 'test'?

    4) -d 옵션

    // 디렉터리가 비어있는 경우
    [root@worker juyoung]# rm -d empty/
    rm: remove directory 'empty/'? y
    // 디렉터리가 비어있지 않은 경우
    [root@worker juyoung]# rm -d test
    rm: cannot remove 'test': Directory not empty

    4) -v 옵션

    [root@worker juyoung]# rm -rfv test
    removed 'test/test.txt'
    removed directory 'test'

4. mv

  • move
  • Usage: mv [옵션] SOURCE DEST
  • 파일 또는 디렉터리를 이동하거나 파일명 변경
  • 옵션
    • -b: 대상 파일이 이미 지워지는 것을 대비해 백업 파일 생성
    • -f: 대상 파일이 이미 있어도 사용자에게 어떻게 처리할지 묻지 않음
    • -v: 파일 이동 진행 상태 출력
# mv --help
Usage: mv [OPTION]... [-T] SOURCE DEST
  or:  mv [OPTION]... SOURCE... DIRECTORY
  or:  mv [OPTION]... -t DIRECTORY SOURCE...
Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

      --backup[=CONTROL]       make a backup of each existing destination file
  -b                           like --backup but does not accept an argument
  -f, --force                  do not prompt before overwriting
  -i, --interactive            prompt before overwrite
  -v, --verbose                explain what is being done
  • 예시
    1) 파일 이동
    // 현재 위치에 있는 test.txt 파일을 /home에 이동
    $  mv test.txt /home
    // 현재 위치에 있는 test.txt 파일을 /home에 text2.txt라는 이름으로 이동
    $  mv test.txt /home/test2.txt
    // 특정 위치에 있는 test.txt 파일을 다른 위치에 이동
    $ mv /home/test.txt /root
    2) 디렉터리 이동
    // 현재 위치에 있는 a 디렉터리를 b 디렉터리 내부로 이동
    $ mv a b
    // 특정 위치에 있는 a 디렉터리를 다른 위치에 이동
    $ mv /home/a /root
    3) 파일명 변경
    // 파일명 변경
    $ mv test.txt test2.txt
    // 현재 위치에 있는 test.txt 파일을 /home에 text2.txt라는 이름으로 이동
    $  mv test.txt /home/test2.txt
    4) -v 옵션
    [root@worker juyoung]# mv -v b a
    renamed 'b' -> 'a/b'

5. touch

  • Usage: touch [옵션] 파일명
  • 파일 크기가 0바이트인 빈 파일을 생성
  • 서버의 현재 시간으로 파일의 최근 사용한 시간과 최근 수정 시간 등 타임스탬프를 변경
    • 타임스탬프: 파일의 시간 정보를 나타내는 것. 접근시간/수정시간/변경시간이 있다.
    • 접근시간(access time): 애플리케이션이나 서비스가 시스템 호출을 사용해 파일을 읽을 때마다 갱신되는 시간
    • 수정시간(modify time): 파일 내용이 변경될 때 갱신되는 시간
    • 변경시간(change time): 파일 내용을 수정했을 때 기록되는 시간. 변경 불가능한 시간
  • 옵션
    • -a: 접근시간(access time) 변경
    • -m: 수정시간(modify time) 변경
    • -c: 시스템 현재 시간으로 파일 시간 정보 수정
    • -t 시간정보: 특정 파일의 시간 정보 변경
    • -r 파일1 파일2: 파일1의 시간 정보를 파일2에 동일하게 적용
# 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.

  -a                     change only the access time
  -c, --no-create        do not create any files
  -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
  • 예시

    [root@worker juyoung]# touch a.txt
    [root@worker juyoung]# ll
    total 0
    -rw-r--r--. 1 root root 0 Oct 25 22:58 a.txt
    
    // -m 옵션
    [root@worker juyoung]# touch -m a.txt
    [root@worker juyoung]# ll
    total 0
    -rw-r--r--. 1 root root 0 Oct 25 22:59 a.txt
    
    // -c 옵션
    [root@worker juyoung]# touch -c a.txt
    [root@worker juyoung]# ll
    total 0
    -rw-r--r--. 1 root root 0 Oct 25 23:00 a.txt
    
    // -r 옵션
    [root@worker juyoung]# ll
    total 0
    -rw-r--r--. 1 root root 0 Oct 25 23:00 a.txt
    -rw-r--r--. 1 root root 0 Oct 25 23:01 b.txt
    [root@worker juyoung]# touch -r a.txt b.txt
    [root@worker juyoung]# ll
    total 0
    -rw-r--r--. 1 root root 0 Oct 25 23:00 a.txt
    -rw-r--r--. 1 root root 0 Oct 25 23:00 b.txt

참고: 2021 이기적 리눅스마스터 2급

0개의 댓글

관련 채용 정보