Perl을 사용하여 압축 출력을 특정 디렉토리로 리디렉션합니다

StackOverflow https://stackoverflow.com/questions/1917009

  •  20-09-2019
  •  | 
  •  

문제

압축 된 파일을 막고 싶어요. files.zip, 내 작업 디렉토리와 다른 디렉토리로. 내 작업 디렉토리입니다 /home/user/address 그리고 파일을 압축하고 싶습니다 /home/user/name.

나는 그것을 다음과 같이하려고 노력하고있다

#!/usr/bin/perl
use strict;
use warnings;

my $files= "/home/user/name/files.zip"; #location of zip file
my $wd = "/home/user/address" #working directory
my $newdir= "/home/user/name"; #directory where files need to be extracted
my $dir = `cd $newdir`;
my @result = `unzip $files`; 

그러나 작업 디렉토리에서 위를 실행하면 모든 파일이 작업 디렉토리에서 압축되지 않습니다. 압축되지 않은 파일을 어떻게 리디렉션합니까? $newdir?

도움이 되었습니까?

해결책

unzip $files -d $newdir

다른 팁

Perl 명령을 사용하십시오

chdir $newdir;

그리고 백 티크가 아닙니다

`cd $newdir`

새 쉘을 시작하고 해당 쉘의 디렉토리를 변경 한 다음 종료합니다.

이 예제에서는 압축을 풀기위한 -d 옵션이 아마도 당신이 원하는 것을 수행하는 가장 간단한 방법 일 것입니다 (Ennuikiller가 언급 한 바와 같이) 다른 유형의 디렉토리 변화에 대해서는 파일을 좋아합니다. Perl "Local"연산자와 결합 될 때 디렉토리 변경 :

#!/usr/bin/perl
use strict;
use warnings;
use File::chdir;

my $files= "/home/user/name/files.zip"; #location of zip file
my $wd = "/home/user/address" #working directory
my $newdir= "/home/user/name"; #directory where files need to be extracted
# doesn't work, since cd is inside a subshell:   my $dir = `cd $newdir`;
{ 
   local $CWD = $newdir;
   # Within this block, the current working directory is $newdir
   my @result = `unzip $files`;
}
# here the current working directory is back to what it was before

아카이브 :: zip 모듈을 사용할 수도 있습니다. 특별히 추출한 tofilename을 봅니다.

"ExtractTofilename ($ filename)

주어진 이름의 파일로 나를 추출하십시오. 파일은 기본 모드로 작성됩니다. 필요에 따라 디렉토리가 만들어집니다. $ filename 인수는 파일 시스템에서 유효한 파일 이름이어야합니다. 성공에 AZ_OK를 반환합니다. "

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top