How to Copy a File or Folder using the Terminal on Linux Mint

Share
In this tutorial, we'll learn how to copy a file or folder using the terminal on Linux Mint using the cp command.

To create copy a file or folder, execute the following terminal command [how?]:

cp original-file.txt copy-of-file.txt

Where original-file.txt refers to the file or folder that you want to copy, and copy-of-file.txt refers to where the copy will be created. See [How to Refer to Files and Folders using the Terminal on Linux Mint] for how to write this filename argument.

Danger: cp will OVERWRITE the target file if it already exists. For example, cp new-file.txt important-file.txt will overwrite important-file.txt with the contents of new-file.txt. Notably, this action doesn't delete the file and replace it by a new file, but modifies it in place, meaning that additional hard links will be affected as well.

Tip: you can copy one or more files into a folder by having the last argument refer to a directory.

cp task-1.txt task-2.txt task-3.txt completed-tasks

Above, the files tasks-{1,2,3}.txt files will be copied to the completed-tasks folder. Similarly, you could use Bash's expansions and globbing like cp tasks-{1,2,3}.txt completed-tasks or cp tasks-*.txt completed-tasks to achieve the same result.

Questions and Answers

Does Copying a Directory Copy Its Contents?

Yes. All descendants of the directory will be copied, including contents of subdirectories.

What Happens if The Target of cp Already Exists?

If you cp a file to the filepath of an existing file, the file will be overwritten.

What Happens if The Target of cp is a Symlink?

If the target of cp is a symlink that points to a non-existing file you get the error "cp: not writing through dangling symlink 'target'." However, if the symlink points to an existing file, it overwrites the symlinked file.

Does Copying a File Copies the Creation Date?

The creation date isn't copied by default with cp, but it's possible to preserve the file's metadata (the inode's metadata) using the option --archive (-a).

Written by Noel Santos.

About the Author

I'm a self-taught Brazilian programmer graduated in IT from a FATEC. In a world of increasingly complex and essential computers, I decided to use my technical expertise in hardware, desktop applications, and web technologies to create an informative resource to make PC's easier to understand.

View Comments