Link to home
Start Free TrialLog in
Avatar of Tbalz
Tbalz

asked on

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?
Avatar of tel2
tel2
Flag of New Zealand image

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?
Avatar of Tbalz
Tbalz

ASKER

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?
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.
Avatar of Tbalz

ASKER

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.
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.
Avatar of Tbalz

ASKER

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

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

Avatar of Tbalz

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial