Shell Scripting , Moving Old file to specific folder

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. 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.

  1. Relevant commands, code, scripts, algorithms:
    Commands : Awk, Date.
    [Algorithm]
  1. Find the 30 days back date
  2. Get all the Log files in a variable called file_data
  3. Awk the Date information from the File name (string)
  4. Convert the File date from string to Numeric
  5. Compare the each file date with 30 days back date
  6. 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
  1. 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
  1. 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).

The given implementation with GNU date works okay.
Only the else echo "NO OLD FILES" is a bit odd; one would rather expect that at the end of the loop. You can set a "found" variable in the loop, and query it at the end.

Hello Sir,

Thank you for your reply,

But my Mentor is replied as :

So can you please guide me to solve in efficient way.

Invoking a utility (such as awk or date ) that is not a built-in in the shell is a relatively expensive operation. If you invoke a utility like once for every file in a directory when it only needs to be invoked once or if you invoke two utilities when only one is needed, your script will be relatively inefficient and slow. Instead of invoking awk and date once for every log file in the directory, would it be possible to:

  1. invoke awk just once:
    [list=a]
  2. giving it $date_30_days as a variable,
  3. feeding each log file name to it as one line to be read from standard input,
  4. having awk convert the string form of the file's date to a numeric string instead of invoking the date utility to perform that conversion,
  5. lelting your awk script do the date comparisons as well as changing the date format, and
  6. just printing the names of the files that need to be moved?
    [/list]
  7. letting your shell script read the output from the above awk script and performing the moves for the filenames awk prints, and
  8. only printing NO OLD FILES if the awk script does not return the names of any files to move?

If performance is an issue, don't use awk at all.

bash-3.2# date=14.Aug.2014.log                         
bash-3.2# IFS=.                                        
bash-3.2# date1=`echo $date`                           
bash-3.2# echo $date1                                  
14 Aug 2014 log                                        
bash-3.2# /usr/gnu/bin/date -d "$date1" +%Y%m%d        
20140814