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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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. CentOS / RHEL : How to get the date and time of executed command in the history command output
  2. Audit rules for monitoring Copy, move, delete and kill Commands In Linux
  3. How to Integrate CentOS/RHEL system into an AD Domain with LDAP/Kerberos/SSSD
  4. “imuxsock lost # messages from pid # due to rate-limiting” – rsyslog rate-limiting in Linux
  5. Can’t start X11 applications after “su” or “su -” to another user
  6. How to create and mount filesystems in Linux
  7. How to Manage Virtual Machines from the CentOS/RHEL 8 Web Console-Cockpit
  8. How to Create and Manage Storage Domains in RedHat Virtualization (RHV)
  9. How to use netstat command under Linux (Examples included)
  10. “Could not resolve proxy: https; Unknown error” – error with ‘yum update’

You May Also Like

Primary Sidebar

Recent Posts

  • grpck command – Remove corrupt or duplicate entries in the /etc/group and /etc/gshadow files.
  • xxd command – Expressed in hexadecimal form
  • sesearch: command not found
  • macof: command not found

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright