linux - Perl Directory Finder doesn't work? -
i have script find directories , .tar.gz files. whatever reason (that's i'm hoping can me with) my
if ( -d $file )
doesn't return it's directory!!!
my script:
# specify directory want start search $directory = $argv[0]; $directorycount = 0; # calling subroutine, searches file readdirectory($directory); sub readdirectory { $directory = shift; $searchfile = shift; @directories; $tarouput; # little bit output, in directory script # searching @ moment (the following line not necessary) print "searching in $directory\n\n"; # open , close directory opendir(dir, $directory) or die("error: couldn't open specified directory $!"); @files = readdir dir; closedir dir; shift(@files); shift(@files); print "------------------------------------------------------------------------ \n\n"; foreach $currentfile (@files) { print "current file:", $currentfile, "\n\n"; if ( -d $currentfile ) #this doesn't work?!?!?!?!? { print "directory: ", $currentfile, "\n\n"; #push (@directories, $currentfile); #print "found new directory: $directories[$directorycount]\n\ncurrent number = $directorycount\n\n"; #$directorycount++; #print "directories: ", @directories, "\n\n"; #next; # subroutine calling hisself new parameters #readdirectory($currentfile); #recursive call through sub-directories } elsif ( $currentfile =~ /\.tar.gz$/i || $currentfile =~ /\.tar$/i) { print "file: ", $currentfile, "\n\n"; $taroutput = `tar -tvzf $currentfile`; print $taroutput, "\n"; } print "----------------------------------------------------------------------- \n\n"; } }
the print statement print $currentfile if it's directory never prints...
output:
searching in /home/gackerma/logs ------------------------------------------------------------------------ current file:dir1 ----------------------------------------------------------------------- current file:file ----------------------------------------------------------------------- current file:configs.tar.gz file: configs.tar.gz tar (child): configs.tar.gz: cannot open: no such file or directory tar (child): error not recoverable: exiting tar: child returned status 2 tar: error not recoverable: exiting ----------------------------------------------------------------------- current file:adstatlog.299 ----------------------------------------------------------------------- current file:adstatlog.tgz -----------------------------------------------------------------------
i've done exact same thing super simple script in same directory , works...but nope, not here. don't understand what's wrong...?
help please?
this looks wrong. see diff
below
--- yours.pl +++ mine.pl # open , close directory opendir(dir, $directory) or die("error: couldn't open specified directory $!"); - @files = readdir dir; + @files = grep { $_ !~ /^\.{1,2}$/ } readdir dir; closedir dir; - shift(@files); - shift(@files); foreach $currentfile (@files) { + $fullpath = "$directory/$currentfile"; print "current file:", $currentfile, "\n\n"; - if ( -d $currentfile ) #this doesn't work?!?!?!?!? + if ( -d $fullpath ) #this doesn't work?!?!?!?!? { print "directory: ", $currentfile, "\n\n"; #push (@directories, $currentfile); #print "directories: ", @directories, "\n\n"; #next; # subroutine calling hisself new parameters - #readdirectory($currentfile); #recursive call through sub-directories + readdirectory($fullpath); #recursive call through sub-directories }
i'm guessing double shift
meaning shift off "." , "..", incorrect. might work if did sort @files
, still bad way it. see grep
.
next problem $currentfile
needs full path here. otherwise it's looking $currentfile
in current working directory
Comments
Post a Comment