• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

perl gurus: script to store only the dirs to an array+find all duplicates?

endervalentine

Senior member
I'm new to perl and what I'm trying to do is to write a script that would search a specified directory and all it's subdirectories and output the files that have the same filenames. I do have a script written in bash that would spit out the files with the same chksum using sort|uniq but I can't figure out a way to do it by filenames and it has to be in perl.

As part of the script above I wanted to stuff an array with the directories at the highest level and can loop through that.

I'm having trouble getting an array with just the directory names ... I'm using perl to call a unix command and store the results into an array, but I can't seem to get the syntax correct.

@dir = split(/ /,`ls -l | grep ^d | awk '{print $8}'`);
print "@dir\n";

The command, ls -l | grep ^d | awk '{print $8}', works by itself but I can't get the syntax correct in perl.

thanks in adv!
 
I'd probably look at using the native file/directory functions like opendir/readdir or the File::Find module instead of calling out to shell commands.
 
As part of the script above I wanted to stuff an array with the directories at the highest level and can loop through that.

Why?

I'm having trouble getting an array with just the directory names ... I'm using perl to call a unix command and store the results into an array, but I can't seem to get the syntax correct.

@dir = split(/ /,`ls -l | grep ^d | awk '{print $8}'`);
print "@dir\n";

You should be splitting on \n, not space, if you're going to do it that way. Also you would be better served by a less-complex approach:

Code:
@dirs =  split /\n/, `find -maxdepth 1 -type d`

Note that this works only if your version of Find supports the maxdepth option (GNU find does), and will include hidden directories, including the current directory.

But really this is all a bit silly when you don't even care about the directory names. It would make more sense to just get a list of filenames and work with that. That could be accomplished by modifying the above command like so:

Code:
@files =  split /\n/, `find -type f`

This command as written will exclude softlinks.

Nothinman is correct that using more portable tools like the File::Find module is a better solution. File::Basename would also be helpful (these are both standard modules that come with Perl and work on virtually every platform in a predictable way). In general, running to the shell should be a last resort (though it rarely is).
 
Last edited:
Back
Top