리눅스 파일 위치 - linugseu pail wichi

각종 파일과 디렉토리등의 경로 확인에 자주 사용되는 명령어를 살펴봅니다.

which

which는 특정명령어의 위치를 찾아주는 명령어이다.

which find /bin/find

검색 가능한 모든 경로에서 해당 명령어를 찾음

which -a find /bin/find /usr/bin/find

where

whereis는 명령어의 실행파일위치, 소스위치, man 페이지파일의 위치를 찾아주는 명령어이다.

whereis find find: /bin/find /usr/bin/find /usr/share/man/man1/find.1.gz

locate

locate는 다양한 패턴의 파일들을 찾고자 할 때 매우 유용하게 사용되는 명령어이다.

현재 시스템에 존재하는 *.bak 에 해당하는 파일 찾기

locate *.bak

특정패턴에 해당하는 파일들 가운데 지정한 개수만큼 검색

locate -n 10 *.conf


공유하기

게시글 관리

구독하기WEBDIR

  • 카카오스토리
  • 트위터
  • 페이스북

'리눅스 > Linux 일반' 카테고리의 다른 글

DNS서버(네임서버)의 이해  (9)2013.06.22하드 디스크의 이해  (2)2013.06.20리눅스 grep 과 awk 를 이용한 파일포맷 및 패턴처리  (0)2013.06.16리눅스 grep을 이용한 문자열 검색 및 편집처리  (0)2013.06.16리눅스 find - 파일 검색  (3)2013.06.16

그런데 이 명령어는 어느 디렉터리에 있는 걸까요? 이 때 사용할 수 있는 명령어가 바로 $ which unzip /usr/bin/unzip8입니다. 이 글에서는 which 명령어 사용법과 기본적인 동작 원리, 그리고 옵션들을 알아봅니다.

  • which 명령어로 실행 파일 경로 찾기
  • $PATH 환경변수와 which 명령어
  • $ which unzip /usr/bin/unzip9 환경변수와 $ which unzip /usr/bin/unzip8의 동작 원리
  • $ which whoami /usr/bin/whoami $ which mkdir /bin/mkdir $ which ping /sbin/ping1 옵션: 모든 실행 파일 경로 출력
  • 종료 코드(Exit Status)
  • 추천 문서

which 명령어로 실행 파일 경로 찾기

which의 첫 번째 인자로 명령어 이름을 넘겨주면 이 실행 파일의 위치를 확인할 수 있습니다. 앞에서 실행해본 $ which unzip /usr/bin/unzip7의 경로가 궁금하다면 다음과 같이 실행합니다.

$ which unzip /usr/bin/unzip

간단하죠? which 명령어는 이 정도만 알고 있어도 활용도가 매우 높으니 꼭 기억해두시기 바랍니다.

자주 사용하는 다른 명령어들의 위치도 한 번 찾아봅니다.

$ which whoami /usr/bin/whoami $ which mkdir /bin/mkdir $ which ping /sbin/ping

다들 시스템 어딘가에 있었네요. 명령어의 위치는 배포판이나 셸에 따라서 다를 수 있습니다.

윈도우에는 $ which unzip /usr/bin/unzip8 명령어가 없는데, 대신 같은 역할을 하는 $ which whoami /usr/bin/whoami $ which mkdir /bin/mkdir $ which ping /sbin/ping4 명령어가 있습니다. 이 명령어에 대해서는 다음 글을 참고해주세요.

  • 관련 글: Where.exe 사용법: 윈도우의 which 명령어

$PATH 환경변수와 which 명령어

which를 사용해서 명령어들의 위치를 찾을 수 있습니다만, 여전히 의문은 남을 수 있습니다. $ which unzip /usr/bin/unzip8 명령어는 어디에서 이 명령어를 탐색하는 걸까요?

리눅스를 사용하다보면 $ which unzip /usr/bin/unzip9 변수에 자주 접할 수 있습니다. 셸에서 어떤 명령어를 전체 경로 없이 실행할 때 기본적으로 이 $ which unzip /usr/bin/unzip9 환경변수의 값을 참조합니다. 이 변수에는 디렉터리 목록이 담겨있습니다.

다음 명령어로 현재 $ which unzip /usr/bin/unzip9 환경변수 내용을 출력할 수 있습니다.

$ echo $PATH /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/opt/homebrew/sbin

$ which unzip /usr/bin/unzip9에는 다수의 디렉터리가 $ echo $PATH /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/opt/homebrew/sbin0 문자로 연결되어있습니다. 다음 명령어로 한 줄 씩 나눠서 경로를 확인할 수 있습니다.

$ IFS=':' PATHS=($(echo $PATH)) $ for string in "${PATHS[@]}"; do echo "$string"; done /opt/homebrew/bin /opt/homebrew/sbin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /opt/homebrew/bin /opt/homebrew/sbin

앞에서 확인했던 명령어들의 위치는 모두 이 디렉터리 목록에 포함되어있습니다.

그렇다면 정말 $ which unzip /usr/bin/unzip9 변수가 $ which unzip /usr/bin/unzip8 명령어 동작에 영향을 끼칠까요? $ which unzip /usr/bin/unzip9 값을 빈 값으로 변경하고 $ which unzip /usr/bin/unzip8 명령어로 $ which unzip /usr/bin/unzip7의 위치를 찾아보겠습니다. $ echo $PATH /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/opt/homebrew/sbin6 명령어로 PATH 환경변수 값을 지정합니다.

$ export PATH= $ echo $PATH

이제 $ which unzip /usr/bin/unzip9 값은 비어있습니다. $ echo $PATH /opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin:/opt/homebrew/sbin8을 실행해봅니다.

$ which unzip bash: which: No such file or directory

앗차, $PATH 값이 비어있어서 $ which unzip /usr/bin/unzip8 명령어도 찾을 수 없게 되어버렸습니다 😅 $ which unzip /usr/bin/unzip8 명령어는 $ IFS=':' PATHS=($(echo $PATH)) $ for string in "${PATHS[@]}"; do echo "$string"; done /opt/homebrew/bin /opt/homebrew/sbin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /opt/homebrew/bin /opt/homebrew/sbin1에 있으니 전체 경로를 입력해 명령어를 실행해보겠습니다.

$ /usr/bin/which unzip

아무런 값도 출력되지 않습니다. $ which unzip /usr/bin/unzip7을 실행해봐도 해당 실행 파일을 찾을 수 없다는 에러가 출력됩니다.

$ unzip bash: unzip: No such file or directory

다시 $ which unzip /usr/bin/unzip9 변수에 $ which unzip /usr/bin/unzip7이 있었던 $ IFS=':' PATHS=($(echo $PATH)) $ for string in "${PATHS[@]}"; do echo "$string"; done /opt/homebrew/bin /opt/homebrew/sbin /usr/local/bin /usr/bin /bin /usr/sbin /sbin /opt/homebrew/bin /opt/homebrew/sbin5을 추가하고 실행해보겠습니다.

$ export PATH=/usr/bin $ which unzip /usr/bin/unzip $ unzip UnZip 6.00 of 20 April 2009, by Info-ZIP. Maintained by C. Spieler. Send bug reports using //www.info-zip.org/zip-bug.html; see README for details. ....

다시 원래대로 동작하는 것을 확인할 수 있습니다!

$ which unzip /usr/bin/unzip9 환경변수와 $ which unzip /usr/bin/unzip8의 동작 원리

그렇다면 여러 디렉터리에 같은 이름을 가진 실행파일이 있는 경우 어떻게 동작할까요? $ which unzip /usr/bin/unzip8 명령어는 기본적으로 $ which unzip /usr/bin/unzip9 환경변수의 디렉터리 순서를 차례대로 탐색합니다.

간단한 테스트를 위해 이름이 같은 가짜 실행파일 $ export PATH= $ echo $PATH 0를 몇 개 디렉터리에 복사해보겠습니다.

$ which unzip /usr/bin/unzip0

다음으로 아래와 같이 $ which unzip /usr/bin/unzip9 환경변수를 변경합니다.

$ which unzip /usr/bin/unzip1

이제 $ which unzip /usr/bin/unzip8로 $ export PATH= $ echo $PATH 0 명령어 위치를 찾아봅니다.

$ which unzip /usr/bin/unzip2

$ export PATH= $ echo $PATH 4가 출력됩니다. 이번에는 $ which unzip /usr/bin/unzip9 환경변수의 디렉터리 순서를 변경하고 실행해봅니다.

$ which unzip /usr/bin/unzip3

이번에는 $ export PATH= $ echo $PATH 6가 출력됩니다. 즉, PATH 변수의 디렉터리를 순서대로 탐색해서 처음 만나는 실행 파일의 위치를 알려주는 것을 확인할 수 있습니다.

$ which whoami /usr/bin/whoami $ which mkdir /bin/mkdir $ which ping /sbin/ping1 옵션: 모든 실행 파일 경로 출력

그럼 같은 이름을 가진 실행파일을 모두 찾으려면 어떻게 해야할까요?

이 때는 $ which whoami /usr/bin/whoami $ which mkdir /bin/mkdir $ which ping /sbin/ping1 옵션을 사용하면 됩니다.

$ which unzip /usr/bin/unzip4

이제 $ export PATH= $ echo $PATH 0 명령어가 있는 모든 디렉터리가 출력됩니다.

종료 코드(Exit Status)

$ which unzip /usr/bin/unzip8는 실행 파일을 탐색 성공 여부에 따라서 다른 종료 코드를 가집니다. 이 종료 코드는 셸 스크립팅에서 유용하게 사용할 수 있습니다. 예를 들어 탐색한 명령어가 존재한다면 정상 종료 코드인 0으로 프로그램을 종료합니다. $ which unzip bash: which: No such file or directory1 변수를 출력해 마지막 명령어의 종료 코드를 확인할 수 있습니다.

$ which unzip /usr/bin/unzip5

존재하지 않는 실행파일을 탐색하면 종료 코드 1로 프로그램을 종료합니다.

$ which unzip /usr/bin/unzip6

잘못된 옵션을 사용하면 2로 종료됩니다.

명령어의 모든 옵션이나 종료 코드는 운영체제나 셸에 따라서 다를 수 있습니다. 이에 대해서는 현재 사용중인 리눅스의 man 페이지를 참고해주세요. 여기까지 리눅스 명령어 $ which unzip /usr/bin/unzip8의 사용법을 알아보았습니다.

Toplist

최신 우편물

태그