Solved

find file copy last subfolder

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

758 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

15 Experts available now in Live!

Get 1:1 Help Now