Solved

find file copy last subfolder

Posted on 2014-10-22
5
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Zimbra on Amazon Linux help 7 115
Need to combine two scripts 2 40
RPM creation 6 23
Krita v3 Linux Mint/Ubuntu 16.04 9 23
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Utilizing an array to gracefully append to a list of EmailAddresses
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.

733 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