#2 Copying files

beginner

Introduction

In this episode we will building on the basics we covered in episode 1 to look at how to copy files in the command line. A common task which is trivial in any GUI file explorer application typically bundled with most modern operating systems.

Outcomes

Copying a single file

To copy any files or directories you will need the cp command, short for "copy". This command takes 2 arguments the first being a path to the item you want to copy (the source) and the latter being another path where you want to copy to end up (the destination).

Example of copying a file named "file-v1.txt" with the copied version called "file-v2.txt"

cp file-v1.txt file-v2.txt

Example of copying a file to a child directory whilst renaming the file

cp my-file.txt ./child/my-copied-file.txt

Example of copying a file to a child directory

cp my-file.txt ./child/

Example of copying a file to a parent directory whilst renaming the file

cp my-file.txt ../my-copied-file.txt

Example of copying a file to a parent directory

cp my-file.txt ../

Copying multiple files with a regular expression

Copying multiple files is very similar to copying a single file. You use the same command cp and also with 2 arguments. However for the first argument you can provide a regular expression that matches a certain pattern. For example you may want to copy all the CSV files in one directory into another directory. A regular expression is a means to express a pattern that if expanded would list potentially many paths. However for brevity this will not be covered in this episode.

Example of copying all CSV files in the present directory to another directory

cp *.csv ../sibling/

Copying multiple files with a list

Copying multiple files without the need for a regular expression is also possible. Instead we must provide the command with all the paths we want to copy and the last argument will be the destination directory we want the files copied to.

Example of copying files "1.csv" and "2.csv" in the present directory to another directory

cp 1.csv 2.csv ../sibling/

Copying a directory and its contents

Another common scenario you will come across is wanting to copy a directory and all the containing files and directories within it. This again is very similar to the previous operations, except this time we need to a pass a flag to the command -r which is short for "resursive". This instruct the cp command to copy all the files containined within the directory.

Directory structure before our recursive copy command

.
├── overview.txt
└── project1
    ├── assets
    │   └── image.png
    └── file1.txt

Example command to recursively copy project1 directory as project2

cp -r project1/ project2

Directory structure after our recursive copy command

.
├── overview.txt
├── project1
│   ├── assets
│   │   └── image.png
│   └── file1.txt
└── project2
    ├── assets
    │   └── image.png
    └── file1.txt

Conclusion

You should now be able to perform some primative copy commands to duplicate files and directories on the command line. In the next few episodes we will cover how to move and delete files. This will mean typical operations you do in a GUI file explorer you should be able to do in the command line.