Help in Logic - How to move files based on a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in Logic - How to move files based on a condition
# 1  
Old 07-27-2012
Help in Logic - How to move files based on a condition

Hi All,

I have 2 LINUX scripts (Lets say ScriptA & ScriptB) which copies files (lets say 'date_abc.txt , it goes to a while loop & checks for the files between 2 date varialbles) to directory X. (as Shown Below)

Quote:
while [ $enddate -gt $startdate ]
do
enddate=`date -d "$enddate 1 day ago" "+%Y%m%d"`
ls -l $COMMON_TMP/*_abc.txt | grep $enddate | awk '{print $9}' >> $COMMON_TMP/filenames.txt
done
for name in `cat $COMMON_TMP/filenames.txt`
do
mv $name $COMMON_INBOX
echo $name;
done
These 2 scripts are called from 2 different Autosys schedules & runs at the same time.(The above code piece is available in both scripts). I need to put a logic where it checks if one script already moved the file, the 2nd one shouldnt try to move it. (both the scripts moves the files to the same directory)

Any thoughts will be really helpful.

Thanks Much
Freddie
# 2  
Old 07-27-2012
That sort of checking is going to be impossible to do 100% reliably. In the instant after checking a file, the other program may move it anyway.

I'd try to prevent the two code segments from running simultaneously instead. The mkfifo command will fail if the file already exists, so you can use it to check who gets there first:

Code:
while ! mkfifo /tmp/lock-fifo 2>/dev/null
do
        sleep 1
done

# Code segment you don't want programs to run at the same time

rm /tmp/lock-fifo

There's many other problems with your script as well:

Useless Use of Cat -- the shell is quite capable of reading files by itself.
Useless Use of Backticks -- using backticks on open-ended lists is an invitation to disaster.
Useless Use of ls *
Useless Use of grep | awk -- awk is an entire programming language which can do much more than 'print $5'. If you're using it, you might as well replace the pipe chain with it.

I'd rewrite it as so:

Code:
# Wait your turn
while ! mkfifo /tmp/lock-fifo 2>/dev/null
do
        sleep 1
done

trap "rm -f /tmp/lock-fifo 2>/dev/null" EXIT # Remove the fifo even if the program gets killed

while [ $enddate -gt $startdate ]
do
        enddate=`date -d "$enddate 1 day ago" "+%Y%m%d"`
        ls -l $COMMON_TMP | awk '($0 ~ ENDDATE) && /_abc[.]txt$/ { print $9 }' ENDDATE="$enddate"
done | while read FILENAME
do
        echo mv $FILENAME $COMMON_INBOX
        echo $FILENAME
done > $COMMON_TMP/filenames.txt

rm -f /tmp/lock-fifo

It may be possible to replace your entire loop with a single ls and a > < string comparison, if the date is in the filename itself.

Last edited by Corona688; 07-27-2012 at 01:24 PM..
# 3  
Old 07-30-2012
Thanks for the below code suggestions. I am currently revisiting my code to put the fixes you provided.

Pls note that the dates are available in the filename itself. Can you pls let me know how to replace the entire loop with a single ls and a > < string comparison ?

Thanks Much Again,
Freddie
# 4  
Old 07-30-2012
if files are 20120730_abc.txt then extract the date part and use math, rather than lots of calls to date and ls. this is for bash.

Code:
for f in "$COMMON_TMP"/*_abc.txt; do
        d=${f%%_*}
        (( d <= start && d >= end )) || continue
        mv "$f" "$COMMON_INBOX"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Move files with a certain suffix based on how many files are in another folder

Hello, First time poster. I am looking for a way to script or program the process of moving files from one folder to another, automatically, based on the count of files in the destination folder. I was thinking a shell script would work, but am open to the suggestions of the experts... (6 Replies)
Discussion started by: comtech
6 Replies

2. Shell Programming and Scripting

Move files based on status

Hi UNIX Experts, Kindly help me in this requirement where i want to move files based on its status. For Eg: I have 3 dir's --FileBox Dir this directory will have all the files which i need to process (in informatica ETL Tool) --Succeeded Dir this directory will have files which... (4 Replies)
Discussion started by: kumarinfa
4 Replies

3. UNIX for Dummies Questions & Answers

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. #!/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 ##... (14 Replies)
Discussion started by: awais290
14 Replies

4. Shell Programming and Scripting

Move files to another directory based on name

Hi Folks, I have different type of file in my current directory. From my current directory i need to move the file which is start with csp_rules and if the file is having the string payg , then I need to move all this files to another directory /output/record. Please help me how to do this? ... (3 Replies)
Discussion started by: suresh01_apk
3 Replies

5. Shell Programming and Scripting

Move input file based on condition

Hello, I have File1 in a directory A, a File2 in a directory B. If the File2 is not empty Then I have to move File1 from directory A to a directory archive Else no action. Is it possible to do this from one command line? Thank you in advance for your answers. Madi (2 Replies)
Discussion started by: AngelMady
2 Replies

6. Shell Programming and Scripting

Need script to move files based on name

Hi folks, I'm new here and appreciate greatly any help. I have a bunch of files that need be moved and renamed. Fortunately, they are all in sequence... Present filename and path: /.catalog1/t76038_842-01 Move to: /.catalog1/76038-01 So, we need to drop the... (8 Replies)
Discussion started by: axslinger
8 Replies

7. Shell Programming and Scripting

Merging of all files based on a condition

Hi Friends, I am new to UNIX. I need to merge all the files(to FINAL.txt) in single directory based one condition. Out of all the files one of file will have specific value like :GF01: at any where in the file. so the file which is having :GF01: should be appended at the last. EX:... (5 Replies)
Discussion started by: arund_01
5 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Move files based on year

Hi All, I have a directory which has crores of files since from 2003. I want to move ony the 2003 files to another directory. Please help (9 Replies)
Discussion started by: IHK
9 Replies

10. UNIX for Dummies Questions & Answers

Move files based on year

I have a directory which has crores of files since from 2003. I want to move only the 2003 files to another directory. Please help in doing this. Thanks (1 Reply)
Discussion started by: IHK
1 Replies
Login or Register to Ask a Question