Copy all files ending with .txt from the current directory and all subdirectories into /home/user/backup/, preserving the directory structure relative to the starting point. Only files with the .txt extension should be touched.
Deliverables
A runnable Bash script named backup_txt.sh placed in the workspace root.
The script must copy all matching files while creating any needed subdirectories under /home/user/backup/. If a destination file already exists, it should be overwritten with the source.
The script should be executable (chmod +x backup_txt.sh).
Approach
Use a POSIX-friendly Bash script that:
Locates all .txt files recursively from the current directory.
Creates the destination directory structure under /home/user/backup/ to mirror the source structure.
Copies each file to its corresponding location in the backup directory.
A robust implementation can be achieved with find and a small loop. Example (illustrative, not required to exactly match this snippet):
bash
#!/usr/bin/env bash
set -euo pipefail
src_root=$(pwd)
dest_root="/home/user/backup"
while IFS= read -r -d '' src; do
rel="${src#$src_root/}"
dest="$dest_root/$rel"
mkdir -p "$(dirname "$dest")"
cp -f -- "$src" "$dest"
done < <(find "$src_root" -type f -name '*.txt' -print0)
How to run
bash backup_txt.sh
Verification tips
After running, verify that every .txt file in the source has a corresponding copy in /home/user/backup with the same relative path.
Example: if ./docs/notes.txt exists, ensure /home/user/backup/docs/notes.txt exists.