Terminus
Copy Files In Linux

Copy Files In Linux

The short answer

In Unix-like operating systems such as Linux and macOS, to copy a file into a specific directory, you can use the [.inline-code]cp[.inline-code] command as follows:

$ cp <source> <destination>

Where:

  • [.inline-code]source[.inline-code] is the path to the file you want to copy.
  • [.inline-code]destination[.inline-code] is the path to the directory you want to copy the file into.

For example, this command will copy the local file named [.inline-code]index.js[.inline-code] into the local directory named [.inline-code]test[.inline-code]:

$ cp index.js test

[#easily-recall-syntax-with-ai]Easily retrieve this command using the Warp’s AI Command Suggestions [#easily-recall-syntax-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]copy file[.inline-code] into the AI Command Suggestions will prompt a [.inline-code]cp[.inline-code] command that can then be quickly inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#copy-into-a-root-directory] Copying files into a root directory [#copy-into-a-root-directory]

To copy files into a directory located outside of your home directory, you will need to gain elevated privileges (i.e., root access) using the [.inline-code]sudo[.inline-code] command (short for “superuser do") as follows:

 
$ sudo cp <source> <destination>

Note that when executing this command, you will be prompted to enter your password.

[#copy-multiple-files] Copying multiple files at once [#copy-multiple-files]

To copy one or more files at once into another directory, you can use the [.inline-code]cp[.inline-code] command with the following syntax:

$ cp <source ...> <destination>

Where:

  • [.inline-code]source ...[.inline-code] is a list of paths to the files you want to copy.
  • [.inline-code]destination[.inline-code] is the path to the directory you want to copy the files into.

For example, this command will copy the files named [.inline-code]index.js[.inline-code], [.inline-code]package.json[.inline-code], and [.inline-code]package-lock.json[.inline-code] into the directory named [.inline-code]website[.inline-code] located in the parent directory:

$ cp index.js package.json package-lock.json ../website

Note that you can also use the [.inline-code]-v[.inline-code] flag (short for *verbose*) to display the list of files that were successfully copied into the destination directory as follows:

$ cp -v <source ...> <destination>

[#copy-files-based-on-patterns] Copying multiple files based on patterns [#copy-files-based-on-patterns]

Additionally, you can use wildcards characters (e.g., [.inline-code]*[.inline-code], [.inline-code]?[.inline-code]) to copy multiple files based on patterns.

For example, this command will copy all the files with a [.inline-code].csv[.inline-code] file extension located in the [.inline-code]~/Downloads[.inline-code] directory into the [.inline-code]~/data[.inline-code] directory:

$ cp ~/Download/*.csv ~/data

You can learn more about copying files recursively by reading our other article on how to copy a directory in Linux.

Copying and renaming a file

To copy a file into a specific directory under a different name, you can use the [.inline-code]cp[.inline-code] command as follows:

$ cp <source> <destination>/<filename>

Where:

  • [.inline-code]<filename>[.inline-code] is the new name of the file you want to copy

For example, the following command will copy the file called [.inline-code]auth.dev.py[.inline-code] into the directory called [.inline-code]authentication[.inline-code]and rename it to [.inline-code]auth.py[.inline-code]:

$ cp auth.dev.py authentication/auth.py

[#prevent-file-overwriting] Preventing the overwriting of existing files [#prevent-file-overwriting]

By default, the [.inline-code]cp[.inline-code] command will overwrite the files with the same name as the source files located in the destination directory.

[#copy-files-in-interactive-mode] Copying files in interactive mode [#copy-files-in-interactive-mode]

To be prompted for confirmation every time a file overlaps with an existing one, you can use the [.inline-code]cp[.inline-code] command with the [.inline-code]-i[.inline-code] flag (short for [.inline-code]--interactive[.inline-code]) as follows:

$ cp -i <source> <destination>

When prompted with this question, you can either type [.inline-code]y[.inline-code] (short for yes) to confirm the overwriting of the file, or [.inline-code]n[.inline-code] (short for no) to skip it, and press [.inline-code]ENTER[.inline-code] to confirm your choice:

cp: overwrite 'file'?

Note that, by default, pressing [.inline-code]ENTER[.inline-code] directly will be considered as a no for safety reasons.

[#silently-skip-overlapping-files] Silently skipping overlapping files [#silently-skip-overlapping-files]

To silently skip the copy of overlapping files without being prompted, you can use the [.inline-code]cp[.inline-code] command with the [.inline-code]-n[.inline-code] flag (short for [.inline-code]--no-clobber[.inline-code]) as follows:

$ cp -n <source> <destination>

[#preserve-file-attributes] Preserving attributes while copying files [#preserve-file-attributes]

By default, the [.inline-code]cp[.inline-code] command doesn't preserve file attributes such as permissions or timestamps.

To preserve the file permissions, ownership, and timestamps, you can use the [.inline-code]cp[.inline-code] command with the [.inline-code]-p[.inline-code] flag as follows:

$ cp -p <source> <destination>

Alternatively, you can selectively preserve specific attributes using the [.inline-code]--preserve[.inline-code] flag as follows:

$ cp --preserve=<attributes> <source> <destination>

Where:

  • [.inline-code]attributes[.inline-code] is a comma-separated list of attributes.

For example, this command will copy the [.inline-code]script.sh[.inline-code] file into the [.inline-code]scripts[.inline-code] directory while preserving the permissions and ownership:

$ cp --preserve=mode,ownership script.sh scripts

[#copy-a-file-to-multiple-directories] Copying a file to multiple directories at once [#copy-a-file-to-multiple-directories]

To copy the same file into multiple directories at once, you can use a Bash script as follows:

#!/bin/bash

# Define the file to copy
source_file="<source_file>"

# List of destination directories
destination_directories=("<dir1>" ... "<dirN>")

# Loop through each destination directory
for dir in "${destination_directories[@]}"
do
 # Copy the file to the current destination directory
  cp "$source_file" "$dir"
done

Where:

  • [.inline-code]"<source_file>"[.inline-code] is the path to the file you want to copy (e.g., [.inline-code]"package.json"[.inline-code]).
  • [.inline-code]"<dir1>" ... "<dirN>"[.inline-code] is the list of paths to the directories you want to copy the file into (e.g., [.inline-code]"~/projects/app" "~/projects/website"[.inline-code]).

You can then give execution permission to the script using the [.inline-code]chmod[.inline-code] command as follows:

$ chmod +x copy_to_multiple_dir.sh

Finally, you can run the script using the following command:

$ ./copy_to_multiple_dir.sh

You can learn more about setting file permissions by reading our other article on how to use the chmod command in Linux.

[#copy-a-file-to-the-clipboard] Copying the content of a file to the clipboard [#copy-a-file-to-the-clipboard]

To copy the content of a file to the clipboard, you can use the [.inline-code]xclip[.inline-code] command as follows:

$ xclip -selection clipboard < <source>

Where:

  • [.inline-code]-selection clipboard[.inline-code] is used to specify that the copied content should be stored in the clipboard.

For example, this command will copy the content of the file named [.inline-code]usernames.txt[.inline-code] into the clipboard:

$ xclip -selection clipboard < data/users/usernames.txt

[#copy-a-file-from-and-to-a-usb-drive] Copying files from and to a USB drive [#copy-a-file-from-and-to-a-usb-drive]

To copy files from or to a USB drive, you will have to first mount the drive (if not automatically done) to an existing directory using the [.inline-code]mount[.inline-code] command:

$ sudo mount /dev/<drive> /mnt/<mount>

Where:

  • [.inline-code]drive[.inline-code] is the name of the USB drive, usually located in the [.inline-code]/dev[.inline-code] directory.
  • [.inline-code]mount[.inline-code] is the name of the directory the drive will be mounted to, usually located in the [.inline-code]/mnt[.inline-code] or [.inline-code]/media[.inline-code] directory.

Then copy the desired file(s) from the drive into the file system of the host (or vice versa):

$ sudo cp /mnt/<mountpoint>/<source> <destination>

And finally, unmount the USB drive using the [.inline-code]umount[.inline-code] command:

$ sudo umount /mnt/<mountpoint>

For example, these commands will mount the USB drive named [.inline-code]sdb1[.inline-code] to the local directory named [.inline-code]usb[.inline-code], copy the files located in the [.inline-code]scripts[.inline-code] directory, and unmount the drive:

$ sudo mount /dev/sdb1 /mnt/usb
$ sudo cp /mnt/usb/scripts/* ~/scripts/.
$ sudo umount /dev/sdb1

[#copy-files-over-ssh] Copying files over the SSH protocol [#copy-files-over-ssh]

To copy a file over SSH from your local machine to a remote server, you can use the [.inline-code]scp[.inline-code] command as follows:

$ scp <source> <ssh_username>@<ssh_server_ip>:<destination>

Where:

  • [.inline-code]ssh_username[.inline-code] is the username used to authenticate to the remote server.
  • [.inline-code]ssh_server_ip[.inline-code] is the IP address of the remote server

You can learn more about copying files over SSH by reading our other article on [how to copy a file from remote to local using scp] (https://www.warp.dev/terminus/scp-from-remote-to-local).

[#copy-files-with-symbolic-links] Copying a file with a symlink [#copy-files-with-symbolic-links]

To copy a file with symlink, there are few options you can use to treat them.

To keep their properties as symlink, you would use the following options:

  • [.inline-code]-P or --preserve=links[.inline-code] which preserves the symbolic link by copying them as link. This further maintains the properties of the symbolic link

To dereference symbolic links and copy the files they point to, you can use the option:

  • [.inline-code]-L or --dereference[.inline-code] which instructs the [.inline-code]cp[.inline-code] command to follow the symbolic link and copy the actual files they reference.