Skip to content

Install

install 명령은 파일을 복사하면서 동시에 권한, 소유자, 그룹을 설정할 수 있는 유틸리티입니다. 단순히 cp 명령으로 파일을 복사하는 것보다 더 강력하고, 주로 소프트웨어 설치 스크립트(Makefile 등)에서 사용됩니다.

Options

  • -m, --mode=MODE: 파일 권한 설정 (예: 755, 644)
  • -o, --owner=OWNER: 소유자 설정
  • -g, --group=GROUP: 그룹 설정
  • -d, --directory: 디렉토리 생성
  • -D: 필요한 모든 상위 디렉토리를 자동 생성
  • -v, --verbose: 상세 출력
  • -b, --backup: 백업 생성
  • -C, --compare: 파일이 다를 때만 복사
  • -s, --strip: 실행 파일에서 심볼 테이블 제거 (크기 감소)

cp vs install

cp 명령 대신 install 명령을 사용하는 이유는 다음과 같다:

  • install is mostly similar to cp but provides additionally the option to set the destination file properties directly without having to use chmod separately.
  • The install command is normally used in installation scripts that come with packages and source code for installing a binary to your system. It can also be used to install any other file or directory. In addition to the -d and -c options you have -m for specifying the new permissions of the file to be installed, so you don't have to do a cp and a chmod to get the same result. For instance: install -m644 "$srcdir/$pkgname-$pkgver-linux64" "$pkgdir/opt/$pkgname"
  • You also have options -g and -o for setting the target group and owner, respectively. This avoids separate calls to chown. In general, using install shortens your script and makes it more concise by doing file creation, copying, mode setting and related stuff in one command instead of many.
  • install은 파일을 기본 모드 755로 복사/설치합니다.
  • install은 파일의 소유자(owner) 또는 그룹(group) 및/또는 파일의 모드(mode)를 설정하도록 구성할 수 있습니다.
  • install은 원본 파일이 대체되기 전에 백업하도록 구성할 수 있습니다.
  • dd는 낮은 수준의 복사 도구로, 주로 소스(예: 파일 또는 장치)의 정확히 지정된 크기의 블록을 복사하는 데 사용됩니다.
  • cp는 파일을 복사하는 일반적인 명령이며, -r 옵션을 사용하여 디렉터리를 재귀적으로 복사하고, -p 옵션을 사용하여 권한을 보존할 수 있습니다.
  • install은 대부분 cp와 유사하지만, 별도로 chmod를 사용할 필요 없이 대상 파일 속성을 직접 설정할 수 있는 옵션을 추가로 제공합니다.
  • install 명령어는 일반적으로 패키지 및 소스 코드와 함께 제공되는 설치 스크립트에서 바이너리를 시스템에 설치하는 데 사용됩니다.
    • 다른 파일이나 디렉터리를 설치하는 데도 사용할 수 있습니다.
    • -d-c 옵션 외에도, 설치할 파일의 새 권한을 지정하기 위한 -m 옵션이 있어, 동일한 결과를 얻기 위해 cpchmod를 할 필요가 없습니다.
  • 또한, 대상 그룹과 소유자를 각각 설정하기 위한 -g-o 옵션이 있습니다.
    • 이는 별도의 chown 호출을 방지합니다.
    • 일반적으로, install을 사용하면 파일 생성, 복사, 모드 설정 및 관련 작업을 여러 명령 대신 하나의 명령으로 수행하여 스크립트를 단축하고 더 간결하게 만들 수 있습니다.

cp 와 비교하는 예제:

# cp를 사용하는 경우
cp myapp /usr/local/bin/
chmod 755 /usr/local/bin/myapp
chown root:root /usr/local/bin/myapp

# install을 사용하는 경우 (한 줄로 가능)
install -m 755 -o root -g root myapp /usr/local/bin/

Examples

# Example 1: 실행 파일 설치 (권한 755)
install -m 755 myapp /usr/local/bin/

# Example 2: 설정 파일 설치 (권한 644)
install -m 644 config.conf /etc/myapp/

# Example 3: 디렉토리 생성과 동시에 파일 설치
install -D -m 755 myapp /opt/myapp/bin/myapp

# Example 4: 여러 파일을 한 번에 설치
install -m 644 file1.txt file2.txt file3.txt /usr/share/doc/myapp/

# Example 5: 소유자와 그룹 지정
install -m 644 -o www-data -g www-data index.html /var/www/html/

# Example 6: 디렉토리만 생성 (권한 755)
install -d -m 755 /opt/myapp/{bin,lib,share}

# Example 7: 백업 생성하면서 설치
install -b -m 644 config.conf /etc/myapp/config.conf

# Scenario 1: Python 스크립트 시스템 전역 설치
install -m 755 myscript.py /usr/local/bin/myscript

# Scenario 2: 라이브러리 파일 설치
install -m 644 libmylib.so /usr/local/lib/
ldconfig  # 라이브러리 캐시 업데이트 / Update library cache

# Scenario 3: systemd 서비스 파일 설치
install -m 644 myapp.service /etc/systemd/system/
systemctl daemon-reload

# Scenario 4: 안전한 설치 (비교 후 복사)
install -C -m 644 config.conf /etc/myapp/config.conf

Makefile

# Makefile install target example
PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
DATADIR = $(PREFIX)/share/myapp

install:
    # Install binary / 바이너리 설치
    install -D -m 755 myapp $(BINDIR)/myapp

    # Install data files / 데이터 파일 설치
    install -D -m 644 data/config.ini $(DATADIR)/config.ini

    # Create directory / 디렉토리 생성
    install -d -m 755 $(DATADIR)/plugins

See also