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

Linux I - 02. 리눅스 기본 명령어 (디렉토리 이동 관련 명령어)

 

 

 

[root@CentOS ~]# cd ..

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test
[root@CentOS /test]#

 

 

 

 

1. 외부 명령어

 

 - 디스크에 있으면 실행됨, 없으면 안됨
 - Ex) ls, mkdir, cp

 

[root@CentOS test]# whereis mkdir
mkdir: /bin/mkdir /usr/share/man/man2/mkdir.2.gz /usr/share/man/man1/mkdir.1.gz /usr/share/man/man3p/mkdir.3p.gz /usr/share/man/man1p/mkdir.1p.gz

[root@CentOS test]# whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

[root@CentOS test]# whereis cp
cp: /bin/cp /usr/share/man/man1/cp.1.gz /usr/share/man/man1p/cp.1p.gz

 


[root@CentOS test]# help ls   (외부 명령어는 'help' 명령어로 도움말 확인 안됨)
-bash: help: no help topics match `ls'.  Try `help help' or `man -k ls' or `info ls'.

 

[root@CentOS test]# man ls   (외부 명령어는 'man' 명령어로 도움말 확인)
[root@CentOS test]# ls --help    (또는 '--help' 명령어로 도움말 확인)

 

 


2. Shell 내장 명령어

 

 - Shell이 제공하는 명령어, 쉘을 삭제하기 전까지는 명령어 삭제 불가능
 - Ex) alias, cd, history

 

[root@CentOS test]# whereis alias
alias: /usr/share/man/man1/alias.1.gz /usr/share/man/man1p/alias.1p.gz

[root@CentOS test]# whereis cd
cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

[root@CentOS test]# man cd  ('cd' 명령어에 대한 내용이 아니라, 모든 쉘 명령어 도움말이 다 나옴)

[root@CentOS test]# help cd   ('cd' 명령어에 대한 실제 도움말이 나옴)

 

 

 

3.  Shell(명령어 해석기)

 

 

ls ---------> shell ---------> kernel -----------> CPU
                  001001010

 

 

 - Shell 종류 : bash, csh, ksh, zsh....
 
[[root@CentOS test]#  ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:04 bash
 7616 pts/0    00:00:00 ps

[root@CentOS test]# csh

[root@CentOS ~]# ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:04 bash <---- 'csh' 이전에 사용한 쉘, 지금은 'csh' 쉘로 동작함
 7617 pts/0    00:00:00 csh
 7626 pts/0    00:00:00 ps


[root@CentOS /test]# exit
exit


[root@CentOS test]#  ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:04 bash
 7627 pts/0    00:00:00 ps

[root@CentOS test]#  man cd <--- shell 내장 명령어 다 나옴

[root@CentOS test]#  help cd <-- cd 도움말 나옴

 

 

 

4. 'PATH' 환경 변수 (변수는 메모리에 올라가서 동작을 실시함)

 

PATH 변수는  명령어를 실행하기 위해서 해당 디렉토리를 검색할 필요 없이, 명령어를 사용할 수 있데 하는 변수이다. 다음과 같이 PATH 변수에 명령어를 갖고 있는 디렉토리가 등록되지 않으면 해당 명령어는 무조건 절대 경로로 찾아서 사용해야 한다. 참고로 변수를 확인하려면 '$'를 이용해야 한다.


[root@CentOS test]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[root@CentOS test]# which ifconfig
/sbin/ifconfig

[root@CentOS test]# time ifconfig    ('ls' 명령어를 PATH 환경 변수에서 찾음)

~ 중간 생략 ~

 

real   0m0.026s
user   0m0.001s
sys    0m0.025s

[root@CentOS test]# time abc    ('abc' 명령어를 PATH 환경 변수에서 찾음)
-bash: abc: command not found   ('abc' 명령어는 PATH 환경 변수에 없음)

real   0m0.002s
user   0m0.000s
sys    0m0.003s

 

 - 만약, PATH에 설정안된 명령어라면, 다음과 같이 절대 경로로 명령어를 실행해야 한다.

 

[root@CentOS test]# cp /bin/ls newls
[root@CentOS test]# ls
newls


[root@CentOS test]# cd ..
[root@CentOS /]# newls
-bash: newls: command not found

[root@CentOS /]# /test/newls
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys   tmp  var
boot  etc  lib  lost+found  misc   net  proc  sbin  srv      test  usr

[root@CentOS /]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin


Ex1) PATH 추가 I

 

[root@CentOS /]# a=hello
[root@CentOS /]# b=$a-linux
[root@CentOS /]# echo $b
hello-linux

 

 

Ex2) PATH 추가 II

 

[root@CentOS /]# PATH=$PATH:/test
[root@CentOS /]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/test

[root@CentOS /]# newls
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys   tmp  var
boot  etc  lib  lost+found  misc   net  proc  sbin  srv      test  usr

 

Ex3) PATH 추가 III


[root@CentOS /]# mkdir lab
[root@CentOS /]# cd lab
[root@CentOS lab]# cp /bin/ls ls2
[root@CentOS lab]# ls
ls2


[root@CentOS lab]# su - user1
[user1@CentOS ~]$

[user1@CentOS ~]$ PATH=$PATH:/lab

[user1@CentOS ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/user1/bin:/lab

[user1@CentOS ~]$ cd ..
[user1@CentOS home]$ cd ..
[user1@CentOS /]$ ls
bin   dev  home  lib    lost+found  misc  net  proc  sbin     srv  test  usr
boot  etc  lab   lib64  media       mnt   opt  root  selinux  sys  tmp   var

[user1@CentOS /]$
[user1@CentOS /]$ exit
logout

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


Ex4) PATH 추가 IIII


[root@CentOS test]# useradd ccna
[root@CentOS test]# su - ccna


[ccna@CentOS ~]$
[ccna@CentOS ~]$ echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/ccna/bin

[ccna@CentOS ~]$ PATH=/bin:/usr/bin
[ccna@CentOS ~]$ export PATH    (환경 변수로 지정하는 명령어)
[ccna@CentOS ~]$ echo $PATH
/bin:/usr/bin

[ccna@CentOS ~]$ ls -a
.  ..  .bash_logout  .bash_profile  .bashrc  .gnome2  .mozilla
[ccna@CentOS ~]$
[ccna@CentOS ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
       . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

[ccna@CentOS ~]$ exit
logout
[root@CentOS test]# usedel -r ccna

 

 

[참고] 사용자 정의 변수

 

[root@CentOS test]# a=1
[root@CentOS test]# $b=2
-bash: =2: command not found

[root@CentOS test]# echo $a
1

 

[참고] 'export' 명령어를 이용한 환경 변수 지정


[root@CentOS test]# a=1
[root@CentOS test]# b=2
[root@CentOS test]# export b
[root@CentOS test]# env
~ 중간 생략 ~

PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/test
PWD=/test
b=2
LANG=ko_KR.UTF-8
PS1=[\u@\h $PWD]#

~ 중간 생략 ~

 

[root@CentOS test]# echo $a
1
[root@CentOS test]#
[root@CentOS test]# echo $b
2

[root@CentOS test]# ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:05 bash
 8597 pts/0    00:00:00 ps

[root@CentOS test]# bash
[root@CentOS test]# ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:05 bash
 8598 pts/0    00:00:00 bash
 8608 pts/0    00:00:00 ps

[root@CentOS test]# echo $a


[root@CentOS test]# echo $b
2

[root@CentOS test]# exit
exit

[root@CentOS test]# ps
  PID TTY          TIME CMD
 2651 pts/0    00:00:05 bash
 8609 pts/0    00:00:00 ps

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

 

 

5. 'pwd' 명령어


[root@CentOS ~]# pwd
/root

 

[root@CentOS ~]# cd /etc/sysconfig/network-scripts/
[root@CentOS network-scripts]# pwd
/etc/sysconfig/network-scripts

 

[root@CentOS network-scripts]# cd
[root@CentOS ~]# pwd
/root

 

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

 

[root@CentOS ~]# PS1='C:> '
C:>
C:> PS1='[\u@\h \W]\$ '
[root@CentOS ~]#
[root@CentOS ~]# PS1="[\u@\h \$PWD]# "
[root@CentOS /root]# pwd
/root

 

 


6. 'cd' 명령어

 

Ex1) 상대 경로

 

[root@CentOS /root]# cd /etc
[root@CentOS /etc]# cd sysconfig
[root@CentOS /etc/sysconfig]# cd network-scripts
[root@CentOS /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts

[root@CentOS /etc/sysconfig/network-scripts]# ls
ifcfg-eth0   ifdown-isdn    ifup-aliases  ifup-plusb     init.ipv6-global
ifcfg-lo     ifdown-post    ifup-bnep     ifup-post      net.hotplug
ifdown       ifdown-ppp     ifup-eth      ifup-ppp       network-functions
ifdown-bnep  ifdown-routes  ifup-ippp     ifup-routes    network-functions-ipv6
ifdown-eth   ifdown-sit     ifup-ipv6     ifup-sit
ifdown-ippp  ifdown-tunnel  ifup-isdn     ifup-tunnel
ifdown-ipv6  ifup           ifup-plip     ifup-wireless

 

[root@CentOS /etc/sysconfig/network-scripts]# cd .
[root@CentOS /etc/sysconfig/network-scripts]# pwd
/etc/sysconfig/network-scripts

 

[root@CentOS /etc/sysconfig/network-scripts]# cd ..
[root@CentOS /etc/sysconfig]# pwd
/etc/sysconfig

 

[root@CentOS /etc/sysconfig]# cd ../../
[root@CentOS /]# pwd
/


[root@CentOS /]# cd /etc/sysconfig/network-scripts
[root@CentOS /etc/sysconfig/network-scripts]# touch ../../test.txt
[root@CentOS /etc/sysconfig/network-scripts]# cd ../../
[root@CentOS /etc]# pwd
/etc

 

[root@CentOS /etc]# ls -l test.txt
-rw-r--r--. 1 root root 0 2015-12-22 20:49 test.txt

 


Ex2) 절대 경로

 

[root@CentOS /etc]# cd /tmp
[root@CentOS /tmp]# cd /etc/sysconfig
[root@CentOS /etc/sysconfig]# cd /usr
[root@CentOS /usr]#
[root@CentOS /usr]# pwd

/usr

 


Ex3) 이전 디렉토리 이동

 

[root@CentOS /usr]# cd -
/etc/sysconfig
[root@CentOS /etc/sysconfig]#
[root@CentOS /etc/sysconfig]# cd -
/usr
[root@CentOS /usr]#

 


Ex4) 같은 트리 레벨 디렉토리 이동

 

[root@CentOS /usr]# cd..

[root@CentOS /]# mkdir test
[root@CentOS /]# cd test
[root@CentOS /test]# mkdir dir1 dir2
[root@CentOS /test]# cd dir2
[root@CentOS /test/dir2]# pwd
/test/dir2


[root@CentOS /test/dir2]# cd ..
[root@CentOS /test]# pwd
/test


[root@CentOS /test]# cd dir1
[root@CentOS /test/dir1]# pwd
/test/dir1


[root@CentOS /test/dir1]# cd ../dir2
[root@CentOS /test/dir2]# pwd
/test/dir2


[root@CentOS /test/dir2]# cd ../dir1
[root@CentOS /test/dir1]# cd ..
[root@CentOS /test]# tree
.
├── dir1
└── dir2

2 directories, 0 files

 

 

 

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

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


Q