perl 打印目录结构
#!/usr/bin/perl
# 递归打印目录结构
use v5.26;
use strict;
use utf8;
use autodie;
use warnings;
use Encode qw(decode encode);
use File::Spec::Functions;
use File::Basename;
use experimental 'smartmatch'; # 忽略智能匹配的错误警告
use Getopt::Long qw(GetOptions);
use Term::ANSIColor;
# 递归目录结构的次数, 0全部递归
my $depath = 0;
my $help;
my $ignores;
GetOptions (
"depath=i" => \$depath,
"ignores=s" => \$ignores,
"help" => \$help
);
if(defined($help)){
print color('green');
say encode('utf-8', "
\t\$ atree [dir=./] [--ignores=./.atree] [--depath=0]
\t\$ atree
\t\$ atree ./lib
\t\$ atree ./lib --depath 1
\tor
\t\$ atree ./lib -d 1
\t\$ atree -i ~/.atree -d 3
");
exit;
}
my $dirPath = $ARGV[0] // "./";
unless(-d -e $dirPath){
print color("red");
say encode('utf-8', "$dirPath 不是目录,或则不存在!!");
exit;
}
my $ignoresFileName = ".atree";
my $ignoresPath = $ignores // catfile($dirPath, $ignoresFileName);
# 默认忽视这些目录
my @ignores = ();
# 读取忽略文件
sub readIgnores {
if(defined($ignoresPath) && -e $ignoresPath){
my $fh;
open $fh, "<", $ignoresPath;
while(<$fh>){
chomp;
$_ =~ s/^\s+|\s+$//g;
next if /^#/;
push @ignores, $_;
}
close $fh;
}
}
readIgnores();
sub scan {
my ($dir, $dep, $depathCount) = @_;
if($dir && -d -e $dir) {
my @files = <$dir/* $dir/.[!.]*>;
$depathCount++;
for(@files){
# next if /\.{1,2}$/g;
my $p = catfile($_);
if(-d $p){
my($filename) = fileparse($p);
unless($filename ~~ @ignores){
say "$dep$filename/";
if($depath eq 0 or $depathCount lt $depath){
scan($p, "| ".$dep."", $depathCount);
}
}
}else {
my($filename) = fileparse($p);
say "$dep$filename";
}
}
};
}
scan($dirPath, "|-- ", 0);
λ atree -i ~/.atree -d 3
|-- analysis_options.yaml
|-- bin/
| |-- main.dart
|-- CHANGELOG.md
|-- lib/
| |-- dart_demo.dart
|-- pubspec.lock
|-- pubspec.yaml
|-- README.md
|-- result.html
|-- test/
| |-- dart_demo_test.dart
|-- .dart_tool/
| |-- build/
| | |-- 098d3ee73e6cc294616d3a2e2c3c81ad/
| | |-- entrypoint/
| | |-- generated/
| |-- pub/
| | |-- bin/
|-- .gitignore
|-- .local-chromium/
| |-- 672088/
| | |-- chrome-win/
|-- .packages
λ cat ~/.atree
.git
node_modules
perl 打印目录结构的更多相关文章
- Python树形打印目录结构
前言 这两天整理数据文件的时候发现,一层层的点击文件夹查看很繁琐,于是想写一个工具来递归打印出文件目录的树形结构,网上找了一些资料几乎都是使用的os.walk, 调试了以后发现返回的貌似的是一个“生成 ...
- 递归打印目录层次(java版)
import java.io.File; public class Test { /** * * @param fileDir 根目录 * @param num 递归层次 */ private sta ...
- CentOS6.5菜鸟之旅:纯转载Linux目录结构
来自:http://www.iteye.com/topic/1125162 使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系 ...
- Centos目录结构详细版
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- CentOS目录结构超详细版
最近初学Linux 对linux的目录产生了很多疑问,看到这篇文章,让我顿时对目录有了一个清晰的认识!推荐给大家! ------------------------------------------ ...
- Linux目录结构【转】
使用linux也有一年多时间了 最近也是一直在维护网站系统主机 下面是linux目录结构说明 本人使用的是centos系统,很久没有发表博文了 近期会整理自己所用所了解知识点,发表linux相关的 ...
- 【Linux笔记】Linux目录结构
[Linux笔记]Linux目录结构 本文内容整理自网络,以作参考. /:根目录,位于linux文件系统目录结构的顶层,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib ...
- (大数据工程师学习路径)第一步 Linux 基础入门----目录结构及文件基本操作
Linux 目录结构及文件基本操作 介绍 1.Linux 的文件组织目录结构. 2.相对路径和绝对路径. 3.对文件的移动.复制.重命名.编辑等操作. 一.Linux 目录结构 在讲 Linux 目录 ...
- Centos7的目录结构
CentOS 目录结构 : /: 根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中/bin:/usr/bin: 可执行二进制 ...
随机推荐
- spark-submit提交spark任务的具体参数配置说明
spark-submit提交spark任务的具体参数配置说明 1.spark提交任务常见的两种模式 2.提交任务时的几个重要参数 3.参数说明 3.1 executor_cores*num_execu ...
- Python 学习博客地址
Alex https://www.cnblogs.com/alex3714林海峰 https://www.cnblogs.com/linhaifeng武佩奇 https://www.cnblogs. ...
- linux系统find命令详解+xargs命令 、exec命令
find 作用:查找文件 1.name: 指定文件名 例子1. 找到以du结尾的文件 ╭─root@localhost.localdomain ~ ╰─➤ find / -name "*du ...
- 网际互连__OSI七层模型
概述 OSI(Open System Interconnection,开放系统互连)七层网络模型称为开放式系统互联参考模型 ,是一个逻辑上的定义.一个规范.它把网络从逻辑上分为了7层.每一层都有相关. ...
- Git实践笔记(一)
Git是什么 Git是目前世界上最先进的分布式版本控制系统. 工作原理 / 流程: Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remo ...
- 并发编程补充--方法interrupted、isinterrupted详解
并发编程 interrupted()源码 /** * Tests whether the current thread has been interrupted. The * <i>int ...
- hdu 3974 Assign the task(dfs序上线段树)
Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...
- SPOJ Favorite Dice(概率dp)
题意: 一个骰子,n个面,摇到每一个面的概率都一样.问你把每一个面都摇到至少一次需要摇多少次,求摇的期望次数 题解: dp[i]:已经摇到i个面,还需要摇多少次才能摇到n个面的摇骰子的期望次数 因为我 ...
- Codeforces Round #665 (Div. 2) Distance and Axis、
题目链接:Distance and Axis 题意:在ox轴上,给出点A的横坐标x,你可以向左或右移动点A(x+1/x-1),问你最小移动A的次数,以使得可以在ox轴上找到B点位置,B点满足从O到B的 ...
- 用数组模拟STL中的srack(栈)和queue(队列)
我们在理解stack和queue的基础上可以用数组来代替这两个容器,因为STL中的stack和queue有可能会导致程序运行起来非常的慢,爆TLE,所以我们使用数组来模拟他们,不仅可以更快,还可以让代 ...