Solved

find file copy last subfolder

Posted on 2014-10-22
5
257 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now