※更に改訂してます。このエントリのUndoは期待に添った動きをしないので改良してあります。
こちらにどうぞ。
数日前に記載したスクリプトに危険な点があって、
ものの見事に僕自身がハマってしまったので、少しプログラムを書き足しました。
フールプルーフ的です。
もしもご利用になる方がいらっしゃいましたら、こちらの方をお使い下さい。
以下ソースコードです
gotoTrash
対象とするファイルをゴミ箱ディレクトリに移動するスクリプト
#! /usr/bin/perl use 5.010; use Cwd; #以下に記述されている「ゴミ箱のディレクトリ」には、 #任意の場所に自分で作った「ゴミ箱ディレクトリ」のパスを #記述してください。 #e.g. : "/home/owner/.Trash/" my $trashDirectoryPath = "ゴミ箱のディレクトリ"; unless( $trashDirectoryPath =~ m#/$# ){ $trashDirectoryPath .= "/"; } if( scalar @ARGV == 0 ){ exit; } my @fileName; foreach( @ARGV ){ unless( /\s*-+\w+\b/g ){ push( @fileName, $_ ); } } if( scalar @fileName == 0 ){ exit; } my @existFileName; foreach( @fileName ){ if( -e $_ ){ push( @existFileName, $_ ); }else{ warn "$_ : No such file or directory\n" } } if( scalar @existFileName == 0 ){ exit; } my ( $sec, $min, $hour, $day, $mon, $year, $wday, $yday, $isdst ) = localtime(time); $year += 1900; $mon += 1; my $timeStamp = sprintf "%4d%02d%02d%02d%02d%02d", $year, $mon, $day, $hour, $min, $sec; my $currentDirectoryPath = getcwd(); #ゴミ箱ディレクトリの下に、タイムスタンプがディレクトリ名となっているディレクトリを作る #e.g. : /home/owner/.Trash/YYYYMMDDhhmmss $trashDirectoryPath .= $timeStamp; mkdir "$trashDirectoryPath", 0755 or die "Cannot make directory: $!"; $trashDirectoryPath .= "/"; my $pathFile = $trashDirectoryPath . ".PATH"; unless( open OUT, " >$pathFile" ){ die "Cannot create PATH file : $!\n"; } print OUT $currentDirectoryPath . "\n"; close OUT; foreach( @existFileName ){ rename $_, $trashDirectoryPath.$_; } foreach( @existFileName ){ say "$_ -> $trashDirectoryPath"; }
注 : ファイルの移動と同時に作成される.PATHファイルは削除しないで下さい。
下記のスクリプトundoが上手く働かなくなります。
undo
直前にゴミ箱へと移動したファイルを、元の位置に戻すスクリプト
#! /usr/bin/perl use 5.010; use File::Path; #以下に記述されている「ゴミ箱のディレクトリ」には、 #任意の場所に自分で作った「ゴミ箱ディレクトリ」のパスを #記述してください。 #e.g. : "/home/owner/.Trash/" my $trashDirectory = "ゴミ箱のディレクトリ"; unless( $trashDirectory =~ m#/$# ){ $trashDirectory .= "/"; } my @directoryList = glob $trashDirectory."*"; if( scalar @directoryList == 0 ){ exit; } foreach ( @directoryList ){ s#^$trashDirectory##; } my @sortedDirectoryList = sort @directoryList; my $targetDirectory = $sortedDirectoryList[-1]; $targetDirectory = $trashDirectory . $targetDirectory; my $pathFileLocation = " <".$targetDirectory."/.PATH"; unless( open PATH, $pathFileLocation ){ die "Cannot open .PATH file: $!\n"; } my $targetUndoLocation; while( <PATH> ){ chomp; $targetUndoLocation = $_; } close PATH; my @targetUndoLocationList = split /\//, $targetUndoLocation; shift @targetUndoLocationList; #Remove null element my $directoryTest; foreach( @targetUndoLocationList ){ $directoryTest .= "/" . $_; unless( -e $directoryTest ){ mkdir "$directoryTest", 0755 or die "Cannot make directory: $!"; } } unlink $targetDirectory."/.PATH"; my @targetUndoFileList = glob $targetDirectory."/*"; my $targetUndoFile; foreach( @targetUndoFileList ){ $targetUndoFile = $_; s#^$targetDirectory/##; rename $targetUndoFile, $targetUndoLocation."/".$_; } rmtree $targetDirectory; say "Undo : $targetUndoFile -> $targetUndoLocation/";
注 : CPANからFile::Pathモジュールをインストールする必要があります。
cleanTrash
いわゆる「ゴミ箱を空にする」スクリプト
#! /usr/bin/perl use 5.010; use File::Path; #以下に記述されている「ゴミ箱のディレクトリ」には、 #任意の場所に自分で作った「ゴミ箱ディレクトリ」のパスを #記述してください。 #e.g. : "/home/owner/.Trash/" my $trashDirectory = "ゴミ箱のディレクトリ"; unless( $trashDirectory =~ m#/$# ){ $trashDirectory .= "/"; } my @trashDirectoryList = glob $trashDirectory."*"; foreach( @trashDirectoryList ){ rmtree $_; }
注 : CPANからFile::Pathモジュールをインストールする必要があります。
とまあこんな感じです。
ゴミ箱のディレクトリを設定するときに、末尾に"/"が入っていないと
色々と不都合な事態が生じるので、それをアレする文を足しただけでした。