Shell script help to eliminate files of todays date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script help to eliminate files of todays date
# 1  
Old 04-15-2009
Shell script help to eliminate files of todays date

Hi
I am very new to shell scripting and have written a script (below).
However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate.
Each day the file extension overnight will change to trx, if this fails I want to know.
Basically what I want to do is add to the script to eliminate any files with a TRN extension for todays date, can anyone help me please.
I am using unix version 11.11
thanks in advance. Smilie

Code:
for DIRECTORY in $(awk '{print $1}' /appl/pro/eftids)
  do
    #ls -t|head ${DIRECTORY}/trans1|grep -v "TRZ"|grep -v "TRX$"}
    if [ $(ls -t ${DIRECTORY}/trans1|head|grep -v "TRZ"|grep -v "TRX$"|wc -l) -gt 0 ]
    then
    echo !!LOG ERROR ${DIRECTORY}/trans1 $(ls -t ${DIRECTORY}/trans1|head|grep -v "TRZ"|grep -v "TRX$")
    else
      echo "${DIRECTORY}/trans1 Directory OK"
    fi
  done


Last edited by otheus; 04-17-2009 at 12:38 PM.. Reason: added code tags
# 2  
Old 04-17-2009
Not bad for a first try. first, learn about BB "code" tags. That helps a bit!

Anyway, it turns out there's a UNIX utility to do what you need. It's called "find".
Code:
awk '{ print $1 }' /appl/pro/eftids | 
while read DIRECTORY; do 
  find $DIRECTORY -type f -name "*.TRN" -print 
done > /tmp/found-trn-files.$$

# You can put this inside or outside the above loop. 
if ! [ -s /tmp/found-trn-files.$$ ]; then
  echo '!!LOG ERROR. The following files were not converted:'
  cat /tmp/found-trn-files.$$
fi
# cleanup
rm /tmp/found-trn-files.$$

The other way is to have a loop-within-a-loop, like this;
Code:
awk '{ print $1 }' /appl/pro/eftids | 
while read DIRECTORY; do 
  /bin/ls -1 $DIRECTORY/trans1/ |
  while read FILE ; do 
     if ! echo $FILE |grep '\.TR[XZ]$' &>/dev/null  ; then
        echo '!!LOG ERROR .... ' $FILE in $DIRECTORY
     fi
  done
done


Last edited by otheus; 04-17-2009 at 12:49 PM.. Reason: changed inner if/grep pair
# 3  
Old 04-20-2009
That's fantastic. Thanks for your help and advice. SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (6 Replies)
Discussion started by: ram1228
6 Replies

2. UNIX for Dummies Questions & Answers

Script to keep todays files based on Timestamp

Hi i need to keep todays files based on timestamp and archive the remaining files ex: Managerial_Country_PRD_20150907.csv Managerial_Country_PRD_20150906.csv Managerial_Country_PRD_20150905.csv (2 Replies)
Discussion started by: ram1228
2 Replies

3. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

4. UNIX for Dummies Questions & Answers

Help in script to check file name with todays date

I am trying to include a snippet in my script to check if the file created is having today's date. eg: File name is : ABC.YYYYMMDD-nnn.log The script should check if 'YYYYMMDD' in the above file name matches with today's date. Can you please help me in achieving this. Thanks in advance!! (5 Replies)
Discussion started by: kiran1112
5 Replies

5. Shell Programming and Scripting

Find and copy files based on todays date and search for a particular string

Hi All, I am new to shell srcipting. Problem : I need to write a script which copy the log files from /prod/logs directory based on todays date like (Jul 17) and place it to /home/hzjnr0 directory and then search the copied logfiles for the string "@ending successfully on Thu Jul 17". If... (2 Replies)
Discussion started by: mail.chiranjit
2 Replies

6. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

7. Shell Programming and Scripting

Script to eliminate files .rlogin

Hi guys, I'm try making to script for eliminate files rlogins. path1='/home/*' for i in `cat /etc/passwd |awk -F: '{print $6}'`; do if test "$i" = "$path1"; then echo $i cd $i if ; then echo "$i/.rhosts detectado"|mail -s "rhosts" root ... (14 Replies)
Discussion started by: nena_redbalon
14 Replies

8. Shell Programming and Scripting

Find files having todays date and hostname in its name.

Hello, I am quite new to unix/shell and want to write a script using bash which will process the files. Basically i want to search files having name as "date+hostname+somestring.out" i am using below variables and then will use them in find command :- TODAY_DATE=$('date +%d')... (5 Replies)
Discussion started by: apm
5 Replies

9. Shell Programming and Scripting

Perl Script to execute todays date.

Hi Folks, I have created a script last month to retrive files thru FTP and cronjob was running fine till yesterday. But the naming convention of the daily file is Filename_<date>.xml where date is YYYYMMDD. But today i have received file name as Filename_20110232.xml :( Part of my Perl... (4 Replies)
Discussion started by: Sendhil.Kumaran
4 Replies

10. Linux

Comparing the file drop date with todays date

Daily one file will dropped into this directory. Directory: /opt/app/jt/drop File name: XXXX_<timestamp>.dat.gz I need to write a script which checks whether the file is dropped daily or not. Any idea in the script how can we compare timestamp of the file to today's date?? (3 Replies)
Discussion started by: Rajneel
3 Replies
Login or Register to Ask a Question