- What is a copy command in shell scripts?
- The syntax of the CP command
- How to copy a file into another directory
- Copy a directory and directory with its files
- How to see files that are copied
- Copy file in shell script examples
- If you want to open Linux files in Windows easily
- How to use DiskInternals Linux Reader
- Related articles
- Linux bash script to copy files
- 1 Answer 1
- Copy List of Files Using Bash Script
- Creating and Executing Bash Script
- Copy only files from a specific directory
- Copy all files of specific extensions
- Copy all Files, Including Directory
- Copy files from the user-specified path
- Conclusion
- About the author
- Karim Buzdar
What is a copy command in shell scripts?
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.
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:
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
- 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.
Related articles
Linux Shell ZSH Bash Perl Ext2/Ext3/Ext4 Arch Linux Systemd New Mount a drive Got error or failure How to
- About a bash date command
- Bash and sh: is there any difference
- About Bash Language
- Guide in Pictures
- Shell script usage
- Bash: A Script For User Input
- Here is everything you need to know about Bash for Windows
- Bash Script SSH: How to Use It
- How to use bash get script directory in Linux
- Learn about a bash error code
- 5 Basic Shell Script Examples for Your First Script
- How to Check Bash String Equality
- How to Use Linux Wait Command
- Bash: How to Loop Through Files in Directory
- 20 Examples of Bash Find Command
- Bash Cat Command in Examples
- Linux ZSH: the basic you need to know
- Shell Script Cut: Basic You Need to Know
- Bash: How to Check if String Not Empty in Linux
- Basic of Grep in Linux Shell Script
- Bash: how to split strings in Linux
- An Algorithm: How to Create Bash While Loop
- Bash Time Command on Linux
- Bash: How to Check if the File Does Not Exist
- How to Use Shell Script Sleep Command
- Linux Shell: What You Need to Know at First
- Bash Script: All You Need to Know
- Mount Linux Drive on Windows for Free
- How to Mount Ext4 on Windows for Free
- How to Access Ext4 from Windows
- How to Access Linux Ext2 or Ext3 on Windows
- Linux: Sudo in Bash Scripts
- A Linux bin/bash Shell
- Linux: Bash Printf Examples
- Linux: Bash First Line
- Linux: Write a Shell Script
- Linux: A Bash Source Command
- A Bash to Loop through Lines in File
- A Bash Test Command
- A Bash Status of Last Command
- Linux: New Line in Shell Script
- Linux: $0 in a Shell Script
- Linux: A Bash Startup Script
- A Bash Multiline Command
- A Bash Dirname Command
- Linux: A Bash Linter
- A Bash Nested Loop
- Use a Shell Script to Send an Email
- Hello World Script in Bash
- A crontab service shell script
- Linux: A Bash Basename Command
- Using Bash to Write to a File
- AWK in a Bash Script
- Learn about systemd startup script
- About chaining bash commands
- Using a bash tee command
- Learn about useful bash scripts
- Bash for Loop in One Line
- Learn about a bash wait command
- Arch Linux install script
- About advanced bash scripting
- To run a shell script in Dockerfile
- About a bash export command
- About a bash UNTIL loop
- Using /usr/bin/env command
- Learn about Korn shell scripting
- Shell Script: Replace String in File
- Linux: Bash String Ends With
- Whether bash waits for command to finish
- Using bash if 0
- Using && in an IF statement in bash
- If you want to run shell script in background
- The disk you inserted was not readable by this computer error
- Learn about C shell script
- Learn about SFTP in bash scripting
- Install Oracle Database
- Examples of using the Expect
- Learn to run Perl script in Linux
Linux bash script to copy files
I need script to copy on cron basis a list of files. Files selected on name/datetime pattern and to name of file destination must by appended data like ddmmyyy. It is not problem copy files or directory, but problem to change name of each file according to its data. May be exists some open source solution? Thanks.
1 Answer 1
You haven’t provided enough information for me to give you real working code; but you can do something like this:
file=dated_log.log ddmmyyyy=$(read -r < "$file" ; echo "$") cp "$file" "$file.$ddmmyyyy"
The above will copy dated_log.log to data_log.log.30102011 , assuming that the first line of dated_log.log starts with 30102011 .
The Bash Reference Manual will hopefully help you adjust the above to suit your needs.
@gniourf_gniourf: Yes, good point. But I think my answer is better the way it is, because I rather doubt that the OP's file format actually starts with the exact eight right characters, and I think the code that I posted is likely to be more easily tweaked into whatever form the OP needs. (Suppose that the OP actually needs characters 30 through 37. How would you adjust read -r -n 8 ddmmyyy to retrieve them?)
for characters 30 through 37: read -r -n 37 ddmmyyyy < "$file"; ddmmyyyy=$
Copy List of Files Using Bash Script
Bash (Bourne Again Shell) is the kind of shell that is for executing commands and scripts. Bash was a developed version of the sh shell. Bash Script is a file where multiple shell commands are scripted to perform a particular task. In this article, we will see how we can copy multiple files using a bash script. For this article, I am using Ubuntu 20.04 to demonstrate the example.
Note: – $USER will print current login users’ usernames.
If you are curious what version of bash shell is installed in the system, we can check it using the following command.
Creating and Executing Bash Script
Let’s start with creating a simple file using any editor of your choice. For me, the vi editor is more comfortable. To make the file executable, we need to add shebang (!#) and bash interpreter location at the beginning of the script. I have created a text.txt file and add it to bash_demo dir in my home dir that contains some text for demo purposes.
Add the following lines in your text editor for a sample demo after creating a file; if you haven’t, the editor will create a new file on write and quit.
We can execute the script using ./ before the script file, which determines the current dir file.
When we execute the script, the following error will be thrown in our terminal.
Initial execution of bash file.
When we create a file by default, the user doesn’t have execution permission for the file. To provide execution permission to the user, the following command must be executed.
Output after permission granted.
Copy only files from a specific directory
For fetching all the files and dir from a specific path, we will use for loop in the script then filter out the only file using if condition. In the example below, we execute the cp command only executed if the iterator was a file which is determined -f flag.
#!/bin/bash
dpath = / var / log / nginx /*
for FILE in $dpath
do
if [ [ -f $FILE ] ]
then
cp $FILE / home / $USER /
else
echo “There are no files in the given path.”
fi
done
Copy all files of specific extensions
In this example, we will copy all the files with the .log extension. We need to add *.log to the path so that iterate the only file with .log extension for loop only.
Copy all Files, Including Directory
In this example, we will copy all the files, including directories, recursively. For that, we simply need to add -R cp command where -R determines recursively fetching of the directory.
Copy files from the user-specified path
In this example, we will copy files from user-specified dir. To do so, we will use the read command to request the path from the user then check if the user provides the path to dir or not, which is done by the -d flag in the condition. After verifying dir, we will use a for loop to iterate all the files and dir inside the given path, then again filter out the only files using the if condition. If the condition matches, the following cp command will be executed.
#!/bin/bash
echo “Please provide a path to dir.”
read path
if [ [ -d $path ] ]
then
for FILE in $path /*
do
if [ [ -f $FILE ] ]
then
cp $FILE / home / $USER /
else
echo “There are no files in the given path.”
fi
done
else
echo “Path to dir is required”
fi
In my home dir, I have the following files and dir.
Output when providing the path to a file.
Output when providing dir location path.
After executing the script file, we can check the output in the predefined dir in the script. In my case, I have copied the file in my home dir, and the following is the result.
Conclusion
In this article, we learn about how to copy files using bash scripting. We can use many other operations like a loop, if-else, etc. Bash scripting is more effective when working with multiple commands to perform specific tasks. I hope you like this article on copying files using a bash script.
About the author
Karim Buzdar
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.