There is file A.lst
A.lst
b/pro/c/ab.par
b/pro/tam.par
b/pro/cc/lam.par
We need to create missing folders like mentioned below in the following way and also add _bkp
to the file name and then copy the files to location, as you can see folder folders c and cc are created and files are also copied.
Output
b/pro/backpro/c/ab_bap.par
b/pro/backpro/tam_bkp.par
b/pro/backpro/cc/lam_bkp.par
How can this be achieved using for loop.
**** Tried below****.
#!/bin/bash
# Define the source directory.
source_dir="b/pro"
# Define the destination directory
destination_dir="b/pro/backpro"
# Loop through each file in
A.lst #
while IFS= read -r file; do
****Extract the filename without extension****
filename=$(basename "$file" | sed 's/\.par$//')
# Append "_bkp.par" to the filename #.
new_filename="${filename}_bkp.par"
# Construct the source and destination paths
source_path="$source_dir/$file"
destination_path="$destination_dir/$new_filename"
# Create the destination
directory if it doesn't exist
mkdir -p "$(dirname "$destination_path")"
**Copy the file to the
destination with the new filename**
cp "$source_path" "$destination_path"
echo "Copied $source_path to $destination_path"
done