• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Linux Challenge #2: Real-World Log Cleanup with Command Line Automation

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
Welcome back, command-line champs!

In this challenge, we’re simulating a real-world task every sysadmin faces: managing and cleaning up system logs. This is where your rm, mv, cp, find, and echo commands come into play in a real scenario!

Challenge Brief


You’ve been asked to clean up a log directory on a RHEL 9 machine.

The logs are eating up space, and you need to:

? Task Overview

  1. List all files in /var/logs/.
  2. Delete all .log files except syslog and auth.log.
  3. Move the remaining logs into an archive folder called /var/logs/archive/.
  4. Create a cleanup report that lists all deleted and moved files with timestamps.
Full Script Solution


#!/bin/bash

# Step 1: List all files in /var/logs/
echo "Listing all files in /var/logs/"
ls /var/logs/

# Step 2: Delete unnecessary log files except syslog and auth.log
echo "Deleting unnecessary log files..."
find /var/logs/ -maxdepth 1 -type f -name "*.log" ! -name "syslog" ! -name "auth.log" -exec rm -v {} \;

# Step 3: Move remaining .log files to an archive folder
echo "Moving important logs to archive..."
mkdir -p /var/logs/archive
find /var/logs/ -maxdepth 1 -type f -name "*.log" -exec mv {} /var/logs/archive/ \;

# Step 4: Create a cleanup report
echo "Cleanup report - $(date)" > /var/logs/cleanup_report.txt
echo "Moved files:" >> /var/logs/cleanup_report.txt
ls /var/logs/archive/ >> /var/logs/cleanup_report.txt
echo "Remaining files in /var/logs/:" >> /var/logs/cleanup_report.txt
ls /var/logs/ >> /var/logs/cleanup_report.txt
What You Just Practiced

CommandPurpose
findSearches for files matching specific criteria
rm -vDeletes files with verbose output
mvMoves files to another directory
mkdir -pCreates a directory (and parents if they don't exist)
echo >Outputs text to a file
lsLists files for logging/reporting
Bonus Challenge


Make this script interactive:

  • Prompt the user for the log folder path.
  • Confirm before deleting files.
  • Add a log rotation option that backs up .log files with today’s date appended.

Want help building that version? Drop a comment and we’ll go there together.

Why This Matters


This type of script is gold in production. Automating file cleanup prevents your system from getting bloated and shows real command over Linux fundamentals. Try tweaking this to work for different folders or file types.

Next up? We’re jumping into file permissions and ownership. But for now—clean up, document your moves, and show that terminal who’s boss!

Tags:

#linux #redhat #rhcsa #techwithengineers #techtransition #learnlinux #devops #linuxlab #cloudwhistler #30daychallenge #opensource #techjourney


Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу