A bash script that runs everyday and deletes some unnecessary files using crontab.
- How to initiate a bash script?
- #! /bin/bash
- How to declare a variable in shell and assign a commands output to it?
- variable=$(command)
- Please make sure that there is no space on either side on = and the command is always inside a ().
- How to assign a normal variable ? Just var=”value”
- How to comment? # this is my comment
- How to write an if loop?
- if [ condition ]; then task else task fi
- Note that there should be spaces before condition and [ ].
- || are used to add condition if the given task fails.
- How to find a file: find $path_from_where_to_delete -type f (for file) -name *.txt
- How to find multiple file types?
- Just put all types inside a ().
- find . -type f (-name *.txt -o (or) -name *.jpg)
- inside if loop if we have -z followed by condition, it checks if null or empty. we can also use -n to check if it is non empty
- IFS( internal field seperator) : he
IFS
variable is used to control the behavior of word splitting, which is how the shell breaks up arguments and input into separate words. In this script,IFS=$'\n'
sets theIFS
to a newline character (\n
). - To write a for loop the syntax is : for file in files; do task done
crontab crontab_file.txt
To check : crontab -l
- Add how much data is being getting free up by this process to monitor the work.
- Also log all the files that are getting deleted to splunk.