Sponsored Content
Full Discussion: Perl copy vs system cp
Top Forums Shell Programming and Scripting Perl copy vs system cp Post 302383877 by slchin on Friday 1st of January 2010 12:22:14 PM
Old 01-01-2010
Perl copy vs system cp

What are the pros & cons, if any, between using Perl's copy module vs OS's system cp, for copying a file to another directory? Or are they exactly the same?

1) Perl's File::Copy module, as in
Code:
  
copy ($filename, $dest_path) or die "ERROR: Cannot copy\n";

2)
Code:
if (system ("cp $filename, $dest_path") != 0) {
  <do something>;
} else {
  print "ERROR: Cannot copy\n";
}

PS. Sorry if this is already discussed in other/older threads.

Thanks.

Last edited by Scott; 01-01-2010 at 01:32 PM.. Reason: Please use code tags
 

10 More Discussions You Might Find Interesting

1. Filesystems, Disks and Memory

on solaris best utility to copy a file system?

Hi, - on a solaris box what is the best utility to copy a whole file system from one place to other? - the aim of the operation is to place the contents of the whole file system to other file system within the discs cp ? - cpio? - tar? - what about file systemes containing symbolic links ?... (1 Reply)
Discussion started by: JAKEZ
1 Replies

2. Solaris

System copy with flarcreate

Hi, i'm trying to make a system copy from one server to another (solaris 9), they have the same configuration. I've tried to create a flash archive using the flarcreate but the flash file size is 5.06 approx. and the method the flarcreate is using is CPIO and he cannot handle sizes over 4 GB. Is... (2 Replies)
Discussion started by: pasalagua
2 Replies

3. Shell Programming and Scripting

copy directory structure to a system on the network

I am trying to write a script which has to copy the directory structure from my system to another system on the network. But I dont want the files to be copied. I think I have to start with copying all subdirectories names in a directory to a system on the network. Here's the case: Source... (1 Reply)
Discussion started by: firefox211
1 Replies

4. Programming

Copy a file using UNIX-system calls

#include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> int main( int argc,char *argv ) { char buf; int sourcefile,destfile,n; if(argc!=3) { write(STDOUT_FILENO,"prgm1 <sourcefile> <destination file>\n",50); ... (6 Replies)
Discussion started by: c_d
6 Replies

5. UNIX for Dummies Questions & Answers

How to copy my system hdd usb stick from 4GB to 8GB ?

Hi, my router is my Linux embedded device. I have system installed on HDD 4GB usb stick, part1 swap, part2 /opt , part3 data. I need to copy my system to new HDD 8GB usb stick. What is a way for 4GB > 4GB HDD and what for 4GB > 8GB As I remeber, I can copy image of my 4GB HDD usb stick... (7 Replies)
Discussion started by: jack2
7 Replies

6. Shell Programming and Scripting

File Copy Perl

Hi Could you please help me on this I have folder called D:\Data in which we have 20 files coming daily and needed all 20 files to copy into D:\Target ,as i am new to PERL, i did nt know how to use this I googled sme of the code and done the copy script,it is throwing error like we do... (1 Reply)
Discussion started by: vaas
1 Replies

7. AIX

Copy huge files system

Dear Guy’s By using dd command or any strong command, I’d like to copy huge data from file system to another file system Sours File system: /sfsapp File system has 250 GB of data Target File system: /tgtapp I’d like to copy all these files and directories from /sfsapp to /tgtapp as... (28 Replies)
Discussion started by: Mr.AIX
28 Replies

8. UNIX for Advanced & Expert Users

Grub - how to boot a copy of Linux (full system backup)

Hi All, I have successfully backup & restore (using tar) one of my Debian Lenny Servers. On the restore server (standby machine), everytime i have to erase the disk & extract the tar backup. I want to extract the tar on the running restore server on a directory for e.g /systembackup-01,... (11 Replies)
Discussion started by: coolatt
11 Replies

9. Shell Programming and Scripting

Copy files with pattern from ext4 to cifs file system

Hi I have a shell script to copy a pattern of files from Linux to Windows Filesystem. When i execute the below command cp -av TOUT_05-02-13* Windows/Folder `TOUT_05-02-13-19:02:37.tar.gz' -> `Windows/Folder/SYSOUT_05-02-13-19:02:37.tar.gz' cp: cannot create regular file... (5 Replies)
Discussion started by: rakeshkumar
5 Replies

10. Solaris

Basic question regarding root file system copy to another disk

Hello, I am creating a new disk using the following command: dd if=/dev/zero of=/export/home/ramdisk/0 bs=512 count=4096k after creating the disk, i tool a ufsdump of a solaris 10 filesytem (disk size 512MB) ufsdump -cvf /export/home/ufsdump/sol_orig /and then restored the dump files onto... (10 Replies)
Discussion started by: Zam_1234
10 Replies
File::GlobMapper(3pm)					 Perl Programmers Reference Guide				     File::GlobMapper(3pm)

NAME
File::GlobMapper - Extend File Glob to Allow Input and Output Files SYNOPSIS
use File::GlobMapper qw( globmap ); my $aref = globmap $input => $output or die $File::GlobMapper::Error ; my $gm = new File::GlobMapper $input => $output or die $File::GlobMapper::Error ; DESCRIPTION
This module needs Perl5.005 or better. This module takes the existing "File::Glob" module as a starting point and extends it to allow new filenames to be derived from the files matched by "File::Glob". This can be useful when carrying out batch operations on multiple files that have both an input filename and output filename and the output file can be derived from the input filename. Examples of operations where this can be useful include, file renaming, file copying and file compression. Behind The Scenes To help explain what "File::GlobMapper" does, consider what code you would write if you wanted to rename all files in the current directory that ended in ".tar.gz" to ".tgz". So say these files are in the current directory alpha.tar.gz beta.tar.gz gamma.tar.gz and they need renamed to this alpha.tgz beta.tgz gamma.tgz Below is a possible implementation of a script to carry out the rename (error cases have been omitted) foreach my $old ( glob "*.tar.gz" ) { my $new = $old; $new =~ s#(.*).tar.gz$#$1.tgz# ; rename $old => $new or die "Cannot rename '$old' to '$new': $! ; } Notice that a file glob pattern "*.tar.gz" was used to match the ".tar.gz" files, then a fairly similar regular expression was used in the substitute to allow the new filename to be created. Given that the file glob is just a cut-down regular expression and that it has already done a lot of the hard work in pattern matching the filenames, wouldn't it be handy to be able to use the patterns in the fileglob to drive the new filename? Well, that's exactly what "File::GlobMapper" does. Here is same snippet of code rewritten using "globmap" for my $pair (globmap '<*.tar.gz>' => '<#1.tgz>' ) { my ($from, $to) = @$pair; rename $from => $to or die "Cannot rename '$old' to '$new': $! ; } So how does it work? Behind the scenes the "globmap" function does a combination of a file glob to match existing filenames followed by a substitute to create the new filenames. Notice how both parameters to "globmap" are strings that are delimited by <>. This is done to make them look more like file globs - it is just syntactic sugar, but it can be handy when you want the strings to be visually distinctive. The enclosing <> are optional, so you don't have to use them - in fact the first thing globmap will do is remove these delimiters if they are present. The first parameter to "globmap", "*.tar.gz", is an Input File Glob. Once the enclosing "< ... >" is removed, this is passed (more or less) unchanged to "File::Glob" to carry out a file match. Next the fileglob "*.tar.gz" is transformed behind the scenes into a full Perl regular expression, with the additional step of wrapping each transformed wildcard metacharacter sequence in parenthesis. In this case the input fileglob "*.tar.gz" will be transformed into this Perl regular expression ([^/]*).tar.gz Wrapping with parenthesis allows the wildcard parts of the Input File Glob to be referenced by the second parameter to "globmap", "#1.tgz", the Output File Glob. This parameter operates just like the replacement part of a substitute command. The difference is that the "#1" syntax is used to reference sub-patterns matched in the input fileglob, rather than the $1 syntax that is used with perl regular expressions. In this case "#1" is used to refer to the text matched by the "*" in the Input File Glob. This makes it easier to use this module where the parameters to "globmap" are typed at the command line. The final step involves passing each filename matched by the "*.tar.gz" file glob through the derived Perl regular expression in turn and expanding the output fileglob using it. The end result of all this is a list of pairs of filenames. By default that is what is returned by "globmap". In this example the data structure returned will look like this ( ['alpha.tar.gz' => 'alpha.tgz'], ['beta.tar.gz' => 'beta.tgz' ], ['gamma.tar.gz' => 'gamma.tgz'] ) Each pair is an array reference with two elements - namely the from filename, that "File::Glob" has matched, and a to filename that is derived from the from filename. Limitations "File::GlobMapper" has been kept simple deliberately, so it isn't intended to solve all filename mapping operations. Under the hood "File::Glob" (or for older versions of Perl, "File::BSDGlob") is used to match the files, so you will never have the flexibility of full Perl regular expression. Input File Glob The syntax for an Input FileGlob is identical to "File::Glob", except for the following 1. No nested {} 2. Whitespace does not delimit fileglobs. 3. The use of parenthesis can be used to capture parts of the input filename. 4. If an Input glob matches the same file more than once, only the first will be used. The syntax ~ ~user . Matches a literal '.'. Equivalent to the Perl regular expression . * Matches zero or more characters, except '/'. Equivalent to the Perl regular expression [^/]* ? Matches zero or one character, except '/'. Equivalent to the Perl regular expression [^/]? Backslash is used, as usual, to escape the next character. [] Character class. {,} Alternation () Capturing parenthesis that work just like perl Any other character it taken literally. Output File Glob The Output File Glob is a normal string, with 2 glob-like features. The first is the '*' metacharacter. This will be replaced by the complete filename matched by the input file glob. So *.c *.Z The second is Output FileGlobs take the "*" The "*" character will be replaced with the complete input filename. #1 Patterns of the form /#d/ will be replaced with the Returned Data EXAMPLES
A Rename script Below is a simple "rename" script that uses "globmap" to determine the source and destination filenames. use File::GlobMapper qw(globmap) ; use File::Copy; die "rename: Usage rename 'from' 'to' " unless @ARGV == 2 ; my $fromGlob = shift @ARGV; my $toGlob = shift @ARGV; my $pairs = globmap($fromGlob, $toGlob) or die $File::GlobMapper::Error; for my $pair (@$pairs) { my ($from, $to) = @$pair; move $from => $to ; } Here is an example that renames all c files to cpp. $ rename '*.c' '#1.cpp' A few example globmaps Below are a few examples of globmaps To copy all your .c file to a backup directory '</my/home/*.c>' '</my/backup/#1.c>' If you want to compress all '</my/home/*.[ch]>' '<*.gz>' To uncompress '</my/home/*.[ch].gz>' '</my/home/#1.#2>' SEE ALSO
File::Glob AUTHOR
The File::GlobMapper module was written by Paul Marquess, pmqs@cpan.org. COPYRIGHT AND LICENSE
Copyright (c) 2005 Paul Marquess. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.16.2 2012-10-11 File::GlobMapper(3pm)
All times are GMT -4. The time now is 11:31 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy