Help with Logic - Copying the same files by 2 Linux scripts to same directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Logic - Copying the same files by 2 Linux scripts to same directory
# 1  
Old 08-08-2012
Help with Logic - Copying the same files by 2 Linux scripts to same directory

Hi All,

Good Morning !

Can anyone help me understand the following scenario.

Scenario 1 : 2 LINUX Scripts(Process 1 & 2) are trying to copy the same file (lets call it FileA.txt) to the same directory (lets say, /home/abc/zyz) at the same time. If both copy is happening at the same time, will the 2 LINUX Scripts abort ?

Scenario 2 : Process 1 copied the fileA.txt to the directory & this file is being read by another script. Process 2 tries to copy the file again while it is being read by Process 1.

What will happen in these 2 scenarios ?

Thanks Much for your inputs,
Freddie
# 2  
Old 08-08-2012
1. No, they will not abort. In case of actual disk block copying the target will contain garbage.

2. The same as 1. The reading proces will get garbage.

In some situations it could "work" - if all files are on the same filesystem and the blocks are
shared between the files.

With short files it could work most of the time - as in the target having the same content as
the source, you cannot rely on it though.

Last edited by hipe; 08-08-2012 at 12:58 PM.. Reason: added the last sentence
# 3  
Old 08-13-2012
Thanks for the reply.

Is there a way to put a validation in the linux script which will do the following,

Quote:
find $sourcepath/*datevalue_filename* -type f -newer /tmp/start -not -newer /tmp/end > $destinationpath/wcc_cpmg_nt_bus_gs.txt
In the below FOR Loop, is there a way to put a logic to check whether the file *datevalue_filename* already exists in the $destinationpath.

Quote:
for name in `cat $destinationpath/wcc_cpmg_nt_bus_gs.txt`
do
cp $name $COMMON_INBOX
echo $name;
done
If the date_filename is available, we shouldnt copy the file again.

Thanks Much,
Freddie
# 4  
Old 08-13-2012
Because the "find" syntax is incorrect on my computer it not possible to guess your intention. The first parameter to "find" is usually a Directory Name.

Which files are you trying to copy ... and to where?

What is/are the actual name/names of "*datevalue_filename*" ? Is it a directory or a file?

Last edited by methyl; 08-13-2012 at 03:25 PM..
# 5  
Old 08-13-2012
Regarding two scripts potentially writing files of the same name to the same target directory.

Conventional technique is for each script to contain identical code to process a semaphore file in some mutally agreed directory (not the target directory) which means "target directory busy". Before accessing the target directory, check whether the semaphore file exists and either back off or exit. If the semaphore file does not exist, create the semaphore file, do your process on the target directory, then delete the semaphore file.

You may find a sempahore file referred to as a flag file or and interlock file. The meaning is the same.
# 6  
Old 08-20-2012
Hi Methyl/Hipe,

Thanks for your reply.

I am currently doing a ls -l command based on the date value present in the file name & writing those filenames(into file_per_chg_indx.txt) that comes under 2 date range. (This is a while loop)

Then, using the below FOR loop, i move the files from one directory to another,

Quote:
for name in `cat $COMMON_TMP/file_per_chg_indx.txt`
do
cp $name $COMMON_INBOX
echo $name;
done
Is there a way I can put a logic inside this FOR loop that will check for the file availability in the $COMMON_INBOX & if it present then skip the copy, else copy the file?

Thanks much in advance.
Freddie
# 7  
Old 08-20-2012
Quote:
I am currently doing a ls -l command based on the date value present in the file name & writing those filenames(into file_per_chg_indx.txt) that comes under 2 date range.
Please post the code which does this process. It is virtually impossible to do this based on the output from ls -l. The find command posted earlier is really dubious.


Quote:
for name in `cat $COMMON_TMP/file_per_chg_indx.txt`
do
cp $name $COMMON_INBOX
echo $name;
done
This is a very dubious script construct which will fail if any filename contains a space character or if there is a large number of files. I'd love to know where it comes from because I have never seen it in any scripting course, but it crops up frequently on unix.com.


Anyway, to answer your question and to correct some of the problems,
Code:
cat "${COMMON_TMP}/file_per_chg_indx.txt" | while read filename
do
      filebase=`basename "${filename}"`
      target="${COMMON_INBOX}/${filebase}" 
      if [ -f "${target}" ]
      then
              echo "Target file already exists: ${target}"  
      else
               cp -p "${filename}" "${target}"
               echo "Copied: ${filename} to ${target}"
      fi
done

Ps. Note that code is indented and that all string variables are within double quote characters.
Pps. Please just once post what Shell you are using. We might be able to eliminate the cat command in the script.

Last edited by methyl; 08-20-2012 at 08:26 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files from parent directory only

Hello, Please can someone assist on a issue I am having. I want to find specific files in the parent directory only that have been modified over the last 2 days and copy them to another location. NOTE: The version of AIX I am using does not have MAXDEPTH. I have currently written the... (3 Replies)
Discussion started by: Dolph
3 Replies

2. Shell Programming and Scripting

Copying xml files to a chosen directory

I want to determine if there's any xml files exist & if so copy each xml to that directory. Is my code correct for doing that? I can't test my script yet. Somebody please explain it to me please? if ]; then #print "No Status type XML files received from server in $DIRECTORY" else for... (2 Replies)
Discussion started by: emc^24sho
2 Replies

3. Shell Programming and Scripting

Copying files to a directory

Hi, I am newbie to unix scripting, need a help in the doubt i have. It is " when files are copied to a directory using cp command, they are arranged by default according to the file name, is there anyway where the files that are copied be arranged with respect to their size, with using the sort... (5 Replies)
Discussion started by: pundalik
5 Replies

4. UNIX for Dummies Questions & Answers

Help in copying multiple files from th directory

Hey Guys, I have directory with thousands of files, I need to copy only march data. can any one please Help.......... Thanks, Uttam N (4 Replies)
Discussion started by: Uttamnsd
4 Replies

5. Shell Programming and Scripting

Find a directory and copying files to it

Hi all, wondering if someone would be able to help with a little query I have. I currently have a script which copies everything in a specific directory to a number of other directories depending on a variable. The problem I have is the destination directories vary (in and incoming directories)... (3 Replies)
Discussion started by: JayC89
3 Replies

6. Shell Programming and Scripting

Copying files to a directory

Hi I have a few questions. I am trying to copy a file to a directory. I need to copy files that do not end in numbers, for example, into a directory. This is what I tried so far. cp filename directorytowhereIwannacopy but it says it can't copy to a directory. I need to copy many files into one... (2 Replies)
Discussion started by: #moveon
2 Replies

7. Shell Programming and Scripting

Copying files from one directory into another.

Could someone please tell me if there is a command similar to head or tail that can be used on directories. I want to select a given number of files from a directory and copy them into another directory. But I found out I can't use head as it doesn't (or I don't know how yet!) work on directories.... (4 Replies)
Discussion started by: Krush187
4 Replies

8. UNIX for Dummies Questions & Answers

Help with cron: copying all files in a directory to another...

Here's what I have thus far: cp -r /home/mydom/public_html/products/Widget/ /home/mydom/public_html/ This works fine but suppose the folder in public_html has a different name (Main_Widget). The cron above needs to copy the files within the folder (Widget) instead of the folder itself. How... (1 Reply)
Discussion started by: ashleyquick
1 Replies

9. UNIX for Dummies Questions & Answers

Copying multiple files with "If Then Else" logic

I need some suggestions on how to write the code to copy multiple files rather than duplicating the code multiple times. Example: I have four files that need to go throught this logic in the same way. Do I have to duplicate this chunk of code four times or can I built the logic in the same set... (2 Replies)
Discussion started by: madhunk
2 Replies

10. UNIX for Dummies Questions & Answers

Copying files from one directory to Other

Hi UNIX Gurus, Could please help me out regarding following situation. I am copying some files from one directory to other directotry using following command. cp /var/tmp/*date*.gz /var/tmp/user/ Problem: Once the copy has completed, I need to check whether all the files (including... (3 Replies)
Discussion started by: satishkeshetty
3 Replies
Login or Register to Ask a Question