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.
$ vi bash_demo.sh
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.
cp text.txt /tmp/
echo “File copied.”
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.
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.
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.
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.
for FILE in /var/log/nginx/*.log
do
cp $FILE /home/$USER/
done
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.
for FILE in /var/log/*
do
cp -R $FILE /home/$USER/
done
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.
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.