ou want to create a Bash script that will automatically delete log files older than 7 days in a specific directory. Which command sequence correctly accomplishes this task while considering best practices for error handling and efficiency?1.0 Marksfind /path/to/logs -type f -name "*.log" -mtime +7 -exec rm {} \;for file in $(find /path/to/logs -type f -name "*.log" -mtime +7); dorm "$file" || echo "Error deleting $file"donefor file in /path/to/logs/*.log; do[ -f "$file" ] && find "$file" -mtime +7 -exec rm {} \;donefind /path/to/logs -type f -name "*.log" -mtime +7 | xargs rm
Question
ou want to create a Bash script that will automatically delete log files older than 7 days in a specific directory. Which command sequence correctly accomplishes this task while considering best practices for error handling and efficiency?1.0 Marksfind /path/to/logs -type f -name ".log" -mtime +7 -exec rm {} ;for file in file" || echo "Error deleting file" ] && find "$file" -mtime +7 -exec rm {} ;donefind /path/to/logs -type f -name ".log" -mtime +7 | xargs rm
Solution
The correct command sequence to accomplish this task is:
find /path/to/logs -type f -name "*.log" -mtime +7 -exec rm {} \;
Here's a step-by-step explanation:
-
find /path/to/logs -type f -name "*.log": This command will find all files (-type f) with the extension.login the directory/path/to/logs. -
-mtime +7: This option will filter out the files that were last modified more than 7 days ago. -
-exec rm {} \;: This option will execute thermcommand on each file that matches the previous conditions. The{}is a placeholder for the current file, and\;indicates the end of the-execcommand.
This command sequence is efficient because it only spawns one rm process per file, and it handles errors well because find will not pass non-existing files to rm.
Similar Questions
Which command is suitable in deleting directory that contains files in it?2.0 Marks#rm –r directory#rm directory#remove directory#delete directory
You're creating a Bash script to automate backups of important configuration files. The script should store backups in a folder named with the current date (e.g., "2023-07-06"). Which command correctly generates such a folder name in Bash?1.0 Marksfolder_name=$(date +%Y-%m-%d)folder_name=date +%Y-%m-%dfolder_name=date +%Ffolder_name=date -Ifolder_name="date +%Y-%m-%d"
Which of the following commands can be used to delete a file on a Linux system?Question 17Answera.rm file.txtb.delete file.txtc.del file.txtd.erase file.txt
Choose the command which will print all the directories within the current directory, whose names starts with the letter 'K' and are accessed in last two days.find /home -type d -mtime +2 -printfind . -type l -mtime +2 -printfind /home -type d -atime +2 -name 'K*' -printfind . -type d -atime -2 -name 'K*' -print
You're developing a Bash script that needs to create a temporary directory, perform some operations within it, and then delete the temporary directory and its contents when done. Which command sequence ensures the secure creation and deletion of the temporary directory?1.0 Markstmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -r $tmp_dirtmp_dir=$(mktemp -d)rm -r $tmp_dirtmp_dir="/tmp/mytempdir"trap "rm -r $tmp_dir" EXITmkdir $tmp_dirtmp_dir=$(mktemp -d)trap "rm -r $tmp_dir" EXITtmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -rf $tmp_dir
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.