Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
- The problem statement, all variables and given/known data:
There are files stored like 14.Aug.2014.log, 15.Aug.2014.log etc. in a folder $HOME/log you need to find out all the log files of last 1 month and move them into $HOME/logs/lastmonth/
this should be implemented with reference of file name.
The solution need be in efficient way.
- Relevant commands, code, scripts, algorithms:
Commands : Awk, Date.
[Algorithm]
- Find the 30 days back date
- Get all the Log files in a variable called file_data
- Awk the Date information from the File name (string)
- Convert the File date from string to Numeric
- Compare the each file date with 30 days back date
- If the File date is older than 30days date move to Last month directory
[/Algorithm]
#!/bin/bash
date_30_days=`date --date="30 days ago" +"%Y%m%d"`
#echo $date_30_days
for file_date in *.log ;
do
echo $file_date
file_date_format=`echo $file_date|awk -F '.' '{ print $1 " " $2" "$3}'`
file_date_numeric=`date -d "$file_date_format" +%Y%m%d`
#echo $file_date_numeric
if [ $date_30_days -ge $file_date_numeric ]; then
mv $file_date ./lastmonth/$file_date
else
echo "NO OLD FILES"
fi
done
- The attempts at a solution (include all code and scripts):
#!/bin/bash
date_30_days=`date --date="30 days ago" +"%Y%m%d"`
#echo $date_30_days
for file_date in *.log ;
do
echo $file_date
file_date_format=`echo $file_date|awk -F '.' '{ print $1 " " $2" "$3}'`
file_date_numeric=`date -d "$file_date_format" +%Y%m%d`
#echo $file_date_numeric
if [ $date_30_days -ge $file_date_numeric ]; then
mv $file_date ./lastmonth/$file_date
else
echo "NO OLD FILES"
fi
done
- Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Shajahan, Sevya Multimedia, Greater Noida, Delhi
India, Narasayya. Test Automation training.
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).