서버/Linux I 2015. 12. 23. 15:20

Linux I - 04. 리눅스 기본 명령어 (파일 관리 명령어)

 

 

 

[root@CentOS ~]# echo $PS1
[\u@\h \W]\$


[root@CentOS ~]# PS1="[\u@\h \$PWD]# "
[root@CentOS /root]# cd ..
[root@CentOS /]#

 

 

 

 

1. 'touch' 명령어


 

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test
[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l
합계 0
-rw-r--r--. 1 root root 0 2015-12-23 10:22 file1

 

[root@CentOS /test]# cp -p /etc/passwd file2
[root@CentOS /test]# ls -l
합계 4
-rw-r--r--. 1 root root    0 2015-12-23 10:22 file1
-rw-r--r--. 1 root root 1816 2015-12-21 15:10 file2

 

[root@CentOS /test]# touch file2
[root@CentOS /test]# ls -l
합계 4
-rw-r--r--. 1 root root    0 2015-12-23 10:22 file1
-rw-r--r--. 1 root root 1816 2015-12-23 10:23 file2

 

[root@CentOS /test]# date
2015. 12. 23. (수) 10:23:58 KST

 

[root@CentOS /test]# touch -t 12151400 file2
[root@CentOS /test]# ls -l file2
-rw-r--r--. 1 root root 1816 2015-12-15 14:00 file2

 


2. 'cp' 명령어

 

[root@CentOS /root]# cp -a        원본 파일 속성, 링크 정보를 유지하여 복사 해줌
[root@CentOS /root]# cp -i         복사할 파일이 존재할 경우, 복사할 것인지 물어봄
[root@CentOS /root]# cp -r         하위 디렉토리 내에 있는 모든 파일까지 복사 해줌
[root@CentOS /root]# cp -p        원본 파일의 소유, 그룹, 권한, 허용 시간을 유지하여 복사 해줌

 

 

Ex1) 복사 파일 inode 확인

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

 

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -li
합계 0
131377 -rw-r--r--. 1 root root 0 2015-12-23 10:28 file1

 

[root@CentOS /test]# cp file1 file2
[root@CentOS /test]# ls -li
합계 0
131377 -rw-r--r--. 1 root root 0 2015-12-23 10:28 file1
131378 -rw-r--r--. 1 root root 0 2015-12-23 10:28 file2

 


Ex2) 'cp -r' 옵션 명령어 (일반 파일이면 복사되며, 디렉토리이면 하위 디렉토리까지 복사됨)

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# touch dir1/file1 dir1/file2
[root@CentOS /test]#
[root@CentOS /test]# tree
.
└── dir1
    ├── file1
    └── file2

1 directory, 2 files

[root@CentOS /test]# cp -r dir1 dir2
[root@CentOS /test]# ls -lR
.:
합계 8
drwxr-xr-x. 2 root root 4096 2015-12-23 10:31 dir1
drwxr-xr-x. 2 root root 4096 2015-12-23 10:31 dir2

./dir1:
합계 0
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file1
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file2

./dir2:
합계 0
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file1
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file2

[root@CentOS /test]# tree
.
├── dir1
│   ├── file1
│   └── file2
└── dir2
    ├── file1
    └── file2

2 directories, 4 files

[root@CentOS /test]# cp -r dir1 dir2
[root@CentOS /test]# ls -lR
.:
합계 8
drwxr-xr-x. 2 root root 4096 2015-12-23 10:31 dir1
drwxr-xr-x. 3 root root 4096 2015-12-23 10:32 dir2

./dir1:
합계 0
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file1
-rw-r--r--. 1 root root 0 2015-12-23 10:31 file2

./dir2:
합계 4
drwxr-xr-x. 2 root root 4096 2015-12-23 10:32 dir1
-rw-r--r--. 1 root root    0 2015-12-23 10:31 file1
-rw-r--r--. 1 root root    0 2015-12-23 10:31 file2

./dir2/dir1:
합계 0
-rw-r--r--. 1 root root 0 2015-12-23 10:32 file1
-rw-r--r--. 1 root root 0 2015-12-23 10:32 file2

[root@CentOS /test]# tree
.
├── dir1
│   ├── file1
│   └── file2
└── dir2
    ├── dir1
    │   ├── file1
    │   └── file2
    ├── file1
    └── file2
3 directories, 6 files

 


Ex3) 파일 덮어 쓰기 방법

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

 

[root@CentOS /test]# mkdir dir1

[root@CentOS /test]# echo "linux200" > file1
[root@CentOS /test]# cat file1
linux200

 

[root@CentOS /test]# touch dir1/file1
[root@CentOS /test]# cat dir1/file1

[root@CentOS /test]# cp file1 dir1
cp: overwrite `dir1/file1'? y

 

[root@CentOS /test]# ls -lR
.:
합계 8
drwxr-xr-x. 2 root root 4096 2015-12-23 10:37 dir1
-rw-r--r--. 1 root root    9 2015-12-23 10:37 file1

./dir1:
합계 4
-rw-r--r--. 1 root root 9 2015-12-23 10:37 file1

 

[root@CentOS /test]# cat dir1/file1
linux200

 


Ex4) 'cp -p' 옵션 명령어

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

 

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -l file1
-rw-r--r--. 1 root root 0 2015-12-23 10:39 file1

[root@CentOS /test]# chmod 777 file1  (파일 퍼미션 변경)
[root@CentOS /test]# ls -l
합계 0
-rwxrwxrwx. 1 root root 0 2015-12-23 10:39 file1

 

[root@CentOS /test]# cp file1 file2
[root@CentOS /test]# ls -l
합계 0
-rwxrwxrwx. 1 root root 0 2015-12-23 10:39 file1
-rwxr-xr-x. 1 root root 0 2015-12-23 10:40 file2

 

[root@CentOS /test]# cp -p file1 file3
[root@CentOS /test]# ls -l
합계 0
-rwxrwxrwx. 1 root root 0 2015-12-23 10:39 file1
-rwxr-xr-x. 1 root root 0 2015-12-23 10:40 file2
-rwxrwxrwx. 1 root root 0 2015-12-23 10:39 file3

 

[root@CentOS /test]# chmod 777 /test  (다른 사용자이 접근하여 파일을 생성할 수 있도록 퍼미션 변경)

[root@CentOS /test]# su - user1


[user1@CentOS ~]$ id
uid=500(user1) gid=500(user1) groups=500(user1) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

 

[user1@CentOS ~]$ pwd
/home/user1


[user1@CentOS ~]$ cd /test
[user1@CentOS test]$ cp file1 file4
[user1@CentOS test]$ ls -l
합계 0
-rwxrwxrwx. 1 root  root  0 2015-12-23 10:39 file1
-rwxr-xr-x. 1 root  root  0 2015-12-23 10:40 file2
-rwxrwxrwx. 1 root  root  0 2015-12-23 10:39 file3
-rwxrwxr-x. 1 user1 user1 0 2015-12-23 10:41 file4

 

[user1@CentOS test]$ cp -p file1 file5
[user1@CentOS test]$ ls -l
합계 0
-rwxrwxrwx. 1 root  root  0 2015-12-23 10:39 file1
-rwxr-xr-x. 1 root  root  0 2015-12-23 10:40 file2
-rwxrwxrwx. 1 root  root  0 2015-12-23 10:39 file3
-rwxrwxr-x. 1 user1 user1 0 2015-12-23 10:41 file4
-rwxrwxrwx. 1 user1 user1 0 2015-12-23 10:39 file5
[user1@CentOS test]$ exit
logout
[root@CentOS /test]#

 


3. 'mv' 명령어

 

Ex1) 'mv' 명령어

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

 

[root@CentOS /test]# mkdir dir1

[root@CentOS /test]# touch file1
[root@CentOS /test]# touch dir1/file2

[root@CentOS /test]# ls
dir1  file1

[root@CentOS /test]# tree
.
├── dir1
│   └── file2
└── file1

1 directory, 2 files

[root@CentOS /test]# mv file1 file3
[root@CentOS /test]# ls
dir1  file3

[root@CentOS /test]# tree
.
├── dir1
│   └── file1
└── file3

1 directory, 2 files

[root@CentOS /test]# mv file3 dir1
[root@CentOS /test]# ls dir1
file1  file3

[root@CentOS /test]# tree
.
└── dir1
    ├── file2
    └── file3

1 directory, 2 files

 

[root@CentOS /test]# mv dir1 dir2

[root@CentOS /test]# ls
dir2


[root@CentOS /test]# ls dir2
file1  file3

[root@CentOS /test]# tree
.
└── dir2
    ├── file2
    └── file3

1 directory, 2 files

 

[root@CentOS /test]# cp -r dir2 dir1
[root@CentOS /test]# ls
dir1  dir2

[root@CentOS /test]# ls dir1
file1  file3

[root@CentOS /test]# tree
.
├── dir1
│   ├── file1
│   └── file3
└── dir2
    ├── file1
    └── file3

2 directories, 4 files

[root@CentOS /test]# mv dir1 dir2
[root@CentOS /test]# ls
dir2

[root@CentOS /test]# ls dir2
dir1  file1  file3

[root@CentOS /test]# tree
.
└── dir2
    ├── dir1
    │   ├── file1
    │   └── file3
    ├── file1
    └── file3

2 directories, 4 files

 


Ex2) 'inode' 확인

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*

 

[root@CentOS /test]# touch file1
[root@CentOS /test]# ls -li
합계 0
131377 -rw-r--r--. 1 root root 0 2015-12-23 11:09 file1

 

[root@CentOS /test]# mv file1 file3
[root@CentOS /test]# ls -li
합계 0
131377 -rw-r--r--. 1 root root 0 2015-12-23 11:09 file3

 


Ex3) 다수 파일 이동

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# touch file1 file2 file3 test1 test2 test3
[root@CentOS /test]# ls
file1  file2  file3  test1  test2  test3

 

[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# mv file* test* dir1
[root@CentOS /test]# ls dir1
file1  file2  file3  test1  test2  test3

 


4. 'rm' 명령어

 

[root@CentOS /root]# rm -f        강제로 파일 삭제해줌, 만약 삭제할 파일이 없으면 아무런 출력 내용이 안나옴
[root@CentOS /root]# rm -i        파일을 삭제할 것인지 물어봄
[root@CentOS /root]# rm -r        일반 파일이면 삭제하고, 디렉토리이면 하위 디렉토리 및 파일을 다 삭제 해줌
[root@CentOS /root]# rm -rf       r 옵션과 f 옵션이 같이 실행됨

 

 

Ex1) 'rm -r' 옵션 명령어


[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]# ls

 

[root@CentOS /test]# mkdir dir1
[root@CentOS /test]# touch dir1/file1 dir1/file2
[root@CentOS /test]# ls dir1
file1  file2

[root@CentOS /test]# tree
.
└── dir1
    ├── file1
    └── file2

1 directory, 2 files

 

[root@CentOS /test]# rm -r dir1
rm: descend into directory `dir1'? y
rm: remove 일반 빈 파일 `dir1/file1'? y
rm: remove 일반 빈 파일 `dir1/file2'? y
rm: remove 디렉토리 `dir1'? y
[root@CentOS /test]#
[root@CentOS /test]# ls

 

 

Ex2) 디렉토리 안에 있는 모든 디렉토리 및 파일 삭제

 

[root@CentOS /test]# cd /test
[root@CentOS /test]# rm -rf /test/*
[root@CentOS /test]#
[root@CentOS /test]# ls

 

 

 

[root@CentOS /test]# cd ..
[root@CentOS /]# rm -rf test

Posted by 김정우 강사(카카오톡 : kim10322)
,


Q