Perl script to copy files from a linux directory and exclude sub-directories already there

Hi Guys,

I'm looking for a perl script to copy files from one directory location in linux to another but I only want the script to copy files and exclude directories. Can someone show me an example of how this can be done?
TbalzAsked:
Who is Participating?
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.

tel2Commented:
Hi Tbalz,

Q1. Do you mean you don't want to copy files from inside sub-directories in the source directory?  Do you just want to copy files from the source directory itself?
Q2. Or do you want to copy files from all levels, but copy them to a single destination directory?
Q3. Do you need a Perl script or would a bash script suffice?
0
TbalzAuthor Commented:
Hi Tel2,

For example I have a super directory in which there are 10 files and 2 directories. I want to use he mv command to copy just the files and leave the directories alone. However I can not get this functionality to work with bash. I have tried everything and even tried shopt and it just doesn't work from within a cron job. I always want those 10 files and i just want to leave he 2 directories alone. Is there anything I can do to get this work in Perl?
0
tel2Commented:
Hi Tbalz,

Please answer Q3 from my previous post.  You may not be able to do it in bash but we may be able to.

Q4. When you say it doesn't work from within a cron job, is that your real problem?  Were you able to get it to work from the command line?

Thanks.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

TbalzAuthor Commented:
I actually get it to work from command line and when i execute the script manually usually ./name_of_script.

Q3. If it would work in bash it would be great, but it is not working with the cron job and I would also like to learn Perl so i want to see if there is a different or easier way of doing it in Perl.
0
tel2Commented:
In general, when asking questions like this, it's good to say/show what you've tried in the original post.  I think bash is probably more suited to this kind of thing than Perl, though Perl can also do it.

Please show us your bash script that works from the command line but not from cron.
0
TbalzAuthor Commented:
Ok Here is what is working from script and not from cron

#!/bin/bash
#This script will check to see if any files in this directory are being written to at the moment and if not it will copy them to various directory
#and also send out an email notification

for f in /scripts/testing/Users/Incoming/*
do
    lsof "$f" | grep -q COMMAND &>/dev/null
    if [ $? -ne 0 ]
    then
        #echo "$f is not being written to, so its ok to move the file from the direcotry now"
        echo "The Files below are being moved now"
        mv "$f" /scripts/testing/tmp/Users/Incoming
        cp -rf /scripts/testing/tmp/Users/Incoming /home/bob/Users/
        cp -rf /scripts/testing/tmp/Users/Incoming /scripts/testing/tmp2/Users/
        rm -f /scripts/testing/tmp/Users/Incoming/*
        chown -R bob.bob /home/bob/*
        chmod -R 755 /home/bob/*
        #email test program
        recipients="super1@example.com example@gmail.com"
        from="ftp_notification_bobsystems@example.com"
        datenow=`perl -MPOSIX -le 'print strftime "%F %T", localtime $^T'`
        printf  "Subject: Test" | sendmail -vf $from $recipients <<EOF
        subject:$f "has been uploaded to Folder"
        from:$from
        Hello,

        A new file has been uploaded to the  FTP site. 

        The file has been uploaded to the Users Incoming Folder and will be moved to the proper location.

        The name of the file is $f

        The date of the upload is: $datenow

       Please do not reply to this email notification.
EOF
    else
        echo "Warning: Open file $f"
    fi
done

Open in new window

0
tel2Commented:
Hi Tblaz,

Q5. What output + errors do you get:
a) When you run it from the command line?
b) When you run it from cron?

Q6. Where you have this command:
    chown -R bob.bob /home/bob/*
Don't you mean:
    chown -R bob:bob /home/bob/*
?

Q7. Is the emailing actually working perfectly from the command line?  I'm not familiar with that syntax.

BTW, I think this:
    datenow=`perl -MPOSIX -le 'print strftime "%F %T", localtime $^T'`
can probably be simplified to this:
    datenow=`date +'%F %T'`
Try it first.

I don't know if I'll have time to help you finish this, so you might have to wait for another expert.
0
käµfm³d 👽Commented:
Here's one approach in Perl:

use warnings;
use strict;

my @files = <SourceDirectory/*>;
my $target_dir = "TargetDirectory";

if (!(-e $target_dir)) {                                # -e checks if $target_dir exists, but does not say whether it's a file or directory
    mkdir $target_dir;
}
elsif (-f $target_dir) {                                # -f checks if $target_dir is a file (-d would check for directory)
    die "Target directory name exists as a file";
}

foreach my $filename (@files) {
    my @parts = split(/\//, $filename);                 # Separate the filename from the directory name
    rename $filename, "$target_dir/$parts[$#parts]";    # Use the filename from the above line and append it to the target directory for the move
}

Open in new window

0
TbalzAuthor Commented:
Hi Kaufmed,

This script is great! but the problem is when i ran it with my directory structure it moved everything in the source directory to the target directory location. It basically didn't leave the sub-directories inside of the source directory and only copy the files. Is there anything I could be doing wrong?
0
käµfm³d 👽Commented:
No, that's my fault. I had another bit in there, but I removed it just before I posted because I forgot why it was in there. Now I realize what it was doing  : \

Corrected:

use warnings;
use strict;

my @files = <SourceDirectory/*>;
my $target_dir = "TargetDirectory";

if (!(-e $target_dir)) {                                # -e checks if $target_dir exists, but does not say whether it's a file or directory
    mkdir $target_dir;
}
elsif (-f $target_dir) {                                # -f checks if $target_dir is a file (-d would check for directory)
    die "Target directory name exists as a file";
}

foreach my $filename (@files) {
    if (-f $filename) {
        my @parts = split(/\//, $filename);                 # Separate the filename from the directory name
        rename $filename, "$target_dir/$parts[$#parts]";    # Use the filename from the above line and append it to the target directory for the move
    }
}

Open in new window

0

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
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
Perl

From novice to tech pro — start learning today.