What is a copy command in shell scripts?
Here you will find out:
- what a copy command in shell scripts is
- how to copy a file into another directory
- what copy file in shell script examples are
- when DiskInternals can help you
Are you ready? Let's read!
Copy commands to use in shell scripting
For every operating system, there are always different commands to copy files between folders or directories. Specifically, if you program in Linux or UNIX-like systems, you need to know the various copy commands to use in shell scripting.
The syntax of the CP command
As a Linux operator, you need to learn the CP command, and they are listed below; these command parameters are used to copy files and directories:
- cp SOURCE DEST
- cp SOURCE DIRECTORY
- cp SOURCE1 SOURCE2 SOURCE3 SOURCEn DIRECTORY
- cp [OPTION] SOURCE DEST
- cp [OPTION] SOURCE DIRECTORY
The first and second forms let you copy a “SOURCE” file to “DEST” file or a DIRECTORY, while the third form lets you copy multiple “SOURCE” files to a DIRECTORY. Similarly, the fourth and fifth forms work for special purposes.
How to copy a file into another directory
Here's a practical example of copying file(s) from one directory to another using the CP command.
The script above will copy “filename” to the “test” folder.
Copy a directory and directory with its files
If you wish to copy a directory with all of its files and sub-directories, there are ways to do that:
Recursive copy
Second method
The asterisk (*) means to copy everything in a directory to another one.
This command will copy all .doc files in a directory to another one. You can identify any file extension for the particular files you want to copy.
How to see files that are copied
When you have copied the files and want to know if the action was successful, here is how to check the copied files.
Use the -v option:
Copy file in shell script examples
If you wish to create a copy of test.docx file to in a directory but save it as tutorial.docx, run:
Output:
For copying multiple files into a new directory, here’s the command:
Let’s copy the files test.c, linux.h and con.c to another directory called myfiles:
If the directory is located in a sub-directory:
If you want to open Linux files in Windows easily
Basically, it is impossible to access Linux files on Windows, unless you use Linux Reader. This is because Windows does not support Linux file systems; hence, your Linux partitions are hidden from Windows File Explorer. But with DiskInternals Linux Reader, you can access Linux partitions on Windows and open the files saved inside.
Interestingly, DiskInternals Linux Reader works for dual-boot PC users, as well as virtual machine users. The software features a user-friendly interface and supports all Linux file systems (Ext, UFS, HFS, ReiserFS, and APFS).
How to use DiskInternals Linux Reader
- Download and install Linux Reader on Windows.
- Launch the program and click on the drive that contains the Linux partitions.
- DiskInternals Linux Reader will scan the hard drive and display all the files and folders saved inside.
- Right-click on the files to preview them in the new window.
- Now, copy and save the Linux files to a Windows partition and open them.