Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux cp Command: Copy Files And Directories

The cp command in Linux (short for "copy") is a utility used to copy files and directories. In this tutorial, we will discuss how to use the cp command effectively, along with various options and examples.

  1. Basic usage of the cp command:

    To copy a file, use the cp command followed by the source file and the destination file or directory:

    cp source.txt destination.txt
    

    This command will create a copy of source.txt and name it destination.txt. If destination.txt already exists, its contents will be overwritten. If the destination is a directory, the file will be copied into that directory with the same name.

  2. Copying directories:

    To copy a directory, use the -R or --recursive option followed by the source directory and the destination directory:

    cp -R source_directory destination_directory
    

    This command will create a copy of source_directory and all its contents (including subdirectories) inside destination_directory.

  3. Preserving file attributes:

    When copying files or directories, you might want to preserve the original file attributes such as timestamps, permissions, and ownership. To do so, use the -p or --preserve option:

    cp -p source.txt destination.txt
    
    cp -Rp source_directory destination_directory
    

    These commands will preserve the attributes when copying files or directories.

  4. Prevent overwriting existing files:

    To avoid overwriting existing files, use the -n or --no-clobber option:

    cp -n source.txt destination.txt
    

    This command will only copy the file if destination.txt does not already exist.

  5. Prompt before overwriting files:

    If you want to be prompted before overwriting a file, use the -i or --interactive option:

    cp -i source.txt destination.txt
    

    This command will ask for confirmation before overwriting destination.txt.

  6. Creating symbolic links and hard links:

    To create a symbolic link instead of copying the file, use the -s or --symbolic-link option:

    cp -s source.txt symlink.txt
    

    This command will create a symbolic link symlink.txt pointing to source.txt.

    To create a hard link instead of copying the file, use the -l or --link option:

    cp -l source.txt hardlink.txt
    

    This command will create a hard link hardlink.txt pointing to the same inode as source.txt.

By following this tutorial, you should now have a good understanding of how to use the cp command in Linux to copy files and directories. The cp command is an essential tool for managing files and allows you to efficiently duplicate, preserve, and organize your data.

  1. Copying files in Linux with cp:

    • To copy a file, use the basic syntax:
      cp source_file destination
      
  2. Copying directories and subdirectories with cp:

    • For copying directories and their contents, use the -r (or -R) option:
      cp -r source_directory destination
      
  3. Preserving file attributes during copy with cp:

    • To preserve attributes like ownership and timestamps, use the -a option:
      cp -a source_file destination
      
  4. Recursive copy with cp in Linux:

    • For recursively copying directories and their contents, use the -r option:
      cp -r source_directory destination
      
  5. Overwriting files with cp command:

    • To overwrite existing files without confirmation, use the -f option:
      cp -f source_file destination
      
  6. Copying files with specific permissions in cp:

    • To preserve specific permissions during copy, use the --preserve option:
      cp --preserve=mode,ownership source_file destination
      
  7. Copying files to a different directory in Linux:

    • Specify the destination directory to copy files to a different location:
      cp source_file /path/to/destination_directory/
      
  8. Using wildcards with cp for bulk file copy:

    • Wildcards (*) can be used to copy multiple files matching a pattern:
      cp *.txt destination_directory/