perl script help

Hello Experts:

I am looking for multi-threaded script to parse through the NAS folders and subfolders .. and get checksum values of all files

Sample output
Path, File, checksumvalue

Using this script - doesn't work

use strict;
use warnings;
use File::Find ();
use Cwd ();

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;

my $cwd = Cwd::cwd();

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/directory1');

sub wanted {
    return unless (-f $_);
    my $cksum = `cksum $name`;
    $cksum =~ s{^\s+}{};
    $cksum =~ s{\s+.*$}{};
    print "$dir,$_,$cksum\n";

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

What doesn't work about it?  Do you get an error?  Is the output not what you expect?  If not, how does it differ?
CalmSoulAuthor Commented:
This is moving very slowing ... its not multi threaded ... that the most main point
find /directory1 -type f | xargs cksum
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

CalmSoulAuthor Commented:
Second bug: when file name has special chars like "(" or ")" I am error invalid token
How do you want it to multi-thread?  Split on each sub-dir under /directory1?  Start a new thread/process for each dir found?

The biggest hassle with multi-thread/process is handling output (it has a tendency to get jumbled together since each one is running asynch).
CalmSoulAuthor Commented:
@Wilcoxon: I haev 2.5 million files to process, all of the them are in the same dir ... I will like to start 2 threads with 100K each ... that will speed up the process.
CalmSoulAuthor Commented:
Here is the error on the special char:

sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `sha256sum /xxx/_Ju^ftg^Procedure^(Final^7-21-04).doc'

Open in new window

If all you want is to output the cksum, you are probably best off doing something like ozo suggests.

This will give you the syntax you want:
find /directory1 -type f | xargs cksum | perl -ne '@a = split; my ($p, $f) = $a[2] =~ m{^(.*)/([^/]+)}; print "$p,$f,$a[0]\n"'

Open in new window

find /directory1 -type f -print0 | xargs -0 cksum

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
To incorporate ozo's fix and a fix for filenames/dirs with spaces...
find /directory1 -type f -print0 | xargs -0 cksum | perl -ne '@a = split /\s+/, $_, 3; my ($p, $f) = $a[2] =~ m{^(.*)/([^/]+)}; print "$p,$f,$a[0]\n"'

Open in new window

CalmSoulAuthor Commented:
@wilcoxon is putting the value in next time .. not on same line
Interesting.  Switching to null-terminated strings apparently did something odd.  Simple fix though...
find /directory1 -type f -print0 | xargs -0 cksum | perl -ne 'chomp; @a = split /\s+/, $_, 3; my ($p, $f) = $a[2] =~ m{^(.*)/([^/]+)}; print "$p,$f,$a[0]\n"'

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.