Title: Automating File Copy with a Bash Script
Introduction:
In this blog post, we’ll explore how to automate the process of copying .* files from a specific directory to a network share using a simple Bash script. This script will not only save time but also provide a convenient way to keep your important .* files synchronized with a shared network location.
Prerequisites:
Before we begin, make sure you have the following:
A Mac computer with macOS installed.
.* files that you want to copy in your “FolderLocation” directory.
A mounted network share or accessible SMB network drive where you want to copy the files.
Step 1: Creating the Script:
We’ll create a Bash script to perform the file copy. Open your favorite text editor and paste the following code:
bash
#!/bin/bash
# Source file pattern (all PDF files) in your Documents directory
source_files=/Documents/*.pdf
# Destination directory on the mounted volume or network share
destination_dir=/Volumes/Public
# Create an array to store the copied file names
copied_files=()
# Check if there are any .* files to copy
if ls $source_files 1> /dev/null 2>&1; then
# Loop through the .* files and copy them to the destination directory
for file in $source_files; do
cp "$file" “$destination_dir”
copied_files+=("$(basename "$file”)”)
done
# Display the list of copied files
if [ ${#copied_files[@]} -gt 0 ]; then
echo "The following files were copied:”
printf '%s\n' "${copied_files[@]}”
else
echo "No .* files found to copy.”
fi
else
echo "No .* files found in the ~/Documents directory.”
fi
Step 2: Making the Script Executable:
Save the script to a file (e.g., copy_pdfs.sh), and then make it executable by running the following command in the terminal:
bash
chmod +x copy_pdfs.sh
Step 3: Executing the Script:
Navigate to the directory where you saved the script in the terminal and run:
bash
./copy_pdfs.sh
The script will copy all the PDF files from your “Documents” folder to the “Public” folder on the mounted volume or network share. It will then display a report of the copied files, or inform you if no PDF files were found in the “Documents” folder.
Conclusion:
Automating file copying tasks with a Bash script can significantly streamline your workflow and help you stay organized. By following the steps outlined in this blog post, you can effortlessly copy your PDF files from your “Documents” directory to a network share, ensuring that your important documents are always accessible to your team or colleagues.
Happy automating!