Bash Script to Read a List of Files and Copy Files

Fustigate (Bourne Again Shell) is the kind of shell that is for executing commands and scripts. Bash was a developed version of the sh shell. Fustigate Script is a file where multiple shell commands are scripted to perform a particular task. In this article, we will see how we can re-create multiple files using a fustigate script. For this article, I am using Ubuntu xx.04 to demonstrate the example.

Note: – $USER will print current login users' usernames.

If you lot are curious what version of bash shell is installed in the system, we can bank check it using the post-obit control.

Fustigate Version Output.

Creating and Executing Bash Script

Permit's starting time with creating a simple file using whatever editor of your choice. For me, the vi editor is more comfy. 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 abode dir that contains some text for demo purposes.

$ bear on bash_demo.sh
$ vi bash_demo.sh

Add the following lines in your text editor for a sample demo later creating a file; if you haven't, the editor volition create a new file on write and quit.

#!/bin/bash
cp text.txt /tmp/
echo "File copied."

We tin can execute the script using ./ before the script file, which determines the current dir file.

When nosotros execute the script, the following fault will exist thrown in our terminal.

Initial execution of bash file.

When we create a file by default, the user doesn't accept execution permission for the file. To provide execution permission to the user, the following command must be executed.

Output later on permission granted.

Copy only files from a specific directory

For fetching all the files and dir from a specific path, we volition use for loop in the script so filter out the but file using if condition. In the example beneath, we execute the cp control simply executed if the iterator was a file which is determined -f flag.

#!/bin/fustigate
dpath = /var/log/nginx/*
for FILE in $dpath
do
if [ [ -f $FILE ] ]
and so
cp $FILE /dwelling house/ $USER /
else
echo "There are no files in the given path."
fi
done

Copy all files of specific extensions

In this example, nosotros 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 but.

#!/bin/bash
for FILE in /var/log/nginx/*.log
do
cp $FILE /home/ $USER /
done

Copy all Files, Including Directory

In this example, we volition copy all the files, including directories, recursively. For that, nosotros simply need to add -R cp command where -R determines recursively fetching of the directory.

#!/bin/fustigate
for FILE in /var/log/*
do
cp -R $FILE /dwelling/ $USER /
done

Copy files from the user-specified path

In this example, nosotros will copy files from user-specified dir. To do so, we will use the read command to request the path from the user and then check if the user provides the path to dir or non, which is done by the -d flag in the status. 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 just files using the if condition. If the condition matches, the following cp control 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
repeat "There are no files in the given path."
fi
done
else
echo "Path to dir is required"
fi

In my domicile 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, nosotros tin can bank check the output in the predefined dir in the script. In my case, I have copied the file in my abode dir, and the following is the effect.

Conclusion

In this article, we learn about how to re-create files using fustigate scripting. We tin apply 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 yous like this article on copying files using a bash script.

About the author

Karim Buzdar holds a degree in telecommunication applied science and holds several sysadmin certifications. As an It engineer and technical writer, he writes for various web sites. He blogs at LinuxWays.

ferrellponver.blogspot.com

Source: https://linuxhint.com/copy-files-list-bash-script/

0 Response to "Bash Script to Read a List of Files and Copy Files"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel