Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How to write the perl script for the following linux command?

by finddata (Sexton)
on Mar 14, 2017 at 11:39 UTC ( #1184533=perlquestion: print w/replies, xml ) Need Help??
finddata has asked for the wisdom of the Perl Monks concerning the following question:

i want the following comamnd to be written in perl script.How can do it in perl?
find /path/to/files -name '*.csv' | cpio -pdm /target

Replies are listed 'Best First'.
Re: How to write the perl script for the following linux command?
by davido (Archbishop) on Mar 14, 2017 at 14:25 UTC

    Are find and cpio unavailable to you? If you have something that works, but simply need to incorporate it into a larger script, you can use the qx{} operator or `backticks`, or system to execute the external calls.

    While it's a good practice to invoke external calls as infrequently as practical, it's also a good practice to have a clear goal in refactoring, and to leave things that work as they are unless the clear goal necessitates the refactor.

    Writing more code to re-implement the existing solution is just likely to introduce new bugs. On the other hand, if you've decided that the need to re-implement is justified for clearly defined reasons, by all means, File::Find is your first step.


      This is good advice overall!

      For the OP, I just wanted to add that there are "better" alternatives to system and backticks. In this particular case, if there are no variables being interpolated into the command string, meaning it's safe enough to call the shell, IPC::System::Simple's run would probably be easiest, since it's just a replacement for system with better error handling.

      use IPC::System::Simple qw/run/; run q{ find /path/to/files -name '*.csv' | cpio -pdm /target };

      Otherwise, e.g. if variables are being interpolated into the command, things can get more complicated - I wrote about the whole topic at length here.

Re: How to write the perl script for the following linux command?
by Corion (Pope) on Mar 14, 2017 at 11:41 UTC

    Can you please also describe in English what this command is supposed to do?

    Most likely, File::Find and File::Copy implement most of the functionality you will need.

      The above command will find the source location and fetches for (*.csv files) and store the same directory structure at destination.

        So, what Perl code did you write, for example using the modules I mentioned already, and how does it fail to do what you need?

Re: How to write the perl script for the following linux command?
by marto (Archbishop) on Mar 14, 2017 at 11:48 UTC
Re: How to write the perl script for the following linux command? ( File::Find::Rule Path::Tiny )
by beech (Vicar) on Mar 15, 2017 at 01:56 UTC


    See File::Find::Rule::Procedural and Path::Tiny

    Write something like

    use File::Find::Rule 'find'; my @files = find( -name => '*.csv', -in => $sourcedir ); for my $infile ( @files ){ my( $rel ) = path( $infile )->relative( $sourcedir ); my $outfile = path( $targetdir, $rel ); $outfile->touchpath; path($infile)->copy( $outfile ); }
why th efollowing error occurs in perl?
by finddata (Sexton) on Mar 15, 2017 at 06:46 UTC
    `find . -type f -exec cp {*.html} htmlfiles`;
    Error: find: missing argument to `-exec'

      Please stop creating new threads for the same problem. Stay in one thread, that makes it much easier for everyone willing to help.

      And by the way, this is not a perl error. Perl isn't even reporting it. See find.


      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1184533]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-06-19 05:19 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.