Solved

find file copy last subfolder

Posted on 2014-10-22
5
262 Views
Last Modified: 2014-10-28
I have a very large directory organized by /date/broker/filename.wav I have a perl script where im using the find command piped to a cp statement to copy the file along with the directory to the /media/findoutput folder. Im using the --parents attribute of the cp command and this works great copying the whole directory structure to the findoutput folder.

My issue is I dont want the whole directory structure, I only want the last directory before the filename...like /audio/name/date/broker/########.wav I only want /broker/########.wav to be copy'd to /findoutput


find . -name '18380352.wav' -exec cp --parents '{}' /media/findoutput/ \;
find . -name '18380662.wav' -exec cp --parents '{}' /media/findoutput/ \;
find . -name '18380662.wav' -exec cp --parents '{}' /media/findoutput/ \;
find . -name '18380952.wav' -exec cp --parents '{}' /media/findoutput/ \;

I currently have about 600 lines like the above....doesn't get me what I want
0
Comment
Question by:Dalexan
  • 3
  • 2
5 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40397869
Can you post the perl script you currently have?  Are you using File::Find or just shelling out from perl and using system find?
0
 

Author Comment

by:Dalexan
ID: 40397875
Just shelling out with the command

find . -name '18380952.wav' -exec cp --parents '{}' /media/findoutput/ \;

about 600 lines of that...
0
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 475 total points
ID: 40397938
I'm not at an interpreter right now but this should be close to what you want...
use strict;
use warnings;
use File::Find;
#use Cwd;
use File::Path qw(make_path);
use File::Copy qw(cp);
*name = *File::Find::name;
*dir  = *File::Find::dir;
#my $cwd = Cwd::cwd();
my @files = qw(18380952.wav 18380662.wav ...); # full list of files
my %fhash = map { $_ => 1 } @files;
File::Find::find({wanted => \&wanted}, '.');
exit;
sub wanted {
    my $fn = $_;
    return unless exists($fhash{$fn});
    my ($ldir) = $dir =~ m{/([^/]+)/?\s*$};
    make_path("/media/findoutput/$ldir");
    cp $fn, "/media/findoutput/$ldir/$fn" or die "could not copy $fn to /media/findoutput/$ldir: $!";
}

Open in new window

0
 

Accepted Solution

by:
Dalexan earned 0 total points
ID: 40399345
Thanks for your quick response on this, I decided to use a little different method but it looks like your method would work just as well.
I used the below:
The first is just a script file with these entry's:
find ./recordings -name '18380352.wav' -exec /media/cpfile '{}' \;
find ./recordings -name '18380662.wav' -exec /media/cpfile '{}' \;
find ./recordings -name '18380663.wav' -exec /media/cpfile '{}' \;
find ./recordings -name '18380952.wav' -exec /media/cpfile '{}' \;

and then cpfile is another script file:
#!/usr/bin/perl
use File::Copy;

print "@ARGV[0]\n";
my @values=split('/',@ARGV[0]);
my $fpath=@values[scalar(@values)-2];
print "$fpath\n";
mkdir "/media/outfiles/$fpath";
copy(@ARGV[0], "/media/outfiles/$fpath/");

Open in new window

0
 

Author Closing Comment

by:Dalexan
ID: 40408018
My script works as well
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question