• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All
  • About
Linux-Ubuntu | cp/scp -- copy files/directories
  1. Notes
  2. Examples
  3. Command Help (man cp)

  1. Notes
    cp source_file target_file
    
    cp source_file ... target_directory
    
    scp source_file {DOMAIN_NAME}\\{USER_NAME}@{HOST_NAME}:target_directory
    
    scp {DOMAIN_NAME}\\{USER_NAME}@{HOST_NAME}:source_file target_directory
  2. Examples
    • Copying file:

      • Create a new copy of file1 and rename it to file2 (see bellow the options -i and -n to check if the destination file already exists):
        $ cp file1 file2

      • Copy file1 from folder1 to folder2 (it's recommended to use a trailing forward slash to be explicit about the destination directory).
        $ cp folder1/file1 folder2/
        
        #Optionally you can also specify the file name to be used for the destination folder
        $ cp folder1/file1 folder2/file1

      • Copy file1 (renaming it to file2) from folder1 to folder2:
        $ cp folder1/file1 folder2/file2

      • Copy all files (excluding subdirectories and files starting with a period (.)) in folder1 to folder2:
        $ cp folder1/* folder2/
        It may complain that there are subdirectories in folder2 "cp: omitting directory ...".

      • Copy all files starting with a period (.) (excluding subdirectories) in folder1 to folder2:
        $ cp folder1/.* folder2/
        It may complain that there are subdirectories in folder2 "cp: omitting directory ...".

    • Copying directory:

      • Copy the directory folder1 in folder2:
        cp -R folder1 folder2/

      • Copy the content (files and subdirectories) of the directory folder1 to folder2:
        cp -R folder1/ folder2/

      • Copy the content (files and subdirectories excluding files starting with a period (.)) of the directory folder1 to folder2:
        cp -R folder1/* folder2/

    • Secure-copying file/directory:

      • Copy a remote file to a local directory:
        $ scp user1@my-remote-host.com:/folder1/folder2/index.php ~/

      • Copy a remote directory to a local directory:
        $ scp -r user1@my-remote-host.com:/folder1/folder2/ ~/

      • Copy a local file to a remote directory:
        $ scp ~/file1 user1@my-remote-host.com:/folder1/folder2/

      • Copy a local directory to a remote directory:
        $ scp -r ~/folder user1@my-remote-host.com:/folder1/folder2/
  3. Command Help (man cp)
    The following options can be used:
    -R
    |If source_file designates a directory, the directory will be copied with all it's files and sub-directories.
    |If the source_file ends in a /, the contents of the directory are copied rather than the directory itself.
    |Created directories have the same mode as the corresponding source directory, unmodified by the "umask" of the process.
    |cp will continue copying even if errors are detected.
    
    -n
    |Do not overwrite an existing file.
    |The -n option overrides any previous -f or -i options.
    
    -f
    |If the destination file cannot be opened, remove it and create a new file, without prompting for confirmation.
    |The target file is not unlinked before the copy.
    |Thus, any existing access rights will be retained.
    |The -f option overrides any previous -n option.
    
    -i
    |Cause cp to write a prompt to the standard error output before copying a file that would overwrite an existing file.
    |If the response from the standard input begins with the character `y' or `Y', the file copy is attempted.
    |The -i option overrides any previous -n option.
    
    -v
    |Cause cp to be verbose, showing files as they are copied.
© 2025  mtitek