Perl 脚本在给定的文件夹目录中递归查找文件
来源:互联网 发布:电视节目在线观看软件 编辑:程序博客网 时间:2024/06/06 23:18
最近在工作中使用到了Perl 脚本,需要在给定目录下对各类编译过的文件进行 Valgrind 安全检查。 谢了一个脚本自动将检查结果写入到文本文件当中。
程序名为 process_files:
转载请注明出处: http://blog.csdn.net/elfprincexu
原理: 从根目录(给定参数)开始查找, 如果找到的文件仍然为子文件夹,递归调用。
如果找到的是文件,则判断是否是我们感兴趣的文件。
查找过程中,排除linux下 . .. 文件夹
最后将结果输出到文本文件,便于查看和纠错。
主要函数: process_files
#!/usr/bin/perluse warnings;use strict;use IO::Handle;use File::stat;use Term::ANSIColor;STDOUT->autoflush(1);my $binStr = "bin.x86_64";my $dir = '/rapid/rapid_workspaces/jxu/jxu_RTC_New/Fusion/SW';print ("fusion dir is :$dir\n");my $wantedfiles = 0;my $processedfs =0;my $target = 'valgrind.log';my $processedfiles = 'valgrindProcessFiles.log';system ("rm -f $processedfiles"); #remove itsystem ("touch $processedfiles"); #create itprint ("processedfiles file is :$processedfiles\n");system ("rm -f $target"); #remove itsystem ("touch $target"); #create itprint ("target file is :$target\n");sub process_files {my $path =shift; #get the argumentopendir (DIR, $path) or die "unable to open $path: $!";my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directoryclosedir (DIR);@files = map{$path . '/' . $_} @files;for(@files) {if (-d $_) {process_files ($_);}else # this a file we found{my $mode = (stat($_)->mode) or (warn "$_ coun't get its mode\n"; next);#if not succcess, warn and go to next element;$mode = $mode & 0777;$processedfs++;print "$processedfs: $_ : $mode\n";print Processedfiles "$processedfs: $_ : $mode \n"; #out put processed files if ((index($_,$binStr)!=-1) #find bin.x86_64 str && ($mode & 111 == 111 )#execution permission file ) #bin.x86_64 files and has execution permission {print color 'red';$wantedfiles++;print ($wantedfiles,": $_ : $mode\n");# only these executable files print FILE "wanted files found: $wantedfiles : $_ : $mode\n";print color 'reset';system("valgrind --tool=memcheck --leak-check=full $_ >>$target 2>&1 &"); }}}}print "\nValgrind Unit Tests Start...\n";for (my $num =0; $num <3; $num++){print ("counting .... " ,3-$num , "\n");sleep (1);}print "\nValgrind Unit Tests now Start...\n";#really starts recursively start#open (FILE,'>>',"$target") or die "could not open $target : $!"; #append , if not exist, create a new fileopen (Processedfiles,'>>',"$processedfiles") or die "couldn't open the file : $!"; # open a file with open commandprocess_files($dir); close Processedfiles;close FILE;my $editor ="vi ";# finally we open the result filemy $pidEditor = fork();if ($pidEditor ==0) {my $editcmd = $editor . $target . " &";system ($editcmd);exit (0);}my $pidEditor2 = fork();if ($pidEditor2 ==0) {my $editcmd2 = $editor . $processedfiles . " &";system ($editcmd2);exit (0);}
其中,使用到了sub routine : process_files, 具体代码如下:
<pre name="code" class="plain">sub process_files {my $path =shift; #get the argumentopendir (DIR, $path) or die "unable to open $path: $!";my @files = grep{!/^\.{1,2}$/} readdir(DIR); #exclude . or .. directoryclosedir (DIR);@files = map{$path . '/' . $_} @files;for(@files) {if (-d $_) {process_files ($_);}else # this a file we found{$processedfs++;print "$processedfs: $_\n";print Processdfiles "$processedfs: $_\n";if ((index($_,$binStr)!=-1) && (index($_,'.')==-1)) #bin.x86_64 files and not contains . {print color 'red';$wantedfiles++;print ($wantedfiles,": $_");print color 'reset';# only these executable files system("valgrind --tool=memcheck --leak-check=full $_ >> $target &");}}}}
0 0
- Perl 脚本在给定的文件夹目录中递归查找文件
- 在文件中查找文本的Perl脚本
- 递归在文件夹中查找文件
- Perl 在指定文件夹中的文件中查找包含指定字符的行
- 脚本实现查找给定后缀的文件
- java 递归查找所有目录并且删除文件夹中包含temp的文件夹
- 在目录中递归查找文件名的插件
- 使用perl将当前目录中子目录下的相应文件拷贝到一个文件夹中
- JAVA 的递归方法查找目录下的文件和文件夹
- 递归删除给定路径下不包含文件的文件夹
- 递归查找一文件夹下的文件。
- 递归方式查找文件夹的所有文件
- perl脚本的目录
- Perl中递归遍历文件夹,寻找包含某个字符串的文件
- 递归搜索给定的目录下所有的文件
- 递归算法 列出给定目录下的文件/java描述
- 递归算法 列出给定目录下的文件/java描述
- 在一个文件夹下面的所有文件中查找字符串
- Linux下Kill函数用法
- Linux 初学者(仅一篇)
- Linux 下的串口编程(一)
- 浅尝C++——const与#define定义符号常量
- STM32 ADC的学习
- Perl 脚本在给定的文件夹目录中递归查找文件
- 创建具有管理员权限的Command命令行快捷方式
- opnet 14.5与vs2008联调
- jsp入门教程
- 删得了所有朋友圈,删得了记忆吗
- X-UA-Compatible IE 浏览器默认文档模式设置
- 1111
- XADM: 如何手动添加混合语言的代码页
- 【ZedGraph】基础