Quote:
Originally Posted by tsitras
hi. in a folder i do have a whole lot of files created every day. i need to compress files older than 30 days in tgz and delete the compressed files.
Code:
#!/bin.bash
FILES=`find . -mtime -30 | xargs`
tar --no-recursion -czf backup_feb_2013.tgz "${FILES}"
rm -rf "${FILES}"
the compressed files have the following format
g303-20130205-060552-1360037152.419.wav
i am stuck on how to edit the script so it will automatically create the month of the backup. Now the naming is manual.
|
You should read the man page on the date command, and the bash scripting tutorial on tldp:
http://tldp.org/LDP/abs/html/timedate.html
Pay particular attention to the format options on the date command, and in your case specifically, the %b, %B, and %m flags. Try this for example:
Code:
#!/bin/bash
monthlong=`date +%B`
monthshort=`date +%b`
monthnum=`date +%m`
year=`date +%Y`
echo "Month Long Form - $monthlong"
echo "Month Abbrev. - $monthshort"
echo "Month Number - $monthnum"
echo "Year - $year"
echo "Backup filename = backup_$monthshort"_"$year.tgz"
# Read the 'date' man page for more formatting options.
exit 0
If you put the month/year into variables, that leaves you a good path in your script for tests later, should you want to. Things like checking what year it is and doing some action, or (for example) if it's a leap year, and the end of the month in February is a different date.
And you should also read the man page on the tar command, and look into the "--remove-files" flag, which will delete the files after they're added to the tar archive.