• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How to Copy / Move Files and Directories in Linux with “cp” and “mv” commands

by admin

Copy Files and Directories

The cp command will copy files and directories or copy multiple sources to a destination directory. The basic syntax of the cp command is:

# cp [options] source destination

If you have multiple files/directories to be coped to a destination directory, use the below command syntax.

# cp [options] source1 source2 [...] destination_directory

Common options used with the cp command, include:
-a – archive, never follow symbolic links, preserve links, copy directories recursively
-f – if an existing destination file cannot be opened, remove it and try again
-i – prompt before overwriting an existing file
-r – copy directories recursively

These examples show typical invocations of the cp command with descriptions of what they do.

Example 1

Copying a single file to a destination directory:

$ cp data.txt /var/tmp/

Example 2

Copying multiple files to a destination directory:

$ cp data.txt file.csv /var/tmp/

Example 3

Copying a directory (and it’s contents) recursively:

$ cp -r /etc/ /var/tmp/backup/

Moving Files and Directories

The mv command will move or rename files or directories, or can move multiple sources (files and directories) to a destination directory. The basic syntax of the mv command is:

# mv [options] source destination

To move multiple files/directories into a destination, use the below syntax.

# mv [options] source1 source2 [...] destination

Common options used with the mv command:
-f – do not prompt before overwriting
-i – prompt before overwrite
-u – move only when the source file is newer than the destination file or when the destination file is missing

Note: that if the destination exists, it will be overwritten unless the -i option is used.

If a file or directory is moved to a new name within the same directory, it is effectively renamed. For example, this would rename a file from oldname to newname.

$ mv -i oldname newname

Filed Under: Linux

Some more articles you might also be interested in …

  1. groupadd: command not found
  2. fprintd-verify: command not found
  3. cmp: Compare two files byte by byte
  4. How to enable NFS debug logging using rpcdebug
  5. add-apt-repository: command not found
  6. urxvt: command not found
  7. How to disable swap in Linux
  8. git cat-file: Provide content or type and size information for Git repository objects
  9. glab Command Examples
  10. CentOS / RHEL : How to Set up SFTP to Chroot Jail only for Specific Group

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright