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'?
You selected this option
for dir in /home/; do tar -czf "/home/${dir##/}.tar.gz" "$dir"; done
You selected this option
for dir in /home/; do tar -czf "/home/\({dir}.tar.gz" "/home/\)"; done
You selected this option
for dir in /home/; do gzip "/home/${dir##/}"; done
You selected this option
for dir in /home/*/; do tar -cf "\(dir" | gzip > "\).tar.gz"; done
The correct script uses a 'for' loop to iterate over every item in the /home directory and then applies the 'tar' command with the correct options to compress each directory into an individual gzip archive file. The '${dir##*/}' expression is used to strip the path and leave just the user's name for the archive file. Incorrect answers may have syntax errors, use incorrect commands, or incorrectly handle the directory names, resulting in failure to achieve the desired task.
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?