• 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. “iscsiadm” Discovery TimeOut With Two Or More Network Interfaces in CentOS/RHEL
  2. ping: command not found
  3. CentOS / RHEL 5,6 : How to Change the timezone
  4. fakeroot Command Examples in Linux
  5. http-prompt Command Examples in Linux
  6. minicom: command not found
  7. How to determine file and free space fragmentation of OCFS2
  8. CentOS / RHEL : Resize (reduce) non-root EXT3/4 filesystem on non-LVM device (hard disk partition)
  9. auditd Command Examples in Linux
  10. CentOS / RHEL 7 : How to configure VLAN Tagging using nmcli

You May Also Like

Primary Sidebar

Recent Posts

  • aws ec2: CLI for AWS EC2 (Command Examples)
  • aws cur – Create, query, and delete AWS usage report definitions (Command Examples)
  • aws configure – Manage configuration for the AWS CLI (Command Examples)
  • aws cognito-idp: Manage Amazon Cognito user pool and its users and groups using the CLI

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright