{
    "mode": "perldoc",
    "parameter": "File::Copy",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/File%3A%3ACopy/json",
    "generated": "2026-06-10T11:51:58Z",
    "synopsis": "use File::Copy;\ncopy(\"sourcefile\",\"destinationfile\") or die \"Copy failed: $!\";\ncopy(\"Copy.pm\",\\*STDOUT);\nmove(\"/dev1/sourcefile\",\"/dev2/destinationfile\");\nuse File::Copy \"cp\";\n$n = FileHandle->new(\"/a/file\",\"r\");\ncp($n,\"x\");",
    "sections": {
        "NAME": {
            "content": "File::Copy - Copy files or filehandles\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use File::Copy;\n\ncopy(\"sourcefile\",\"destinationfile\") or die \"Copy failed: $!\";\ncopy(\"Copy.pm\",\\*STDOUT);\nmove(\"/dev1/sourcefile\",\"/dev2/destinationfile\");\n\nuse File::Copy \"cp\";\n\n$n = FileHandle->new(\"/a/file\",\"r\");\ncp($n,\"x\");\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The File::Copy module provides two basic functions, \"copy\" and \"move\", which are useful for\ngetting the contents of a file from one place to another.\n\ncopy\nThe \"copy\" function takes two parameters: a file to copy from and a file to copy to. Either\nargument may be a string, a FileHandle reference or a FileHandle glob. Obviously, if the\nfirst argument is a filehandle of some sort, it will be read from, and if it is a file\n*name* it will be opened for reading. Likewise, the second argument will be written to. If\nthe second argument does not exist but the parent directory does exist, then it will be\ncreated. Trying to copy a file into a non-existent directory is an error. Trying to copy a\nfile on top of itself is also an error. \"copy\" will not overwrite read-only files.\n\nIf the destination (second argument) already exists and is a directory, and the source\n(first argument) is not a filehandle, then the source file will be copied into the directory\nspecified by the destination, using the same base name as the source file. It's a failure to\nhave a filehandle as the source when the destination is a directory.\n\nNote that passing in files as handles instead of names may lead to loss of information on\nsome operating systems; it is recommended that you use file names whenever possible. Files\nare opened in binary mode where applicable. To get a consistent behaviour when copying from\na filehandle to a file, use \"binmode\" on the filehandle.\n\nAn optional third parameter can be used to specify the buffer size used for copying. This is\nthe number of bytes from the first file, that will be held in memory at any given time,\nbefore being written to the second file. The default buffer size depends upon the file, but\nwill generally be the whole file (up to 2MB), or 1k for filehandles that do not reference\nfiles (eg. sockets).\n\nYou may use the syntax \"use File::Copy \"cp\"\" to get at the \"cp\" alias for this function. The\nsyntax is *exactly* the same. The behavior is nearly the same as well: as of version 2.15,\n\"cp\" will preserve the source file's permission bits like the shell utility cp(1) would do,\nwhile \"copy\" uses the default permissions for the target file (which may depend on the\nprocess' \"umask\", file ownership, inherited ACLs, etc.). If an error occurs in setting\npermissions, \"cp\" will return 0, regardless of whether the file was successfully copied.\n\nmove\nThe \"move\" function also takes two parameters: the current name and the intended name of the\nfile to be moved. If the destination already exists and is a directory, and the source is\nnot a directory, then the source file will be renamed into the directory specified by the\ndestination.\n\nIf possible, move() will simply rename the file. Otherwise, it copies the file to the new\nlocation and deletes the original. If an error occurs during this copy-and-delete process,\nyou may be left with a (possibly partial) copy of the file under the destination name.\n\nYou may use the \"mv\" alias for this function in the same way that you may use the \"cp\" alias\nfor \"copy\".\n\nsyscopy\nFile::Copy also provides the \"syscopy\" routine, which copies the file specified in the first\nparameter to the file specified in the second parameter, preserving OS-specific attributes\nand file structure. For Unix systems, this is equivalent to the simple \"copy\" routine, which\ndoesn't preserve OS-specific attributes. For VMS systems, this calls the \"rmscopy\" routine\n(see below). For OS/2 systems, this calls the \"syscopy\" XSUB directly. For Win32 systems,\nthis calls \"Win32::CopyFile\".\n\nSpecial behaviour if \"syscopy\" is defined (OS/2, VMS and Win32):\n\nIf both arguments to \"copy\" are not file handles, then \"copy\" will perform a \"system copy\"\nof the input file to a new output file, in order to preserve file attributes, indexed file\nstructure, *etc.* The buffer size parameter is ignored. If either argument to \"copy\" is a\nhandle to an opened file, then data is copied using Perl operators, and no effort is made to\npreserve file attributes or record structure.\n\nThe system copy routine may also be called directly under VMS and OS/2 as\n\"File::Copy::syscopy\" (or under VMS as \"File::Copy::rmscopy\", which is the routine that does\nthe actual work for syscopy).\n",
            "subsections": [
                {
                    "name": "rmscopy",
                    "content": "The first and second arguments may be strings, typeglobs, typeglob references, or objects\ninheriting from IO::Handle; they are used in all cases to obtain the *filespec* of the input\nand output files, respectively. The name and type of the input file are used as defaults for\nthe output file, if necessary.\n\nA new version of the output file is always created, which inherits the structure and RMS\nattributes of the input file, except for owner and protections (and possibly timestamps; see\nbelow). All data from the input file is copied to the output file; if either of the first\ntwo parameters to \"rmscopy\" is a file handle, its position is unchanged. (Note that this\nmeans a file handle pointing to the output file will be associated with an old version of\nthat file after \"rmscopy\" returns, not the newly created version.)\n\nThe third parameter is an integer flag, which tells \"rmscopy\" how to handle timestamps. If\nit is < 0, none of the input file's timestamps are propagated to the output file. If it is >\n0, then it is interpreted as a bitmask: if bit 0 (the LSB) is set, then timestamps other\nthan the revision date are propagated; if bit 1 is set, the revision date is propagated. If\nthe third parameter to \"rmscopy\" is 0, then it behaves much like the DCL COPY command: if\nthe name or type of the output file was explicitly specified, then no timestamps are\npropagated, but if they were taken implicitly from the input filespec, then all timestamps\nother than the revision date are propagated. If this parameter is not supplied, it defaults\nto 0.\n\n\"rmscopy\" is VMS specific and cannot be exported; it must be referenced by its full name,\ne.g.:\n\nFile::Copy::rmscopy($from, $to) or die $!;\n\nLike \"copy\", \"rmscopy\" returns 1 on success. If an error occurs, it sets $!, deletes the\noutput file, and returns 0.\n"
                }
            ]
        },
        "RETURN": {
            "content": "All functions return 1 on success, 0 on failure. $! will be set if an error was encountered.\n",
            "subsections": []
        },
        "NOTES": {
            "content": "Before calling copy() or move() on a filehandle, the caller should close or flush() the file to\navoid writes being lost. Note that this is the case even for move(), because it may actually\ncopy the file, depending on the OS-specific implementation, and the underlying filesystem(s).\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "File::Copy was written by Aaron Sherman *<ajs@ajs.com>* in 1995, and updated by Charles Bailey\n*<bailey@newman.upenn.edu>* in 1996.\n",
            "subsections": []
        }
    },
    "summary": "File::Copy - Copy files or filehandles",
    "flags": [],
    "examples": [],
    "see_also": []
}