Unix Command 학습정리

Ubuntu 로 명령어 공부하면서 정리한 노트


목차


Command

Managing Packages

apt update
apt list
apt install [패키지 이름]
apt remove [패키지 이름]

Navigating the file system

pwd             # print the current working directory
ls              # list the files and directories
ls -l           # print ls commend in long list
ls -a           # list the files and directories including hidden files
ls -al          # print ls commend in long list including hidden files
cd /            # go to the root directory
cd [상대경로]    # go to 상대경로
cd ..           # go to one level up
cd ~            # go to the home directory
cd -            # go to the previous directory


# find명령어
find -type d # (directory type 만 찾기)
find -type f # (file type 만 찾기)

# (파일타입중에서 f로 시작하는 이름 모두찾기) (대소문자 구분)
find -type f -name "f*" 

# (파일타입중에서 f로 시작하는 이름 모두찾기) (대소문자 구분안함)
find -type f -iname "f*" 

# (root directory에서 *.py 패턴에 일치하는 파일을 모두 가져오기)
find / -type f -name "*.py" 

ls -l 할경우 해당경로 내 파일,폴더를 권환내용과 함께 출력된다.

d로 시작하는것은 directory

- 로 시작하는것은 file을 의미한다.


Manipulating files and directories

mkdir [directory name]            # create directory
mv [old_filename] [new_filename]  # rename file or directory
touch [filename]                  # create file
rm [filename]                     # remove filename
rm -r [directory]                 # recursively remove a directory
rm -f                             # remove a directory by force

# remove directory recursively
rm -rf [directory]

Managing env variables

printenv            # print all env path
printenv [PATH]     # print selected env path
echo $[PATH]        # print selected env path
export name=bob     # set variable path

Managing processes

ps                  # list runnning processes
kill [process ID]   # kill the process with selected ID value

Managing user and groups

useradd [name]   # create a user with out a home directory
useradd -m [name]   # create a user with a home directory

# to add a user interactive with default value from "/etc/adduser.conf"
adduser [name]      # useradd와 비슷하지만 더 많은 기능 보유
usermod             # modify user
userdel             # delete user

groupadd [group name]   # create a group
groups [username]       # get username's group list
groupmod                # modify group
groupdel                # delete group
groupadd developers # cat /etc/group 에서 group목록 확인가능

usermod -G developers john # developers 그룹에 john을 추가한다

# john의 password를 확인하고싶을때 두 가지 방법
-  cat /etc/passwd | grep john
- grep john /etc/passwd

Usermod Detail

# ref: https://dololak.tistory.com/270

usermod -c <"이름"> <계정> # 계정의 이름을 변경합니다.
usermod -d <"경로"> <계정>	# 계정의 홈 디렉터리를 변경합니다.
usermod -s <""> <계정> # 계정의 로그인 기본 셸을 변경합니다.
usermod -e <날짜> <계정> # 계정이 해당 날짜에 만료되도록 합니다.
ex) usermod -e 2018-05-01 myuser

usermod -g <그룹> <계정> # 사용자의 기본 소속 그룹을 변경합니다.
usermod -G <그룹> <계정> # 계정의 소속 그룹을 변경합니다.
# 만약 여러 그룹을 지정할 때에는 ,(콤마) 로 구분하여 지정합니다.

usermod -a -G <그룹> <계정>	# 계정의 소속 그룹을 추가(add) 합니다.

접근권환의 의미

  • rwx (read, write, execute)
  • rw- (read, write, )
  • r-x (read, , execute)

  • chmod u : user who owns the file
  • chmod g : groups that owns the file
  • chmod o : others

Managing File Permissions

# ref : codewithmosh.com
# additional study ref : https://ttend.tistory.com/686

chmod u+x [file name]     # give the owning user execute permission
chmod g+x [file name]     # give the owning group execute permission
chmod o+x [file name]     # give everyone else execute permission
chmod ug+x [file name]    # to give the owning user and group
                          # execute permission

chmod ug-x [file name]    # to remove the execute permission from
                          # the owning user and group

# deploy.sh 파일에 u 사용자에게 execute 권한을 부여한다
chmod u+x deploy.sh

# set read only permission only for the owing user 
chmod 400 mysecurity.pem

# give all permission of recursive directory to everyone
chmod -R 777 [directory] 

Reading Text files

# concatenate
cat
more

# apt install less
less
head -n 5 # 첫 5줄 출력
tail -n 5 # 마지막 5줄 출력

Redirection

# 출력된 내용을 [filename]으로 저장한다
[출력 명령어] > [filename]

# [filename] 을 읽어서 head 로 출력한다
head < [filename]

# filename을 head로 읽은 후 head로 출력된 결과물을 filename2로 저장
head < [filename] > [filename2]

# append contents to files.txt (not overwriting but appending)
ls >> files.txt

Cat 커맨드 심화

# file1.txt 파일내용을 읽은 후 file2.txt 다름이름으로 저장한다
cat file1.txt > file2.txt

# 한번에 두 파일을 읽어서 모든 합친내용을 출력
cat file1.txt file2.txt

# file1.txt와 file2.txt을 합쳐서 읽은 후 combined.txt로 다른이름으로 저장
cat file1.txt file2.txt > combined.txt 

# echo로 입력한 문자를 file1.txt로 저장
echo {문자} > file1.txt 

Grep 명령어

# ref : https://recipes4dev.tistory.com/157
global regular expression


grep -i # (대소문자 구분안하는 명령어)

# 대소문자 구분없이 'root' 이라는 단더를 /etc/passwd 경로에서 탐색
grep -i  root /etc/passwd


# 대소문자 구분없는 "hello": 단어를 file1.txt, file2.txt 파일 탐색
grep -i hello file1.txt file2.txt

# 대소문자 구분없는 "hello" 단어를 'file'이름을 가진 모든파일 내에서 검색
grep -i hello file*


# 이렇게 하면 . 이 directory라서 오류 발생
grep -i hello . 


# 현재다이렉토리 (.) 내에서 재귀식으로 대소문자 구분없는 'hello' 파일 읽기
grep -i -r hello . 


# 동일한 기능, 동일한 결과물 출력됨
grep -i -r hello .   
grep -ir hello .

Command Chain

# ; 로 여러 명령어를 한줄로 처리가능하다.
mkdir test; cd test; echo done

# 커맨드 실행오류가 발생한 경우 그 뒤에 있는 커맨드는 실행하지 않는다.
mkdir test && cd tst && echo done

# 좌측 커맨드 실행오류가 난 경우 우측 커맨드 실행
mkdir test || echo "mkdir test failed"
# ls /bin의 결과물을 less 커맨드 결과물로 출력하는 방식
ls /bin | less 
# 커맨드를 여러줄로 끊어서 작성
mkdir test; \
cd test; \
echo done

입문레퍼런스 링크