質問
私は自分の作業ディレクトリとは異なるディレクトリに、files.zip
、言う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で述べたように)、ディレクトリ変更の他のタイプのために、私は、ファイルを好き:: chdirのモジュール、可能になりますあなたperlの「ローカル」演算子と組み合わせたとき、ディレクトリの変更をローカライズする:
#!/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
また、アーカイブ::ジップモジュールを使用することができます。 extractToFileNamedで特異的に見てます:
「extractToFileNamed($ fileNameに)
指定した名前のファイルに私を抽出します。ファイルは、デフォルトのモードで作成されます。必要に応じてディレクトリが作成されます。引数$ filenameは、ファイルシステム上の有効なファイル名でなければなりません。 AZ_OKが成功した場合に返します。 「
所属していません StackOverflow