리눅스
locate , whereis , which , cat , touch , cp , mv, find , grep , wc (word count) , pipe
정지홍
2024. 7. 31. 07:45
locate keyword
#다음은 keyword에 해당되는 모든 항목을 찾는 명령어.
#만약 안되면 sudo updatedb를 입력후 실행
whereis name
#다음은 name에 해당되는 바이너리 파일,소스 및 매뉴얼 페이지를 찾는다.
which name
#name에 해당되는 바이너리 위치만 반환
cat > name
#name에 해당되는 파일 이름을 만든다.
#이중 리다이렉션이라면 파일 끝에 내용을 더 추가하는것이 가능
# ex) cat >> testfile
touch name
#name의 파일 건드려서 생성 혹은 수정 날짜를 변경한다. 기본적으로 name파일 없으면 생성
cp <src> <dest src>
#파일을 복사
mv <src> <dest src>
#파일 이름을 바꾸거나 이동
find
find <옵션> <경로> <표현식>
# 파일을 검색하는데 사용되는 명령어.
# 다양한 표현식을 사용하여 원하는 파일의 목록을 추출
# 옵션
# -> P : 심볼릭 링크를 따라가지 않고, 심볼릭 링크 자체 정보 사용
# -> L : 심볼릭 링크에 연결된 파일 정보 사용
# -> H : 심볼릭 링크를 따라가지 않으나, command line argument를 처리할 땐 예외
# -> D : 디버그 메시지 출력
find / -type f -name apchae2
# 루트 디렉터리에서 apache2라는 이름의 파일을 검색하는것
# find / 는 /가 루트 디렉토리를 의미
# -type f는 파일 유형을 지정하는데 f는 일반 파일을 의미
# 이는 모든 디렉토리를 거치면서 나열하니 검색은 느릴수있다.
# 속도를 높히고 싶다면 예상되는 디렉토리만 찾는다. (즉, 예시로 /가 아닌 /etc를 입력)
find /etc -type f -name apache2.*
#와일드카드를 이용해서 apache2로 시작하고 확장자가 있는 모든 파일을 찾는다
=============name 예시=====================
(base) jeongjihong@jjh:~$ find . -name "intellij.*"
./ideaIU/plugins/karma/js_reporter/karma-intellij/lib/intellij.conf.js
./ideaIU/plugins/maven/lib/intellij.maven.server.indexer
./intellij.sh
# 현재 디렉토리에서 intellij.가 포함되는 파일 찾기
(base) jeongjihong@jjh:~$ find . -name "*.sh"
./anaconda3/lib/python3.11/site-packages/bleach/_vendor/vendor_install.sh
./anaconda3/lib/python3.11/site-packages/pexpect/bashrc.sh
./anaconda3/lib/python3.11/site-packages/conda_build/launcher_sources/build.sh
# 현재 디렉토리에서 확장자가 sh인 파일 찾기
=============type 예시=====================
# d : 디렉토리
# f : 일반적인 파일
# l : 심볼릭 링크
(base) jeongjihong@jjh:~$ find ~/ -type d
# 현재 디렉토리에서 모든 디렉토리 찾기
(base) jeongjihong@jjh:~$ find ~/ -name "dfs" -type d
/home/jeongjihong/hdata/dfs
# 현재 디렉토리에서 dhs가 들어간 디렉토리 찾기
=============empty size 예시=====================
# b : 블록단위
# c : byte
# k : kbyte
# w : 2byte 워드
(base) jeongjihong@jjh:~$ find . -empty
# 현재 디렉토리에서 빈 디렉토리 or 크기가 0인 파일 검색
(base) jeongjihong@jjh:~$ find . -name "*cache" -empty
./hdata/nm-local-dir/usercache
./hdata/nm-local-dir/filecache
# 현재 디렉토리에서 cache로 끝나는 파일중 크기가 0인것 검색
(base) jeongjihong@jjh:~$ find . -size 1024c
# 현재 디렉토리에서 1024byte인 파일 검색
(base) jeongjihong@jjh:~$ find . -size +1024c
# 현재 디렉토리에서 1024byte보다 큰 파일 검색
(base) jeongjihong@jjh:~$ find . -size -1024c
# 현재 디렉토리에서 1024byte보다 작은 파일 검색
(base) jeongjihong@jjh:~$ find . -size +1k -size -20k
# 현재 디렉토리에서 1kb보다 크고 20k보다 작은 파일 검색
==============user 예시==============
(base) jeongjihong@jjh:~$ find . -user jeongjihong
# user소유의 파일 검색
==============mtime 예시==============
(base) jeongjihong@jjh:~$ find . -mtime -10
# 최근 10일동안 안에 변경된 파일 검색. during임. 즉, 0~10일의 파일 모두 검색
# +mtime은 10일전에 수정된 파일 검색
==============exce 예시==============
# find파일로 찾고 바로 연계하여 어떠한 명령을 내리는것이 가능.
# 결과는 {}로 표시하고 \;로 끝내야함
(base) jeongjihong@jjh:~$ find . -name "intellij.sh" -exec ls -l {} \;
-rwxr-x--- 1 jeongjihong jeongjihong 50 6월 17 09:20 ./intellij.sh
(base) jeongjihong@jjh:~$ find . -name "intellij.sh" -exec wc -l {} \;
3 ./intellij.sh
(base) jeongjihong@jjh:~$ find . -name "intellij.sh" -exec cat {} \;
#!/bin/bash
/home/jeongjihong/ideaIU/bin/idea.sh
(base) jeongjihong@jjh:~$ find . -name "intellij.sh" -exec grep "hong" {} \;
/home/jeongjihong/ideaIU/bin/idea.sh
==============print 예시==============
일반적으로 print가 기본 옵션
(base) jeongjihong@jjh:~$ find ./ -name "intellij*" -print0
# 개행문자로 구분하여 출력 x
(base) jeongjihong@jjh:~$ find ./ -name "intellij*" -print
# 개행문자로 구분하여 출력 o
==============perm , prune 예시==============
(base) jeongjihong@jjh:~$ find . -perm 644
# 권한이 644인 파일 검색
(base) jeongjihong@jjh:~$ find . -perm 644 -exec rm {} \;
# 특정 권한 찾아서 해당 파일 삭제
(base) jeongjihong@jjh:~$ find . -name "*cache*" -prune
# cache문자열을 포함하는 디렉토리는 하위 디렉토리는 검색 안함
grep
grep -c
일치하는 행의 수 출력
grep -b
검색 결과 내용이 디스크 어느곳에 위치한지 출력
grep -i
대소문자 구분 x
grep -I
대소문자 구분 o
grep -n
행 번호도 같이 출력
grep -s
에러 메시지외에는 출력 안함
grep -v
패턴과 일치 안하는것만 출력
grep -w
패턴과 일치하는 행만 출력
grep -r
하위 디렉토리도 같이 검색함
grep -m숫자
최대호 표시할수 있는 숫자 제한
grep -F
찾을 패턴을 문자열로 찾음
wc (word count)
(base) jeongjihong@jjh:~$ cat tes.txt
123
abc dfe fawerf aewgw qwertasdf
ASDFG QWERTYH ZSXCV
# 우선 연습에 필요한 파일 생성
====================c 옵션==========================================
(base) jeongjihong@jjh:~$ wc -c tes.txt
55 tes.txt
# 파일의 바이트 수 출력
(base) jeongjihong@jjh:~$ cat tes.txt | wc -c
55
# 파일의 바이트 수 출력
====================m 옵션==========================================
(base) jeongjihong@jjh:~$ wc -m tes.txt
55 tes.txt
# 파일의 글자수 출력
(base) jeongjihong@jjh:~$ cat tes.txt | wc -m
55
# 파일의 글자수 출력
====================l 옵션==========================================
(base) jeongjihong@jjh:~$ wc -l tes.txt
3 tes.txt
# 라인 수를 출력
(base) jeongjihong@jjh:~$ cat tes.txt | wc -l
3
# 라인 수를 출력
====================w 옵션==========================================
(base) jeongjihong@jjh:~$ wc -w tes.txt
9 tes.txt
# 단어 갯수 카운트
(base) jeongjihong@jjh:~$ cat tes.txt | wc -w
9
# 단어 갯수 카운트
pipe
pipe는 단어에서 알수있듯이 명령어가 흘러가는 통로다.
각각 파이프는 해당되는 조건을 필터링 한다.