Skip to main content

Bash script safely remove or delete files

Automation is a buzzword in today's technology; it makes things faster, effortless, and most of all ease the burden of system administrators.

However, automation should be tested thoroughly to avoid any unforeseen circumstances or else instead of making life easier it might deliver some nightmares.

For example, removing log files to save space or remove any unwanted or old log files to make room for new files; this task can be done manually or using some automation via a script.

In Linux, cron jobs that fires automatically as per schedule is quite ideal to automate removing or deleting log files.

This can also be done manually, by simply logging in to the server and delete files.

There is nothing wrong in deleting manually but would not be ideal if the operation of the server is operating day and night. 

There might be a chance that the operation will halt if there is no more room to write the log files. Or simply when there is some issue and wanted to troubleshoot; if log files are not available then it might take a longer time to troubleshoot since there are no reference files for troubleshooting and troubleshooting will be a nightmare.

Clearing the log files and removing old files is a must, in order to save space and make room for new files.

In Linux, rm  file_to_be_deleted  is quite straight forward to remove or delete a file.

Removing files via script or automation, needs to be done carefully in order not to remove any unwanted files.

Example script:

LOG_DIR="." #or change the location

# Find files older than 30 days and loop through them

find "$LOG_DIR" -type f -mtime +30 -print0 | while IFS= read -r -d '' file; do

    echo "Processing: $file"

    # Or just move to archive folder

    # mv "$file" ./archive/

    #show which files will be deleted (check if it shows the correct files)

    echo "Removing files: rm -rf -- $file"

    # Or delete (uncomment carefully)

    #rm  -rf -- "$file"

done

rm -rf -- "$file"  => is a safety mechanism in order not to delete recursively
The -- forces rm to treat anything beyond -- as a literal path.

Example, if there is a malicious filename like:

file="-rf-badass-filename.txt"   # A malicious or weird filename
rm -f $file                                 # Becomes: rm -f -rf-badass-filename.txt (-rf is treated as a parameter not a filename)

But yes, newer operating distro or systems doesn't fall into this trick anymore since they will append the full path when removing the file.

Example for the script above it will be:
rm -rf ./any_old_files.log
rm -rf ./-rf-badass-filename.txt

The full path is appended, averting a recursive deletion.

Even if the operating system has already a mechanism to avoid disaster, it would be better to just use the rm -rf -- $file syntax to have a peace of mind.

Till next time...





Fill your hearts with grateful thoughts and there is no more room for worry and fear. 

And turn your eyes and hearts towards the love of God.


Comments

Popular posts from this blog

Print error 016-799 - Fuji Film Xerox

016-799 Fuji Xerox or Fuji Film print error code. That shows a description error as “Print instruction Fail detected in decomposer.” The error code and error description are alien languages for users and even system administrators who are not familiar with Fuji Xerox error code. The error code is quite simple and easy to fix, if the job print goes to the printer but print out doesn’t come out. So, basically the print job was received by the printer, but the printer just doesn’t know what type of paper or what size to use or which tray to utilize for the print out. In some instances, this is just a paper mismatch but the error description; if using Windows 10 to print does not exactly points to what is the issue. First thing to check, is the paper size selected by the user to print. Example, if the printer configuration is A3 and A4 sizes only. But then the person printing the file accidentally chooses “A4 Cover” then this error 016-799 will occur. ...

WMIC get computer name

WMIC get computer model, manufacturer, computer name and  username. WMIC is a command-line tool and that can generate information about computer model, its manufacturer, its username and other informations depending on the parameters provided. Why would you need a command line tool if there’s a GUI to check? If you have 20 or 100 computers, or even more. It’s quite a big task just checking the GUI to check the computer model and username. If you have remote computers, you need to delegate someone in the remote office or location to check. Or you can just write a batch file or script to automate the task. Here’s the code below on how get computer model, manufacturer and the username. Open an elevated command prompt and type:     wmic computersystem get "Model","Manufacturer", "Name", "UserName" Just copy and paste the code above, the word “computersystem” does not need to be change to a computer name. A...

PowerShell GUI with buttons, textbox and combobox

GUI makes life easier, but of course command line has a power of its own. How to add a form in PowerShell with Buttons, TextBox and ComboBox? Adding GUI forms in PowerShell must be done manually by code. It’s not that hard, you just need to love PowerShell and see what it can do to automate IT administration and makes your life easier. Anyway, code below introduces how to add GUI to PowerShell and it also illustrates how to make use of those GUI buttons and send a command to remote computers. Code to add buttons, textbox and combobox in PowerShell, and how to execute a command after the button is clicked. #initialize the main form $form = new-object Windows.forms.form $form . text = "Server Selection Form" $form . minimumSize = New-Object System.Drawing.Size ( 600 , 300 ) $form . maximumSize = New-Object System.Drawing.Size ( 600 , 300 ) #add a button to the form $button = new-object windows.forms.button $button . text...