A system administrator is automating the backup process for a set of user directories. They have decided to write a shell script that compresses each user's home directory into an individual archive file. Assuming all user directories are located in /home, which snippet of code will successfully create a gzip-compressed archive of each user's home directory with the filename format 'username.tar.gz'?
for dir in /home/; do tar -czf "/home/\({dir}.tar.gz" "/home/\)"; done
for dir in /home/; do tar -czf "/home/${dir##/}.tar.gz" "$dir"; done
for dir in /home/; do gzip "/home/${dir##/}"; done
for dir in /home/*/; do tar -cf "\(dir" | gzip > "\).tar.gz"; done
The correct snippet iterates over every entry under /home, then calls tar with the -c (create), -z (gzip), and -f (file) options. The expression ${dir##*/} removes the leading path so the archive is named only after the user directory. Other snippets fail because they attempt to gzip a directory directly, place arguments to tar in the wrong order, or loop over /home itself rather than its subdirectories.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is the role of the tar command in the script?
Open an interactive chat with Bash
What does the ${dir##*/} syntax do in the script?
Open an interactive chat with Bash
Why is the incorrect snippet using 'gzip' after 'tar' not effective?