Copying files and folders in linux bash scripts is a fundamental task in any operating system. In Linux, this can be efficiently done using the Bash shell and the cp command. This tutorial provides a n overview with examples on how to use the cp command in various scenarios, ensuring a clear and concise understanding for users of all levels.
Table of Contents
Understanding the linux cp Command
The cp command is the primary method for copying files and directories in Linux. It’s included in the GNU core utilities package, making it available in virtually all Linux distributions. This is a sample generic command to understand how the copy command works.
cp [option] [source] [destination]
This command copies files or directories from the source to the destination. The [option] parameter is optional and allows for additional functionality, which will be discussed in the following sections.
Basic file copy with Linux
Copying a Single File with on linux
To copy a single file, specify the source file and the destination directory or file. If the destination file name is omitted, cp creates a file with the same name as the source file in the specified directory.
cp source_file.txt /path/to/destination/directory/
Rename linux files during the copy
To copy and rename a file in one single step, it is easy, simply specify a new file name at the destination.
cp source_file.txt /path/to/destination/new_file_name.txt
Copy multiple linux files
You can copy multiple files to a single destination directory by specifying each source file, separated by spaces.
cp file1.txt file2.txt /path/to/destination/
Recursive copy of files and directories in linux
To copy entire directories, including all subdirectories and files, use the
--recursive option. It is also a straight forward copy.
cp -r /path/to/source/directory/ /path/to/destination/directory/
Advanced copy options
The previous section of the tutorial explained how to copy files and folders using simple linux bash options. Now let’s dive in more advanced scripts.
Verify copy integrity
-v option to verify that files are copied correctly.
cp -v source_file.txt /path/to/destination/
Prompt overwrite during files copy
By default, cp will overwrite files without warning. This behaviour can be dangerous in some cases. To enable a prompt before overwriting, use the -i option.
cp -i source_file.txt /path/to/destination/
Copy and prevent overwriting
To prevent completely the cp command from overwriting existing files, use the -n option.
cp -n source_file.txt /path/to/destination/
Preserving linux file attributes
To preserve the file attributes such as timestamp and permissions, use the -p option. It will prevent for example to have a creation date equal to the date the file was copied.
cp -p source_file.txt /path/to/destination/
Using Wildcards for Pattern Matching
Wildcards can be used to copy multiple files that match a specific pattern. As we previously saw this copy and filter option in the MS-DOS file system, it is extremely powerful to filter and copy only a specific extension type, like for example:
Indeed, this wildcard option combined with the recursive copy can help a lot when sorting and copying a large number of files in multiple folder levels.
cp /path/to/source/*.txt /path/to/destination/
Use advanced linux options for effective copy
This guide covers the basics and some advanced examples on how to copy files and folders using linux bash. Whether you are copying single files, entire directories, or using advanced options for specific requirements, understanding and using these commands will enhance your skills in Linux file management.
For further information and support on Linux distributions where these copy commands are applicable, you can visit the official websites of popular distributions like these ones: