Skip to main content
Engineering LibreTexts

05-C.6.3: Working with Files and Directories - cp/mv Command

  • Page ID
    32188
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The cp Command

    cp stands for copy. This command is used to copy files or group of files or directories. It creates an exact copy of a file on a disk with different file name. cp command requires at least two filenames in its arguments.

    This video covers the use of copy and move commands.

    Syntax:

    cp [OPTION] Source Destination
    cp [OPTION] Source Directory
    cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
    

    Looking at the above options a bit of clarification is necessary.

    In the first option, the Destination can be a file or a directory. If the Destination does NOT exist, cp will assume the Destination is to be a file and will create a copy of the Source file. If the Destination file exists cp will overwrite the destination file unless the -i option (see below) is specified.

    In the second option things can get a bit confusing. If the command contains two directory names, cp copies all files of the source directory to the destination directory, creating any files or directories needed. This mode of operation requires an additional option, typically -R, to indicate the recursive copying of directories as in:

    cp -R Src_directory Dest_directory
    

    When copying one directory to another as in the above command, the behavior of cp depends on whether Dest_directory exists or not. If the Dest_directory does NOT exist, cp creates it and recursively copies the contents of Src_directory into Dest_directory. But if Dest_directory DOES exist the copy of Src_directory becomes sub-directory within Dest_directory.

    In the last option the cp command allows multiple source files to be copied to a single destination directory. The destination directory MUST exist as cp will not create it.

    Command Option:

    Options for cp and mv:

    Options Option Meaning
    -i, --interactive Prompt before overwrite (overrides a previous -n option).
    -n Do not overwrite an existing file (overrides a previous -ioption).
    -R, -r, --recursive Copy directories recursively.

    The mv Command

    mv stands for move, and it is used to move one or more files or directories from one place to another in Linux. It has two distinct functions:
    1) It renames a file or directory and 2) it moves a group of files to different directory.

    mv options are pretty much the same as the cp command, although there is not a recursive capability with the mv command

    No additional space is consumed on a disk during renaming. This command normally works silently, which means no prompt for confirmation. The source file or directory no longer exists.

    Syntax:

    mv [ OPTION ] source destination
    

    mv has a few nuances just like cp (If the source is a file and the destination does not exist, mv assumes the destination is a file, and will create the new file).

    • If the source is a directory and the destination does NOT exist, mv will create the destination directory.
    • If the source is a directory and the destination is a directory, then the source directory is moved as a subdirectory of destination.

    The options listed above for cp are also valid for mv. If you desire to see the full range of options for any command take a look at the man page for the command.

    Adapted from:
    "cp command in Linux with examples" by AKASH GUPTA 6, Geeks for Geeks is licensed under CC BY-SA 4.0
    "mv command in Linux with examples" by AKASH GUPTA 6, Geeks for Geeks is licensed under CC BY-SA 4.0


    05-C.6.3: Working with Files and Directories - cp/mv Command is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by LibreTexts.