Compress a Folder/Directory via Perl5
Compress a Folder/Directory via Perl5
tested in Windows, Mac OS X, Ubuntu16.04
#!/usr/bin/perl
#压缩指定目录
#nultiple folder
use IO::Dir;
use Archive::Tar;
use v5.16;
die "Give me some Folder\n" if ! @ARGV;
&tar_bz2(@ARGV);
sub tar_bz2 {
my @haystack = ();
my $fname = join ',', map { $_=~s/\/$//;$_ } @_;
#the compressed file name
$fname = @_.";".$fname;
while(my $dir = shift){ #iterate the given list
$dir =~ s|/$||;
if (! -d $dir){
push @haystack, $dir;
#not a dir/folder, file hodoooor
next
}
sub haystack {
my $dir = shift; # current dir name
my $pat = shift; # under where
my $d = IO::Dir->new("$dir");
if (defined($d)) {
chdir $dir;
while (defined($_ = $d->read)) {
next if ($_ =~ /^\.\.?$/);
if (-f "$_"){
push @haystack, "$pat/$dir/$_";
}
if (-d "$_") {
chmod 0755, "$_";
&haystack($_,"$pat/$dir");
# __SUB__ not work in windows
# no matter `use feature "current_sub"`
# or `use v5.16`
# run under perl v5.18.2
}
}
chdir("..");
}
}
&haystack("$dir",'.');
}
$fname =~ s/[^A-Za-z0-9_\-\.;]+/_/g;
$fname = substr($fname,0,15) if $fname > 14;
# create a Archive::Tar object
my $tar = Archive::Tar->new;
# add files
$tar->add_files(@haystack) or die "$!";
# say STDERR "$_" for (@haystack);
$tar->write("$fname.tbz",COMPRESS_BZIP);
say "Saved to $fname.tbz";
}
So,
$ perl ./backupRenLab.pl font/
Saved to 1;font.tbz
font/
|-- A/
|-- A File 2.txt
|-- AFile1.txt
|-- B/
|-- C/
|-- CFile.txt
The sturcture
More,
- Options Getopt::Long
-o(outdir),-t(compress method,gzip bz2,7z,xz),-f(tar filename),-l(compress level)
Compress a Folder/Directory via Perl5的更多相关文章
- Compress a folder using powershell
There are many ways to compress a folder using powershell: Method 1: Using System.IO.Compression and ...
- CentOS 7, Attempting to create directory /root/perl5
By francis_hao Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...
- Managing IIS Log File Storage
Managing IIS Log File Storage You can manage the amount of server disk space that Internet Informa ...
- 第十三章:Python の 网络编程进阶(二)
本課主題 SQLAlchemy - Core SQLAlchemy - ORM Paramiko 介紹和操作 上下文操作应用 初探堡垒机 SQLAlchemy - Core 连接 URL 通过 cre ...
- Ubuntu notes
ubuntu notes Table of Contents 1. backup data 2. Basics Ubuntu 3. Install, uninstall packages 4. Bas ...
- [.NET] 利用 async & await 进行异步 IO 操作
利用 async & await 进行异步 IO 操作 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6082673.html 序 上次,博主 ...
- C# XMLDocument
今天开发一个WPF模块需要本地化保存一些用户设置,鉴于数据量不大,用XML. (要是再小的话可以用Resources 和 Settings). 清晰简短教程移步:http://bdk82924.ite ...
- C#写入日志信息到文件中
为了在服务器上运行程序及时的跟踪出错的地方,可以在必要的地方加入写日志的程序. string folder = string.Format(@"D:\\{0}\\{1}", Dat ...
- BlogEngine2.9模仿yahoo滚动新闻Widget
widget.ascx <%@ Control Language="C#" AutoEventWireup="true" CodeFile="w ...
随机推荐
- linux设备模型_转
建议原博文查看,效果更佳. 转自:http://www.cnblogs.com/wwang/category/269350.html Linux设备模型 (1) 随着计算机的周边外设越来越丰富,设备管 ...
- 第十二篇 Integration Services:高级日志记录
本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...
- Megacli查看raid磁盘信息
准备: 新版本的 MegaCli-1.01.24-0.i386.rpm (下载地址:http://www.lsi.com/downloads/Public/MegaRAID%20Common%20Fi ...
- ipmi使用
1.安装ipmitool Linux: yum -y install OpenIPMI-tools 备注:Linux机器也可以安装ipmi yum -y install OpenIPMI OpenI ...
- 异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults
有方法说找到web.config 文件修改如下(蓝色部分) <behaviors> <serviceBehaviors> <behavior> ...
- Radius session
1,EAP 中继 client start, NAS require identity, client sent username, NAS sent username to sever, serve ...
- Java基础之序列化对象——将对象写入到文件中(SerializeObjects)
控制台程序. 首先定义一个含有任意不同数据类型域的可序列化类: import java.io.Serializable; public class Junk implements Serializab ...
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)
12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...
- Codeforce Round #221 Div2
每次的CF都是一把辛酸泪! 什么时候能打破这局面,昨天做着睡着了! 有时候有的题目也就差一线! 哎,! A:杠杆原理! B:算最后负的和! B:没弄出来当时就脑短路... C:事后写了个n*log(n ...
- 【皇甫】☀亲爱的~help me
亲爱的,我不知道该怎么把我想对你说的话表达出来,希望我对你的认识真的像下面的内容一样,如果我有错,那说明我还不够了解你... 希望我们能够一起走到最后吧... 首先,说说最近的吧, 在我还没有和你 ...