Unix Copy Command Examples [ cp command ]

The cp (copy) command is used to copy files and directories in Unix-like systems. Here are some common examples of using the cp command:

  1. Copy a single file:
    cp file1.txt file2.txt

    This will copy the file file1.txt to a new file named file2.txt.

  2. Copy multiple files:
    cp file1.txt file2.txt file3.txt /destination/directory

    This will copy file1.txt, file2.txt, and file3.txt to the directory /destination/directory.

  3. Copy a directory and its contents:
    cp -r source_directory /destination/directory

    The -r option tells cp to copy the directory and its contents recursively. This will copy the entire source_directory and all of its subdirectories and files to the directory /destination/directory.

  4. Preserve file permissions when copying:
    cp -p file1.txt file2.txt

    The -p option tells cp to preserve the original file’s permissions, timestamps, and other metadata.

  5. Overwrite an existing file:
    cp -f file1.txt file2.txt

    The -f option tells cp to force the overwrite of an existing file. Without this option, cp will prompt you for confirmation before overwriting an existing file.

These are just a few examples of using the cp command. The cp command has many other options and uses, and you can find more information by running man cp in a terminal window.

Leave a Comment