디렉터리&파일 / 경로 / Linux 기본 명령어INFRA/Linux2024. 6. 27. 23:08
Table of Contents
Linux 디렉터리
- 디렉터리 == 폴더
- Linux 디렉터리 구조
- FHS(Filesystems Hierarchy System) : Tree 형태의 계층적인 구조
- 최상위 디렉터리 : /
- Linux 파일 및 디렉터리 특징
- 대/소문자 구분
- 파일과 디렉터리 이름 중복 불가
- 디렉터리 종류
/etc 환경설정 파일 var 수시로 업데이트 되는 파일 (log) /home 사용자 계정의 홈 디렉터리, 계정명으로 홈 디렉터리가 생성됨 lost + found 부팅 시 파일 시스템에 문제 발생 시 fsck(file system check) 명령으로 복구할 때 사용 /sys system 동작에 필요한 파일 proc 실행중인 프로세스, CPU, memory등의 시스템 정보가 가상 파일로 저장 - a.txt가 파일인지 디렉터리인지 판단불가 → 확장자 개념이 없음
[root@localhost ~]# ls -l
합계 4
-rw-------. 1 root root 985 5월 20 15:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 5월 20 16:28 공개
--맨앞을 보고 판단해야함 : - 파일, d 디렉터리
파일 종류
- 일반 파일
- d(/) 디렉터리 파일
- l(a) 심볼릭 링크파일
- 원본파일 위치 정보를 가지는 바로가기 단축 아이콘
- 하드 링크 : 원본 파일과 동일한 inode에 연결되는 링크
[root@localhost test]# ls -l /sbin
lrwxrwxrwx. 1 root root 8 5월 16 2022 /sbin -> usr/sbin
lrwxrwxrwx. 1 root root 7 5월 26 2022 clock -> hwclock
경로
- 절대경로
- 최상위 경로를 기준으로 현재 파일이나 디렉터리의 위치를 나타내는 경로
- 사용자의 현재 위치와는 관계 없음cd /etc/sysconfig
- 상대 경로
- 현재 위치를 기준으로 파일이나 디렉터리의 위치를 나타내는 경로
- cd ../sysconfig
- .. : 현재 디렉터리의 상위 디렉터리
- . : 현재 디렉터리
- 상황에 맞게 쓸 것
[root@localhost /]# cd /etc/sysconfig/network-scripts/
--상대경로로 최상위 디렉터리 이동
[root@localhost network-scripts]# cd ../../../root
--절대 경로로 이동
[root@localhost network-scripts]# cd /
alias
별칭설정 ↔ unalias
[root@localhost ~]# alias c=claer
*nano .bashrc : bashrc shell 파일 수정시 재부팅해도 저장되어있음
date
시스템의 시간과 날짜
[root@localhost ~]# date
2024. 05. 21. (화) 14:43:16 KST
man
메뉴얼 상세확인
[root@localhost ~]# man ls
rdate
원격지로부터 시간 동기화
- #rdate -s tme.bora.net (← NTP서버 : 정확한 표준시 알려줌)
- 시간에 맞춰서 작업을 많이 하기 때문에 중요 (핸드폰도 수시로 받아오고 있음)
- 윈도우 예시
- NTP 서버 한국 사이트로 변경
[root@localhost ~]# rdate -s time.bora.net [root@localhost ~]# timedatectl set-timezone Asia/Seoul [root@localhost ~]# nano /etc/chrony.conf
pool 2.pool.ntp.org iburst → server time.bora.net 변경
uname
시스템 정보 확인
[root@localhost ~]# uname
Linux
[root@localhost ~]# uname -a --모든 정보
Linux localhost.localdomain 5.14.0-70.13.1.el9_0.x86_64 #1 SMP PREEMPT Wed May 25 21:01:57 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -n --hostname
localhost.localdomain
[root@localhost ~]# uname -v --커널 버전
#1 SMP PREEMPT Wed May 25 21:01:57 UTC 2022
[root@localhost ~]# uname -r --커널 릴리즈 번호
5.14.0-70.13.1.el9_0.x86_64
which
명령어 저장 경로 확인
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
history
사용한 명령어 목록 확인
[root@localhost ~]# history
1 shutdown -r now
2 alias f=ifconfig
3 clear
재부팅
- Option-k(knock) 종료를 알리는 경고 메시지 전송(실제 종료 되지 않음)
-h Shutdown 후 종료(halt) -r Shutdown 후 재부팅(reboot) 실행 -c 예약되어 있는 shutdown을 취소 -f(fast reboot) 재부팅 시 fsck(fast system check)를 수행하지 않음(빠른 재부팅) - +다른 명령어
- #halt
- #init 0
- #poweroff : 전원 플러그 뽑듯 즉시 종료 → #sync로 하드디스크 동기화 해주기
--$ : 일반유저
[test01@localhost ~]$ su - root
암호:
--# : 관리자 권한 로그인
[root@localhost ~]#
[root@localhost ~]#shutdown -r now --지금 재부팅
[root@localhost ~]# shutdown -r +10 --10분후 재부팅 예약
Reboot scheduled for Tue 2024-05-21 16:42:11 KST, use 'shutdown -c' to cancel.
[root@localhost ~]# shutdown -c --예약 취소
- 프로그램 설치 시 윈도우와 달리 리눅스는 항상 재시작 해야한다.
-
- 프로그램 설정파일 열기 : .conf 로 원하는 것 수정
- 프로그램 관련 내용 설정
- 관련 데몬 restart : 해당 서비스에 필요한 포트 open
- 데몬 : 백그라운드에서 실행되는 컴퓨터 프로그램 시스템 서비스 제공 or 특정 작업을 주기적으로 수행
-
--항상 수동으로 restart 해줘야함
[root@localhost ~]# systemctl restart chronyd
ls
- 지정한 디렉터리의 파일 목록 출력
- -a :all → 숨김 파일 ’. 으로 표시’ 을 포함하여 출력
- -l : long →파일의 inode를 포함한 상세 내용 출력
[root@localhost ~]# ls
anaconda-ks.cfg 공개 다운로드 문서 바탕화면 비디오 사진 서식 음악
[root@localhost ~]# ls -a
. .bash_logout .cache .lesshst .tcshrc 다운로드 비디오 음악
.. .bash_profile .config .local anaconda-ks.cfg 문서 사진
.bash_history .bashrc .cshrc .mozilla 공개 바탕화면 서식
[root@localhost ~]# ls -l
합계 4
-rw-------. 1 root root 985 5월 20 15:43 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 5월 20 16:28 공개
...생략.....
[root@localhost ~]# ls -la
합계 36
dr-xr-x---. 14 root root 4096 5월 21 15:02 .
dr-xr-xr-x. 18 root root 255 5월 21 13:53 ..
...생략.....
[root@localhost ~]# ls /
afs boot etc lib media opt root sbin sys usr
bin dev home lib64 mnt proc run srv tmp var
--etc 디렉터리 내용을 자세히+숨김파일까지 상대경로로 출력
[root@localhost ~]# ls -la ../etc
--파일 검색
--test 문자열이 포함된 행 전부 검색
[root@localhost test]# ls -l |grep test
-rw-r--r-- 1 test03 test03 0 5월 27 15:31 file1
--디렉터리만 검색
[root@localhost test]# ls -l / |grep test
drwxr-xr-x 2 root root 4096 5월 28 15:10 test
pwd : print working directory
- 현재 디렉터리의 전체경로 표시
[root@localhost ~]# pwd
/root --/최상위 디렉터리 /하위 root에 와있음
cd : change directory
- 디렉터리 이동
--절대경로 사용해서 이동
[root@localhost ~]# cd /etc/sysconfig/
[root@localhost sysconfig]#
--상대경로를 사용해서 상위 디렉터리로 이동
[root@localhost sysconfig]# cd ../etc
[root@localhost etc]#
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /etc
[root@localhost etc]# cd - -- - : 바로 직전 root 디렉터리로 이동됨
/root
[root@localhost ~]# -- etc에서 바로 직전 root 디렉터리로 이동됨
mkdir
- 디렉터리 생성
[root@localhost /]# mkdir test
[root@localhost /]# ls
afs boot etc lib media opt root sbin sys tmp var
bin dev home lib64 mnt proc run srv test usr
-- -p 상위 디렉터리 함께 생성
-- a밑에 b밑에 c디렉터리가 있음
[root@localhost test]# mkdir -p a/b/c
[root@localhost /]# ls test
a
[root@localhost test]# cd a
[root@localhost a]# ls
b
[root@localhost a]# cd b
[root@localhost b]# ls
c
[root@localhost b]# cd c
[root@localhost c]# ls
-- 없음
--{}
[root@localhost test]# mkdir {e,f,g}
[root@localhost test]# ls
a e f g
[root@localhost test]# mkdir ab{a,b,c}
[root@localhost test]# ls
a aba abb abc e f g
rm
- 파일, 디렉터리 삭제
- -r : 해당 디렉터리의 하위 디렉터리까지 모두 삭제
- -f : 확인하지 않고 강제 삭제
--rm -rf(무조건)
[root@localhost test]# rm -rf abb
[root@localhost test]# ls
a aba abc e f g
[root@localhost test]# rm -rf a*
[root@localhost test]# ls
e f g
touch
- 파일 생성
[root@localhost test]# mkdir abc
[root@localhost test]# touch efg
[root@localhost test]# ls -l
합계 0
drwxr-xr-x 2 root root 6 5월 22 16:19 abc
-rw-r--r-- 1 root root 0 5월 22 16:19 efg
--포렌식할때 날짜 바꿈
[root@localhost test]# touch -t 199910101200 efg
[root@localhost test]# ll
합계 0
drwxr-xr-x 2 root root 6 5월 22 16:19 abc
-rw-r--r-- 1 root root 0 10월 10 1999 efg
cat
- 파일 내용 확인
-- >로 파일 내용 생성, >>로 수정
[root@localhost test]# cat > efg
testcat
abc123 --ctl d 저장후 종료
-- 파일 내용 출력
[root@localhost test]# cat efg
testcat
abc123
cp
- 복사
- cd -r : 하위 디렉터리까지 복사
--현재 디렉터리에 anaconda 복사
[root@localhost test]# cp /root/anaconda-ks.cfg .
[root@localhost test]# ls
aaa abc anaconda-ks.cfg efg
mv
- 파일 경로 이동
[root@localhost test]# ls abc anaconda-ks.cfg efg [root@localhost test]# mv anaconda-ks.cfg abc/snake [root@localhost test]# ls abc efg [root@localhost test]# cd abc [root@localhost abc]# ls snake
--파일명을 rr로 바꿀때 경로를 따로 안주면 test 디렉터리로 옴 [root@localhost test]# mv abc/aa rr [root@localhost test]# cd abc [root@localhost abc]# ls --사라짐 [root@localhost test]# ls abc efg rr
- --abc 디렉터리에서 anaconda 파일 잘라내서 test 디렉터리로 이동 [root@localhost abc]# ls anaconda-ks.cfg [root@localhost abc]# mv a* .. [root@localhost abc]# ls [root@localhost abc]# cd .. [root@localhost test]# ls aaa abc anaconda-ks.cfg efg
- 파일 이름 변경
- [root@localhost test]# ls aaa abc anaconda-ks.cfg ccc efg [root@localhost test]# mv anaconda-ks.cfg cobra-ks.cfg [root@localhost test]# ls aaa abc ccc cobra-ks.cfg efg
head
- 파일의 처음부터 지정된 줄까지 출력 (default : 처음부터 10줄)
[root@localhost test]# head -3 anaconda-ks.cfg
# Generated by Anaconda 34.25.0.29
# Generated by pykickstart v3.32
#version=RHEL9
[root@localhost test]# head anaconda-ks.cfg
# Generated by Anaconda 34.25.0.29
# Generated by pykickstart v3.32
#version=RHEL9
# Use graphical install
graphical
repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream
%addon com_redhat_kdump --enable --reserve-mb='auto'
%end
tail
- 파일의 마지막부터 지정된 줄까지 출력(default : 마지막부터 10행) ↔ head
- -f : 실시간 추가되는 내용 출력 → 주로 log파일을 확인할 때 자주 사용(log는 밑에서 부터 쌓이니)
[root@localhost test]# tail -3 anaconda-ks.cfg
# Root password
rootpw --iscrypted $6$cWvp1XfTVLecNgVT$QE4cgrQtLQIRVOOAoA6HH08mUFsmDGaEqJbUsAvywcpP.AxOdDbUxgWirPunqbW8LU3Ri7keEQbDVxggbsqwJ
less
- 파일 내용 길게 출력, more와 유사, 방향키로 페이지 이동 가능
- /검색하고 싶은 문자열 : 파일 내용 검색 가능
- 검색 내용 하이라이트되어 표시됨, b키로 여러 문자열 이동하며 찾고 q로 빠져나옴
grep
- 특정 문자열 검색
- root를 /etc/passwd에서 검색
ln
링크 파일 생성
- -s 심볼릭 링크파일 생성
[root@localhost link]# ln -s file1 file1-symbol
[root@localhost link]# ls
file1 file1-symbol
--다른 파일인것 고유번호로 확인가능
[root@localhost link]# ls -li
합계 0
70115379 -rw-r--r-- 1 root root 0 5월 23 14:19 file1
70115380 lrwxrwxrwx 1 root root 5 5월 23 14:19 file1-symbol -> file1
- 하드링크
[root@localhost link]# ln file1 file1-hard -- -s 없이 생성
--하드 링크는 같은 파일임
[root@localhost link]# ls -li
합계 0
70115379 -rw-r--r-- 2 root root 0 5월 23 14:19 file1
70115379 -rw-r--r-- 2 root root 0 5월 23 14:19 file1-hard
70115380 lrwxrwxrwx 1 root root 5 5월 23 14:19 file1-symbol -> file1
--파일내용 확인
[root@localhost link]# cat file1
test
[root@localhost link]# cat file1-hard
test
[root@localhost link]# cat file1-symbol
test
--file1 지우고 다시 내용 확인
[root@localhost link]# rm -rf file1
[root@localhost link]# ls
file1-hard file1-symbol
[root@localhost link]# cat file1-hard
test
[root@localhost link]# cat file1-symbol
cat: file1-symbol: 그런 파일이나 디렉터리가 없습니다
find
지정된 디렉터리 하위 파일 검색
- ?, * 사용해서 검색
[root@localhost test]# ls
dir1 ff file-mod file2
--현재 디렉터리에서 file2를 찾음
[root@localhost test]# find . -name file2
./dir1/file2
./file2
--파일 내용을 변경한 시간을 이용하여 찾기
[root@localhost /]# find /root -mtime +5
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
압축
- gzip
- 압축 해제 : gunzip =(-d)
[root@localhost test]# ls -l
-rw------- 1 root root 985 5월 24 10:12 anaconda-ks.cfg
-rw-r--r-- 1 root root 5 5월 24 14:16 test001
[root@localhost test]# gzip test001
[root@localhost test]# ls -l
-rw------- 1 root root 985 5월 24 10:12 anaconda-ks.cfg
-rw-r--r-- 1 root root 33 5월 24 14:16 test001.gz
[root@localhost test]# gzip -d test001.gz
[root@localhost test]# ls -l
-rw------- 1 root root 985 5월 24 10:12 anaconda-ks.cfg
-rw-r--r-- 1 root root 5 5월 24 14:16 test001
- tar
- 압축 : tar zcvf
- 압축 해제 : tar zxcf
- -c : 압축하고 싶은 파일 선택 후 create
- -x : tar 파일에서 정의된 파일을 추출
- -z : tar로 묶여진 파일을 gzip으로 압축하여 최종 파일을 생성
- test.tar.gz : 타블 소스
--tar묶으면서 zcvf 압축시키겠다. test001,2를
[root@localhost test]# tar zcvf test.tar.gz test001 test002
test001
test002
[root@localhost test]# ls
anaconda-ks.cfg test.tar.gz test001 test002
실습
1. 최상위 디렉터리 하위에 test라는 폴더 생성
# mkdir /test
2. 생성한 test 디렉터리로 이동
# cd /test
3. file1의 이름을 갖는 빈 파일 생성
# touch /test/file1
4. file2 파일을 생성하면서 hello라는 데이터를 새로 쓰기(단, 명령어 한 줄로 처리)
# echo hello > /test/file2
5. file2 데이터에 byebye 데이터 이어 쓰기(단, cat 명령어를 이용해서 추가)
# cat >> /test/file2
byebye 엔터 -> Ctrl + D
6. file2의 데이터 내용 확인
# cat /test/file2
7. 생성한 test 디렉터리 하위에 dir1 디렉터리 생성
# mkdir /test/dir1
8. test 디렉터리 하위에 있는 file2 파일을 이전에 생성한 dir1 폴더로 복사
# cp /test/file2 /test/dir1/
9. test 디렉터리 하위에 있는 file1 파일의 이름을 file1-mod로 변경
# mv /test/file1 /test/file1-mod
10. test 디렉터리 하위에 있는 dir1 디렉터리 하위에 있는 file2 파일의 하드링크 파일을
/root/file2-hard 이 위치에 파일 생성
# ln /test/dir1/file2 /root/file2-hard
11. test 디렉터리 하위에 있는 dir1 디렉터리 하위에 있는 file2 파일의 심볼릭 링크 파일을
/var/file2-soft 라는 파일 이름으로 생성(단, 상대경로로)
/test/dir1/file2 <- 원본 파일
/var/file2-soft <- 바로가기 파일
어디서든지, 상대경로로 만들려면 심볼릭 링크 파일의 입장에서 상대 경로로 생성
# ln -s ../test/dir1/file2 /var/file2-soft
12. 최상위 밑에 존재하는 test 폴더를 한번에 삭제
# rm -rf /test
'INFRA > Linux' 카테고리의 다른 글
RAID 구성+복구 실습 -> Window+Linux (1) | 2024.06.27 |
---|---|
Linux 소유권, 허가권, 특수 권한 (0) | 2024.06.27 |
Linux 계정 관리 (0) | 2024.06.27 |
Linux Software 관리 (0) | 2024.06.27 |
vi 편집기 (0) | 2024.06.27 |