Script to move files to a directory according to date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to move files to a directory according to date
# 1  
Old 08-25-2011
Script to move files to a directory according to date

hi all,

here is the description to my problem.

input parameter: $date1

based on the date i need to select three files starting with audit.log* based on its modified date, a date before, a date after(if its exists). We need to compare the input date to modified date of the file. And then move these three files to a directory.

can u please help me in writing the code to select the multiple files based on the date input.

An example of how a ls -lrt looks like.

-rwx--r--r oracle oinstall 4354334 Aug 24 10:13 ois_audit.log.20110824.3445543.9090899
When i do a ls --full-time

-rw-r--r-- 1 oracle oinstall 52428553 2011-08-16 15:13:44.000000000 -0700 ois_audit.log.20110816.221344.336078

i need it urgently for the delivery of the code.

Any help is highly appreciated.


Thanks,
Chandu
# 2  
Old 08-26-2011
The first question is, which format $date1 has. It needs to be in seconds-since-epoch.
If it is a string, convert it with
Code:
date1=`date -d "$date1" +%s`

Then
Code:
export date1
files_to_save=`ls -ltr --time-style=+%s | awk 'function abs(x){return x<0 ? -x : x}BEGIN{date1=ENVIRON["date1"]}{print abs($(NF-1)-date1)"\t"$NF}' | sort -n -k1,1 | head -3 | cut -f 2`;

# 3  
Old 08-26-2011
I can give date1 in any format i like. I can mention it as MMDDYYYY
Can you explain how the code works...
do i have to save the first code in a separate file called date1 and export it?
Also it does not have a (date1+1)next day.

Thanks for your response.
# 4  
Old 08-28-2011
Proposal: Give date1 as "YYYY-MM-DD HH:MM". The script would then be called as:
Code:
./myscript.sh "YYYY-MM-DD HH:MM"

(if such precision not necessary, simply drop the HH:MM) and contains:
Code:
export date1=`date -d "$1" +%s`
files_to_save=`ls -ltr --time-style=+%s | \
awk 'function abs(x){return x<0 ? -x : x}BEGIN{date1=ENVIRON["date1"]}{print abs($(NF-1)-date1)"\t"$NF}' | \
sort -n -k1,1 | head -n 3 | cut -f 2`

What it does is to convert the date argument ($1) to seconds-since-epoch format
pass it as environment variable date1 inside the awk script,
which receives the output from the ls command as standard input.
The second-to-last column contains the file modification time as seconds-since-epoch
(epoch = 1970-01-01 00:00 GMT), the last column contains the filename.
These two columns are taken, from the timestamp the absolute difference
to your date argument (converted to seconds-since-epoch) is calculated,
so first column contains this absolute time difference, the second one the filename.
Then it is numnerically sorted according to the first column, i.e. the filenames with
the three smallest time differences appear in the first three lines, which are taken
by "head -n 3" and then only the second column of these three lines is taken
by "cut" to stay only with the filenames.
Finally you may add the following two lines to the script
in order to create a directory with a unique name and copy the files to it:
Code:
mkdir -p $date1
cp $files_to_save $date1


Last edited by hfreyer; 08-28-2011 at 12:44 PM..
This User Gave Thanks to hfreyer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

2. Shell Programming and Scripting

How Create new directory and move files to that directory.?

Hi All, We have main directory called "head" under this we have several sub directories and under these directories we have sub directories. My requirement is I have to find the SQL files which are having the string "procedure" under "head" directory and sub directories as well. And create... (14 Replies)
Discussion started by: ROCK_PLSQL
14 Replies

3. UNIX for Dummies Questions & Answers

How to move gz files from one source directory to destination directory?

Hi All, Daily i am doing the house keeping in one of my server and manually moving the files which were older than 90 days and moving to destination folder. using the find command . Could you please assist me how to put the automation using the shell script . ... (11 Replies)
Discussion started by: venkat918
11 Replies

4. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

5. UNIX for Dummies Questions & Answers

Zip all files in a directory and move to another directory

Hi, need to zip all files in a directory and move to another directory after the zip.. i am using this one but didnt help me... zip -r my_proj_`date +%Y%m%d%H%MS`.zip /path/my_proj mv in_proj_`date +%Y%m%d%H%M%S`.zip /path/source/ i am trying to zip all the files in my_proj... (0 Replies)
Discussion started by: dssyadav
0 Replies

6. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

7. Emergency UNIX and Linux Support

How to move files from a directory which falls between Date Range?

Hi All, I am trying to to move files from a directory to another which falls from Current day - 7 days. The files are in zipped format with dates appended on it. Can you pls help me as this came as a immediate change before the production Release planned next week. Pls let me know if... (11 Replies)
Discussion started by: dsfreddie
11 Replies

8. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

9. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies
Login or Register to Ask a Question