Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Linux 'cp' command

May 17, 2024

Introduction to the Linux ‘cp’ Command

As a programmer, one of the essential skills you will need to master when working with Linux systems is the ability to manage files and directories. The ‘cp’ command is a powerful tool that allows you to copy files and directories to a new location. In this blog post, we will explore the various use cases of the ‘cp’ command with extensive examples.

Basic Syntax

The basic syntax of the ‘cp’ command is as follows:

1
cp [OPTION]... SOURCE... DEST

Copying Files

Let’s start by looking at how to copy files using the ‘cp’ command. In its simplest form, you can copy a file by specifying the source file and the destination path:

1
cp file.txt /path/to/destination/

To copy multiple files, you can provide multiple source files separated by spaces:

1
cp file1.txt file2.txt file3.txt /path/to/destination/

You can also use a wildcard (*) to copy a batch of files that match a specific pattern:

1
cp *.txt /path/to/destination/

Copying Directories

The ‘cp’ command can also be used to copy directories. To copy a directory, simply specify the source directory and the destination path:

1
cp -r directory/ /path/to/destination/

The ‘-r’ flag is used to copy directories recursively, ensuring that all files and subdirectories are copied as well.

Preserving File Attributes

By default, the ‘cp’ command preserves the basic attributes of the copied files, such as permissions and timestamps. However, if you want to preserve additional attributes such as ownership and special files, you can use the ‘-p’ flag:

1
cp -rp source/ destination/

Copying With Confirmation

If you want to copy files and be prompted for confirmation before overwriting existing files, use the ‘-i’ flag:

1
cp -i file.txt /path/to/destination/

Verbose Output

To get a more detailed output of the files being copied, you can use the ‘-v’ flag:

1
cp -v file.txt /path/to/destination/

This will display each file as it is being copied.

Copying to Different Names

By default, the ‘cp’ command copies files to the destination with the same name. However, you can specify a different name for the copied file:

1
cp file.txt /path/to/destination/newfile.txt

Conclusion

The ‘cp’ command is a versatile tool for copying files and directories in Linux systems. We explored its basic syntax and covered various use cases, including copying files, copying directories, preserving file attributes, interactive copying, verbose output, and copying files with different names. Understanding the ‘cp’ command is crucial for efficient file management on Linux, so be sure to practice and explore its functionality further to become a more effective programmer.

I hope you found this blog post helpful in expanding your knowledge of the ‘cp’ command. Stay tuned for more informative posts on various Linux commands and programming techniques. Happy coding!


➡️ Implementing WebSocket Communication on ESP32


⬅️ Real-time Operating Systems for ESP32: Choosing the Right One for Your Project


Go back to Posts.