즉, sudo란 Linux, OS X 개발환경에서 특정 명령을 실행하거나 특정 파일에 접근하기 위해 일시적으로 필요한 root 권한을 사용하는(빌리는) 명령어이다.
su는 switch user의 줄임말로 현재 계정을 로그아웃하지 않고 다른 계정으로 전환하는 명령어이다.
su root
라면 현재 계정은 잠시 두고(로그아웃X) root 계정으로 전환하라는 것이다.
su 명령어는 사용시 옵션이 존재한다.
su -c # 쉘을 실행하지 않고 주어진 명령만 수행
su -s # 지정된 쉘로 로그인
su -,-l # 지정한 사용자의 환경 변수를 적용하여 로그인
+⍺
su root
와 su - root
에는 차이가 존재한다
su root
: root계정의 환경변수를 가져오지 않고 현재 계정의 환경 변수를 사용한다.
su - root
: root계정의 환경변수를 가져온다. (로그인시 기본 /root 디렉토리로 이동한다.)
현재 사용자를 확인하는 명령어이다.
전환 전 계정으로 돌아가는 명령어이다.
$ pwd
/Users/sangmin
현재 작업중인 디렉토리 정보 출력
# 절대 경로
$ cd /Users/sangmin/Desktop/code
$ pwd
/Users/sangmin/Desktop/code
# 상대 경로
$ cd ..
$ pwd
/Users/sangmin/Desktop
디렉토리 이동은 절대경로와 상대경로로 가능하다.
만약 현재 위치가 sangmin이고 code디렉토리로 이동하고 싶다면 cd ./Desktop/code
or cd Desktop/code
가 가능하다
만약 code에서 Desktop으로 가고싶다면 cd ..
로 상위 디렉토리로 이동가능하다.code에서 바로 Desktop으로 가고싶다면 cd ../..
으로 가능하다.
$ ls
MPI Python django_test locallibrary profile.pickle score.txt
$ls -l
total 16
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
drwxr-xr-x@ 7 sangmin staff 224B 9 17 21:03 Python
drwxr-xr-x@ 4 sangmin staff 128B 8 13 16:38 django_test
drwxr-xr-x@ 6 sangmin staff 192B 7 12 17:08 locallibrary
-rw-r--r-- 1 sangmin staff 52B 7 7 13:50 profile.pickle
-rw-r--r-- 1 sangmin staff 24B 7 7 13:32 score.txt
$ ls -a
. .DS_Store MPI django_test profile.pickle
.. .vscode Python locallibrary score.txt
$ ls -al
total 40
drwxr-xr-x@ 10 sangmin staff 320B 9 17 21:03 .
drwx------@ 41 sangmin staff 1.3K 9 28 20:53 ..
-rw-r--r--@ 1 sangmin staff 8.0K 9 17 21:02 .DS_Store
drwxr-xr-x@ 3 sangmin staff 96B 6 29 14:52 .vscode
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
drwxr-xr-x@ 7 sangmin staff 224B 9 17 21:03 Python
drwxr-xr-x@ 4 sangmin staff 128B 8 13 16:38 django_test
drwxr-xr-x@ 6 sangmin staff 192B 7 12 17:08 locallibrary
-rw-r--r-- 1 sangmin staff 52B 7 7 13:50 profile.pickle
-rw-r--r-- 1 sangmin staff 24B 7 7 13:32 score.txt
ls -l
의 경우 디렉토리의 내용을 자세하게 보여주고ls -a
의 경우 숨긴 디렉토리까지 전부 보여준다.(디렉토리 이름이 .으로 시작하면 숨긴 디렉토리이다.)
그외 -f, -r, -t, -h, -s
등 다양한 옵션이 존재하고 옵션들을 조합하여 사용한다.
ls - l
의 다음과 같은 정보는 무엇을 의미할까?
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
먼저 해당 정보를 8구역으로 나눌수 있다.
d | rwxr-xr-x@ | 29 | sangmin | staff | 928B | 6 29 14:39 | MPI
1영역 | 2영역 | 3영역 | 4영역 | 5영역 | 6영역 | 7영역 | 8영역
1 영역 : 종류 [일반파일(-), 디렉토리(d), 블록 디바이스(b), 문자 디바이스(c), 링크(l)]
2 영역 : 접근 모드 [r(read), w(write), x(execute)]
3 영역 : 하드 링크의 수 - 이 문서에 연결된 하드링크의 수로 윈도우의 바로가기와 유사한 개념이다.
4 영역 : 소유자
5 영역 : 그룹
6 영역 : 바이트(byte) 크기
7 영역 : 최종 수정 시각
8 영역 : 이름
접근 모드는 총 9자리 문자열로 표시되고 있다.1~3자리 소유주 권한 / 4~6자리 그룹 권한 / 7~9자리 그외 사용자 권한
즉, 위 예제에서 rwxr-xr-x
는 각각
rwx
: 사용자 읽기, 쓰기, 실행 권한
r-x
: 그룹 읽기, 실행 권한
r-x
: 그외 사용자 읽기, 실행 권한
을 갖음을 알 수 있다.
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
여기서 @는 무슨 의미일까?
@: The presence of the @ indicates that there are extended attributes.즉, 추가적인 속성값이 존재한다는 것이다. (OS X terminal에서만 존재한다.)
해당 속성값은 xattr '디렉토리 명'
or ls -al@
로 확인할 수 있다.
$ xattr MPI
com.apple.metadata:_kMDItemUserTags
$ ls -al@
total 40
drwxr-xr-x@ 10 sangmin staff 320B 9 17 21:03 .
com.apple.macl 72B
com.apple.metadata:_kMDItemUserTags 42B
drwx------@ 41 sangmin staff 1.3K 9 28 20:53 ..
com.apple.icloud.desktop 11B
com.apple.macl 72B
com.apple.metadata:_kMDItemUserTags 42B
-rw-r--r--@ 1 sangmin staff 8.0K 9 17 21:02 .DS_Store
com.apple.FinderInfo 32B
drwxr-xr-x@ 3 sangmin staff 96B 6 29 14:52 .vscode
com.apple.metadata:_kMDItemUserTags 42B
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
com.apple.metadata:_kMDItemUserTags 42B
drwxr-xr-x@ 7 sangmin staff 224B 9 17 21:03 Python
com.apple.metadata:_kMDItemUserTags 42B
drwxr-xr-x@ 4 sangmin staff 128B 8 13 16:38 django_test
com.apple.metadata:_kMDItemUserTags 42B
drwxr-xr-x@ 6 sangmin staff 192B 7 12 17:08 locallibrary
com.apple.metadata:_kMDItemUserTags 42B
-rw-r--r-- 1 sangmin staff 52B 7 7 13:50 profile.pickle
-rw-r--r-- 1 sangmin staff 24B 7 7 13:32 score.txt
파일 혹은 디렉토리를 복사한다.디렉토리 복사시 -r
옵션을 주어야한다.
디렉토리를 생성한다.-p
옵션 사용시 하위 디렉토리까지 한번에 생성 가능하다.
$ mkdir testdir
$ ls
MPI django_test profile.pickle testdir
Python locallibrary score.txt
$ mkdir -p a/b
$ ls -R a/
b
a//b:
파일이나 디렉토리를 삭제한다. 디렉토리 삭제시 -r
옵션을 주어야 한다.
-f
옵션 사용시 삭제 여부를 묻지 않고 바로 삭제한다.물론 하위 디렉토리까지 모두 삭제된다.
$ rm -r testdir
examine files in directory testdir? y
remove testdir? y
$ ls
MPI Python django_test locallibrary profile.pickle score.txt
파일이나 디렉토리의 최근 업데이트 일자를 현재 시간으로 변경한다.
만약 파일이나 디렉토리가 존재하지 않는다면 빈 파일을 만든다.
$ ls -l
drwxr-xr-x@ 29 sangmin staff 928B 6 29 14:39 MPI
$ touch MPI
$ ls -l
drwxr-xr-x@ 29 sangmin staff 928B 9 28 21:46 MPI
사실 cat의 기능은 출력만 있는 것은 아니다. 파일 출력, 파일 합치기, 기존 파일에 다른 파일에 덧붙이기, 새로운 파일 만들기 등이 cat이 하는 일들이다.
file1에는 a가 file2에는 b가 file3에는 c가 적혀있다.
# 파일 출력
$ cat file1
a
$ cat file2
b
$ cat file3
c
# 파일 합치기
$ cat file1 file2 > file1_2
$ ls
file1 file1_2 file2 file3
$ cat file1_2
a
b
# 파일 덧붙이기
$ cat file1 >> file2
$ cat file2
b
a
# 파일 생성
$ cat > file4
hello world
(ctrl + d로 파일 저장)
# cat file4
hello world
특정 파일이나 디렉토리를 검색한다.사용법은 다음과 같은 구조로 되어있다.
find [ 검색경로 ] -name [ 파일명 ]
$ ls
MPI Python django_test locallibrary profile.pickle score.txt
# 파일명 찾기
$ find ./ -name 'MPI'
.//MPI
# 특정 확장자인 파일 찾기
$ find ./ -name '*.txt'
.//MPI/Hw2(middle)/input.txt
.//MPI/병렬과제2.txt
.//score.txt
# -type 옵션으로 디렉토리나 파일만 지정 검색
$ find ./ -type d
~ 디렉토리만 검색
$ find ./ -type f
~ 파일만 검색
# 찾은 파일들을 rm하는 명령어
$ find ./ -name ".text" -exec rm {} \;
# txt 파일 안 'hi' 문자열을 'hello'라고 바꾸는 명령어
$ find ./ -name "*.txt" -exec sed -i 's/hi/hello/g' {} \;