Help with Archiving multiple files based on name and date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Archiving multiple files based on name and date
# 15  
Old 08-03-2011
Shankar, this may be of help. You may need to tweak it a bit.

Code:
!/bin/ksh

FEED_DIR=~/tmp
ARCH_DIR=~/tmp/arch
CURR_DIR=~/tmp/curr

for file in $(ls -1 $FEED_DIR/File?_extract*)
do
    file_pfx=$(basename ${file%%_[0-9][0-9]*})

    echo $file_pfx

    [ -e $CURR_DIR/${file_pfx}* ] && mv $CURR_DIR/${file_pfx}* $ARCH_DIR

    mv ${file_pfx}* $CURR_DIR
done

#   Remove files that are 6 days or older, from the archive directory.

find $ARCH_DIR -name 'File?_extract*' -atime +6 -exec rm {} \;

# 16  
Old 08-03-2011
Ingenious, but not robust or in requirement.

He said the app writes them new file to $CURRENT_DIR, and the app is not to be touched. So, at best you have to move the file to /tmp first. That is expensive for big files, as mv becomes cp (twice) for change of file system, endangers the file if /tmp is too full or if unplanned reboot clears /tmp, and will change the mod time stamp.

find -mtime runs on real time, so you may get more or less than 6 files, and on mod time, so if the mod times get messed up, may not remove the right files at the right time.
# 17  
Old 08-03-2011
Good points, DGPickett. I may have misunderstood his requirements.

I am using -atime in the find command, with the assumption that once the file has been processed, it is not accessed again.
# 18  
Old 08-03-2011
Missed that, and never investigated the ramifications of atime. I think every open changes that. ctime might be before mtime, as it reflects non-time inode values. Time stamps are a fluffy thing, so using the file name date is safest, but drat that mdy date.

Staying on the fs with mv also means even if the file is open for write, it can move around without problems. After all, it is just an entry name and inode# in a dir node or two!
# 19  
Old 08-03-2011
DGPickett: When I execute the script it doesn't do anything or throw error.

g.pi: When I execute your script what ever is in the CURRENT goes to the ARCHIVE. But the new file from Source does not move to CURRENT.
Am I missing something?
Code:
for file in $(ls -1 $FEED_DIR/*.*)
do
    file_pfx=$(basename ${file%%_[0-9][0-9]*})

    echo $file_pfx

    [ -e $CURR_DIR/${file_pfx}* ] && mv $CURR_DIR/${file_pfx}* $ARCH_DIR

    mv ${file_pfx}*.* $CURR_DIR
done

#   Remove files that are 6 days or older, from the archive directory.

find $ARCH_DIR -name 'File?_extract*' -atime +6 -exec rm {} \;


Last edited by Franklin52; 08-06-2011 at 04:45 PM.. Reason: Please use code tags for data and code samples, thank you
# 20  
Old 08-03-2011
He assumed feed sir was not curr dir.

Forgot suffix! Fixed rm not to be mod time:
Code:
#!/usr/bin/ksh
 
cd $CURRENT_DIR
 
ls *_[01][0-9][0-3][0-9]20[0-3][0-9].csv | sed '
  s/\(.*\)_\([01][0-9][0-3][0-9]\)20\([0-3][0-9]\)\.csv/\3\2 \1 &/
 ' | sort | while read xxkey file_base file
do
 if [ $file_base = "$last_file_base" ]
 then
  mv last_file $ARCHIVE_DIR # one at a time for simplicity
  while (( 6 < $(
               ls $ARCHIVE_DIR/${file_base}_[01][0-9][0-3][0-9]20[0-3][0-9].csv 2>/dev/null | wc -l
               ) ))
  do
    ls $ARCHIVE_DIR/${file_base}_[01][0-9][0-3][0-9]20[0-3][0-9].csv | sed '
      s/.*_\([01][0-9][0-3][0-9]\)20\([0-3][0-9]\)\.csv/\2\1 &/
     ' | sort | read y x
    rm -f $x
  done
 fi
 
 last_file=$file  last_file_base=$file_base
done

The beauty of scripting is you can try parts on your command line to see where it falls short or goes off track.

Last edited by DGPickett; 08-03-2011 at 05:57 PM..
# 21  
Old 08-03-2011
Adding one more point currently the files always has .CSV as the extension. Will there be a problem if the file extensions are different.
For example if we get files like:
HMIA_CLS_08012011.csv
BAXT_INV_08022011.txt

The file type or name won't change.
On Aug1, if we get "HMIA_CLS_08012011.csv" then the next day we will be getting only
"HMIA_CLS_08022011.csv"
Similarly for "BAXT_INV_08022011.txt" the next file will be "BAXT_INV_08032011.txt"

Its always the MMDDYYYY before .txt or .csv changes.

Thanks
Shankar
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

2. UNIX for Dummies Questions & Answers

Find the count of files by last created date based on the given date range

My unix version is IBM AIX Version 6.1 I tried google my requirement and found the below answer, find . -newermt “2012-06-15 08:13" ! -newermt “2012-06-15 18:20" But newer command is not working in AIX version 6.1 unix I have given my requirement below: Input: atr files: ... (1 Reply)
Discussion started by: yuvaa27
1 Replies

3. Shell Programming and Scripting

Divide an EBCDIC files into multiple files based on value at 45-46 bytes

Hi All, I do have an EBCDIC file sent from the z/os , this file has records with different record types in it, the type of record is identified by bytes 45-46 like value 12 has employee record value 14 has salaray record and etc.... we do now want to split the big ebcdic file into multiple... (3 Replies)
Discussion started by: okkadu
3 Replies

4. Shell Programming and Scripting

Archiving assistance needed date to month

We have year folder say in a path /opt/informat/Archive a folder 2012. And in the same folder /opt/informat/Archive we have different folders month based, like 201210, 201211, 201212, this have data for each month, ie files. Now time to time i need to move the monthly folders to the main folder... (1 Reply)
Discussion started by: raghavraok
1 Replies

5. UNIX for Dummies Questions & Answers

Remove files based on date

Hello team, I have a number of files in a folder which are dated yesterday and today.Can i remove all the files which i created today based on date?? is there any syntax for this ?? (1 Reply)
Discussion started by: kanakaraju
1 Replies

6. Shell Programming and Scripting

Copy files based on date

Hi all i am having so many files in my directory.Is there any option to copy files based on date. example i am having file this -rw-rw-r-- 1 ram user 1 Feb 2 17:12 abc -rw-rw-r-- 1 ram user 1 Feb 2 17:12 bnw -rwxrwxr-x 1 ram user 21122 Feb 4... (3 Replies)
Discussion started by: suryanarayana
3 Replies

7. Shell Programming and Scripting

Need script to select multiple files from archive directory based on the date range

hi all, here is the description to my problem. input parameters: $date1 & $date2 based on the range i need to select the archived files from the archived directory and moved them in to working directory. can u please help me in writing the code to select the multiple files based on the... (3 Replies)
Discussion started by: bbc17484
3 Replies

8. Shell Programming and Scripting

Get the newest files based on date

Hello friends, I'm learning to script, and I need help. How can I get the latest/newest files based on date? the format is as following: Feb 07 19:25 TESTPWD_file_1vk6pn40_19519_1 Feb 07 19:46 TESTPWD_file_1uk6pn40_19518_2 Feb 07 19:47 TESTPWD_file_20k6pn40_19520_2 Feb 07 19:56... (5 Replies)
Discussion started by: Beginer0705
5 Replies

9. Shell Programming and Scripting

Count of files based on date?

Hi Friends, Can anyone help me with this: To get the count of files that are existing in a directory created on a perticular date like in the example (01/08) .(having same pattern for the filename) ex: FileName Creted Date FILE001 01/08/2007 FILE005 ... (6 Replies)
Discussion started by: sbasetty
6 Replies

10. UNIX for Dummies Questions & Answers

Remove files based on date

I am trying to write a shell script that will remove files in a directory based on the date. For instance, remove all files older than yesterday. Any ideas? (4 Replies)
Discussion started by: hshapiro
4 Replies
Login or Register to Ask a Question