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

The Geek Diary

CONCEPTS | BASICS | HOWTO

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • Linux Services
    • VCS
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
    • Data Guard
  • DevOps
    • Docker
    • Shell Scripting
  • Interview Questions
  • 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. How to disable NUMA on EFI Boot Loaders using GRUB2 (CentOS/RHEL 6)
  2. What are the mount options to improve ext4 filesystem performance in Linux
  3. How to Provide Credentials From a File While Mounting CIFS Share in Linux
  4. How to Reduce an LVM volume on Ubuntu
  5. TCP Wrapper (hosts.allow & hosts.deny) Command Options in Linux
  6. How to Switch Password Algorithm on CentOS/RHEL
  7. How to Setup a sudo Switch to Another User That Has no Password or ssh Key Set in Linux
  8. How To Identify User Deleting Files From A Given Directory in Linux
  9. Repairing filesystem issues at Boot in CentOS/RHEL 7 and 8
  10. How to Password Protect GRUB2 in Oracle Enterprise Linux 7

You May Also Like

Primary Sidebar

Recent Posts

  • MySQL: how to figure out which session holds which table level or global read locks
  • Recommended Configuration of the MySQL Performance Schema
  • MySQL: Identify what user and thread are holding on to a meta data lock that is preventing other queries from running
  • MySQL: How to kill a Long Running Query using max_execution_time
  • Archives
  • Contact Us
  • Copyright

© 2021 · The Geek Diary