Are you working on a bash script that needs to be executed without any human inputs? Do your scripts fail when the process of copying files encounters a problem?

If you answered “yes” to either of these questions then you’re in the right place.

In this post, we will take a quick look at the cp command in Linux, and examine how it can be used in different scenarios.

How to Copy Files in Linux CLI

Copying files is one of the simplest actions that you can perform on a computer. To copy files via the command line you can use the cp command, which stands for ‘copy’. The general syntax for the cp command is:

cp [OPTIONS] SOURCE(s)... DESTINATION

The SOURCE(s) can be one or more files or directories, and the DESTINATION can be a single file or directory. If you’ve passed more than two parameters to the command, then the last one is always considered the destination, and all other parameters before it are considered the source(s).

  • If the DESTINATION is a directory, the SOURCE files or directories are copied into it.
  • If the DESTINATION is a file, the SOURCE file is copied and renamed as the DESTINATION file.

Here is a simple example of how to copy a file named file.txt from the current directory to another directory named backup:

cp file.txt backup/

This will copy the file file.txt to the backup directory with the same name. If you want to copy the file with a different name, you can specify the new name after the backup directory:

cp file.txt backup/new_file.txt

This will copy the file file.txt to the backup directory as new_file.txt.

How to Overwrite Existing Files

By default, cp will overwrite any existing files in the destination without asking for confirmation. However, this behavior can be changed by using different flags or options with the cp command. Here are some of the common flags that can affect how cp handles overwriting:

  • -f or --force: This option will force cp to overwrite any existing files in the destination, even if they cannot be opened or removed. This option will also ignore any -n option that is used before it.

    For example, cp -f source.txt destination.txt will overwrite destination.txt with source.txt, regardless of any permissions or errors.
  • -i or --interactive: This option will make cp prompt the user before overwriting any existing files in the destination. You can choose to overwrite or skip the file by typing y or n and then pressing Enter.

    If you’re copying multiple files at once, you will be prompted for each file separately. If you don’t type anything and press Enter then the file will not be overwritten.

    This option will also override any -n option that is used before it. For example, cp -i source.txt destination.txt will ask the user if they want to overwrite destination.txt with source.txt, and proceed accordingly.
  • -n or --no-clobber: This option will prevent cp from overwriting any existing files in the destination. If the destination file already exists, cp will skip it and move on to the next file. This option will also override any -i option that is used before it.

    For example, cp -n source.txt destination.txt will not overwrite destination.txt with source.txt, if destination.txt already exists.
  • -u or --update: This option will make cp copy only when the source file is newer than the destination file, or when the destination file does not exist. This can be useful for updating or synchronizing files between different locations.

    For example, cp -u source.txt destination.txt will overwrite destination.txt with source.txt, only if source.txt is newer or destination.txt does not exist.

Note that these flags can be combined to achieve different effects. For example, cp -uf source.txt destination.txt will force cp to overwrite destination.txt with source.txt, only if source.txt is newer or destination.txt does not exist.

Changing the Default Behavior

On some computers, the default behavior of the cp command can be affected by an alias. An alias is a way of creating a shortcut or a new name for a command, with some predefined options.

For example, some Linux distributions come with the following alias pre-defined in their code:

alias cp='cp -i'

This means that whenever the user types cp, the shell will actually run cp -i, which will prompt the user before overwriting any files.

Checking For An Existing Alias

If you’re not sure whether your computer has a predefined alias, then you can run the following command to list all aliases, and then look to see whether the cp is present in the output:

alias

Removing An Alias

To remove the alias for cp, you can run the command unalias cp, which will restore the default behavior of cp.

Removing alias of copy command

In the above example, we can see that an alias was defined for the cp command. After executing the unalias command, the alias entry was removed from the list.

Temporarily Bypass An Alias

If you don’t want to permanently remove the alias, you have an option to use a backslash before the cp command to bypass the alias and run the original command. For example, \cp source.txt destination.txt will overwrite destination.txt with source.txt, without prompting the user.

bypassing alias of copy commands

In the above example, we can see that the user was prompted for input when executing the copy operation, but not when the command was prefixed with the \ character.

Automatically Accepting All Prompts

In Linux there are multiple ways to do the same thing using different methods. If you don’t want to use the in-built -f option to overwrite files then you can use the copy command in conjunction with the yes command to automatically respond with y to all prompts.

yes | cp -v source destination

In the above example, we can see that the copy command prompted us for input before replacing each file, but the yes command responded ‘yes’ to each and every single one of them nearly instantaneously.

Wrapping Up

In this article, we have discussed how to copy files and directories in Linux via the command line. We have also explained different options and features of the copy command that are helpful in day-to-day usage.

If you’re learning Linux to better manage your servers, we wish you all the best! However, you don’t need to be a Linux expert to host websites on the internet. RunCloud makes it extremely easy to deploy and manage websites using a user-friendly dashboard, and provides complete freedom to tinker under the hood.

Whether you’re a beginner or a seasoned professional with decades of experience, sooner or later everyone makes a configuration change that completely renders websites useless. RunCloud has idiot-proofed many complex operations such as updating server configurations, installing SSL certificates, deleting applications, etc. – all of which makes it difficult for novice users to crash their website, and provides an extra set of guide-rails to advanced users.

RunCloud is a versatile and reliable tool that can help you manage your server on your own, without the hassle and cost of hiring a system administrator. Don’t miss this opportunity to take your server management to the next level. Sign up for RunCloud today and enjoy the benefits of a cloud-based server management platform that is fast, secure, and easy to use.