In the world of Linux and Unix environments, the cp
command plays a crucial role in copying files and folders. With its straightforward syntax, it allows you to effortlessly duplicate files and directories. Whether you’re a Linux aficionado, macOS user, or even operating in a Windows Subsystem for Linux (WSL) environment, the cp
command is at your disposal.
Let’s explore some practical examples of how to use the cp
command effectively.
Copying a File
To copy a file, you can utilize the cp
command along with the name of the file you wish to duplicate. For instance, let’s say we have a file named test
. We want to create a copy called another_apple
:
touch test
cp test another_apple
With these simple commands, the cp
command copies the contents of test
and generates a new file called another_apple
. You now have two identical files residing in the same location.
Copying a Folder
When you need to copy an entire folder, the cp
command requires the -r
option. By adding this flag, you ensure that all the contents within the folder are recursively copied.
Suppose we want to create a duplicate of a directory called fruits
and name it cars
:
mkdir fruits
cp -r fruits cars
In this example, the cp -r
command copies the entire contents of the fruits
directory and replicates them inside the cars
directory. With the -r
flag, you can preserve the structure and hierarchy of the original folder, making it an ideal choice for duplicating directories.
That’s it! With the cp
command, you can conveniently copy files and folders in Linux, macOS, WSL, and any Unix-like environment. It serves as a versatile tool for efficiently managing your files.