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)
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:
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:
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..
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 ?
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)
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)
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)
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)
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)
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)
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)
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)