Solved

find file copy last subfolder

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 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…
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…

724 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