Shell Scripting , Moving Old file to specific folder

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Scripting , Moving Old file to specific folder
# 1  
Old 09-15-2014
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.

2. 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]

Code:
#!/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

3. The attempts at a solution (include all code and scripts):

Code:
#!/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

4. 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).

Last edited by rbatte1; 09-15-2014 at 09:25 AM.. Reason: Added LIST=1 tags
# 2  
Old 09-15-2014
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.
# 3  
Old 09-17-2014
Thank you

Hello Sir,

Thank you for your reply,

But my Mentor is replied as :
Quote:
What if there are lots of log files in that directory. Can you think of a way to iterate not more than 30 times?
The way you have done is not efficient.
So can you please guide me to solve in efficient way.

Last edited by rbatte1; 09-18-2014 at 01:24 PM.. Reason: Added QUOTE tags
# 4  
Old 09-17-2014
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:
    1. giving it $date_30_days as a variable,
    2. feeding each log file name to it as one line to be read from standard input,
    3. 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,
    4. lelting your awk script do the date comparisons as well as changing the date format, and
    5. just printing the names of the files that need to be moved?
  2. letting your shell script read the output from the above awk script and performing the moves for the filenames awk prints, and
  3. only printing NO OLD FILES if the awk script does not return the names of any files to move?
# 5  
Old 09-18-2014
If performance is an issue, don't use awk at all.
Code:
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Ubuntu

Shell Scripting , Moving Old file to specific folder

There are files stored like 14.Aug.2014.log, 15.Aug.2014.log etc. in a folder $HOME/logyou 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. ---------- Post updated at 12:30 PM ----------... (3 Replies)
Discussion started by: shajoftaj
3 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Programming

Perl - Moving file based upon filesize in folder

Hi I'm trying to look through a series of directories in A folder, lets just call it A: for example: A/1 A/2 A/3 Etc and I wish to move the files in the folder if they are bigger than a certain size into a structure like below: A/TooBig/1 A/TooSmall/1 A/TooBig/2 A/TooSmall/2... (1 Reply)
Discussion started by: PerlNewbRP
1 Replies

5. Shell Programming and Scripting

Shell Script for moving 3 days old file to Archive Folder

Hi Experts, I have a "Source" folder which may contain some files. I need a shell script which should move all files which are older than 3 days to "Archive" folder. Thanks in Advance... (4 Replies)
Discussion started by: phani333
4 Replies

6. Shell Programming and Scripting

How to copy specific file.txt in specific folder?

hye there... i have a problem to copy file in specific folder that will change the name according to host,time(%m%s) and date(%Y%M%D) example folder name: host_20100531.154101801 this folder name will always change... but i just want to copy the AAA.txt and BBB.txt file.. really need... (17 Replies)
Discussion started by: annetote
17 Replies

7. Shell Programming and Scripting

Moving 100K file to another folder using 1 command

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the source... (6 Replies)
Discussion started by: unx100
6 Replies

8. UNIX for Dummies Questions & Answers

moving file from one folder to another

i have created file in one of the folders on unix UNIX 's36tou -T XYZ /tmp/p400/dataout/ias/AB >/dev/null I am using above command to copy file from one system to unix XYZ is name of file on my system usually this name is very big so i use -T to trim some charaters from name. noe... (1 Reply)
Discussion started by: ajit.yadav83
1 Replies

9. Shell Programming and Scripting

shell script for moving all the file from the same folder

Hi , I need a shell script which basicaly moves all the files from one folder say folder x to folder y and once they are moved to folder y a datetimestamp should be attached to there name for ex file a should be moved to y folder and renamed as a_20081015 (1 Reply)
Discussion started by: viv1
1 Replies

10. Shell Programming and Scripting

moving a file to a new folder and automated ftp

how to move a file to a different folder after an automated FTP . (1 Reply)
Discussion started by: dineshr85
1 Replies
Login or Register to Ask a Question