moving files based on condition


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers moving files based on condition
# 1  
Old 02-29-2012
moving files based on condition

hi

i have to move files and send an email and attached the bad files to inform the developer about that.

Code:
#!/bin/ksh
BASE_DIR=/data/SrcFiles
cd $BASE_DIR
## finding the files from work directory which are changed in 1 day
find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt
##
csvok() 
{
awk -F, 'NR==1{n=NF} n!=NF{b=1;exit} END{exit b}' "$1"
}
## Loop thru all the file names
while read line
do
FN=`basename $line`
if csvok "$FN"; then
echo "$FN" >> /home/mydir/goodfile.txt
else
echo "$FN" >> /home/mydir/badfile.txt
##-- here we have to send the email with attachment of all the bad files which is listed in badfile.txt. 
fi
done < /home/mydir/flist.txt

will be highly appericiated. thanks

Last edited by Scott; 02-29-2012 at 02:41 PM.. Reason: Code tags, please...
# 2  
Old 02-29-2012
Code:
sendmail destuser@host <<EOF
To:  destuser@host
From: srcuser@host
Subject:  The owls are not what they seem

Seriously.
$(sed 's/$/\r/' < filename | uuencode attachment_name.txt)
EOF

The sed is so UNIX text files can open in Windows email clients.
# 3  
Old 02-29-2012
The creation of your file /home/mydir/badfile.txt

will not be finished until you are done with the "while" loop.

so maybe instead of
Code:
...
echo "$FN" >> /home/mydir/badfile.txt
##-- here we have to send the email with attachment of all the bad files which is listed in badfile.txt. 
  fi
done < /home/mydir/flist.txt



You should considere

Code:
echo "$FN" >> /home/mydir/badfile.txt
fi
done < /home/mydir/flist.txt
##-- here we have to send the email with attachment of all the bad files which is listed in badfile.txt

does it make sens ?
# 4  
Old 02-29-2012
hi ctsgnb

you mean to say i should send the files after come out from the condition done.
# 5  
Old 02-29-2012
That's exactly what he's saying. If you do it in the loop you'll send 99 different emails for 99 different bad files. email 1 will have the first file, email 2 will have the first two, email 3 will have the first three, etc.
# 6  
Old 02-29-2012
@Corona : exactly, thx

@awais290
I am not sure about what you are trying to do, but if you are willing to loop over the badfile.txt to process every entry in it (to send the listed entry by mail, or do whatever you need), it would be better to scan it once its creation is finished.
# 7  
Old 02-29-2012
hi coronna688

thanks for your reply. we need to send whatever the files which is listed in the badfile.txt.. get that files from the source and attach it and send it.

here in this case source will be BASE_DIR=/data/SrcFiles
we have a badfile.txt which only shows us the names of the badfiles. now here we have to read the badfile.txt first and then go to /data/SrcFiles and based on that name picks the files and send it to the developer. hope i make sense to you.

---------- Post updated at 11:50 AM ---------- Previous update was at 11:47 AM ----------

yeh i got it. then how can i should edit my code with the new requirment.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving old files based on pattern

Hi all I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location. Is there any straightforward way of doing this ? One of ways I... (1 Reply)
Discussion started by: sthapa
1 Replies

2. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

3. Shell Programming and Scripting

Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: filesize=$(wc -c < logfile.log) if then echo "is greater than 100M" else echo "is less than 100M" fi I'm sure there's something... (2 Replies)
Discussion started by: Flakman
2 Replies

4. Shell Programming and Scripting

Moving files based on file creation

Hi, I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir). Simply, if file is olderthan one month move it from source1 dir to BKP1 dir. My file names doesn't have... (7 Replies)
Discussion started by: karumudi7
7 Replies

5. UNIX for Dummies Questions & Answers

Script moving files based on date

Hi, I need a script that moves files based on date to a folder. The folder should be created based on file date. Example is : Date file name ----- -------- Oct 08 07:39 10112012_073952.xls Oct 09 07:39 10112012_073952.xls Oct 10 07:39 ... (6 Replies)
Discussion started by: rockingvj
6 Replies

6. UNIX for Advanced & Expert Users

Moving multiple files based on the pattern

I want to search for a particular file name patterns and move them to a specific folder, is it possible to do it with awk or sed? (1 Reply)
Discussion started by: rudoraj
1 Replies

7. Shell Programming and Scripting

How to compare 2 files & get only few columns based on a condition related to both files?

Hiiiii friends I have 2 files which contains huge data & few lines of it are as shown below File1: b.dat(which has 21 columns) SSR 1976 8 12 13 10 44.00 39.0700 70.7800 7.0 0 0.00 0 2.78 0.00 0.00 0 0.00 2.78 0 NULL ISC 1976 8 12 22 32 37.39 36.2942 70.7338... (6 Replies)
Discussion started by: reva
6 Replies

8. Shell Programming and Scripting

Moving the files based on count and time.

Hi, I have a requirement ,let us say 1000 files needs to be transferred in an hour from one path to another path and if the files (1000 files) are transferred within an hour ( say 40 mins), then the process should remain idle for the remaining time ( 20 mins). (3 Replies)
Discussion started by: Asaikarthik
3 Replies

9. Shell Programming and Scripting

Moving file to directory based on condition.

Can any one help me to correct following script. I have 2 directories DropZone and ProcessZone. File pattern is *VEHDESCSUM*. Finding the 'no of files' in DropZone directory using ls *VEHDESCSUM* |wc -l If DropZone has more than one file or 0 files then exit 1 If DropZone has one file then... (2 Replies)
Discussion started by: ramanagh
2 Replies

10. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies
Login or Register to Ask a Question