Alternate to copy + remove


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Alternate to copy + remove
# 1  
Old 10-12-2015
Alternate to copy + remove

is there any single command for

Code:
cat file1 > file2

Code:
cp /dev/null file1


The original file must retain intact but it should get empty. Actually, the contents needs to be processed by another process and for new entries, the file must remain there with zero records.
# 2  
Old 10-12-2015
Try
Code:
mv file1 file2 && touch file1

# 3  
Old 10-12-2015
I need a single command, can't lose the file placement even for a ns.
# 4  
Old 10-12-2015
The following is faster - while still two commands
Code:
cp file1 file2
>file1

# 5  
Old 10-12-2015
Note, however, that you have a built-in race condition...
If someone opens file1 for appending during the cp and then writes to that file descriptor:
  • the data may be copied into fie2 by the cp,
  • may be cleared by the > /dev/null never to be seen again,
  • or may appear in file1 after it has been cleared by the > /dev/null.
To get rid of this race condition, we need to know more about how processes writing to file1 open the file and how the permissions of processes writing to that file are related to the owner and group of the directory in which the target file resides.

The writers obviously have write permission to the target file. If they also have write permission in the directory in which it resides, the safe thing would be to use:
Code:
mv file1 file2

and have the writers open file1 with the flags:
Code:
fd = open("file1", O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);

guaranteeing (assuming no write errors) that data written to that file descriptor will appear either in file1 or file2.

If you can't do that; you need to block all writers to file1 while it is being rotated.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. HP-UX

Alternate for wget

Hi, Whats the alternate for wget in HP-UX ? (4 Replies)
Discussion started by: mohtashims
4 Replies

2. UNIX for Dummies Questions & Answers

how to copy a file without remove the contents of the target file?

Hello every body, Kindly support me to "copy a file without remove the contents of the target file" Thanks in advance. :) Ahmed Amer Cairo,Egypt (2 Replies)
Discussion started by: ahmedamer12
2 Replies

3. Solaris

Self ssl alternate in Solaris

Instead of using an external Certification Authority CA such as Verisgn, in windows I have been told there is something called self ssl ( The server is its own Certification Authority) In Solaris is there such an alternate? In need it for apache. Much appreciate for any guidence. Thanks (2 Replies)
Discussion started by: Tirmazi
2 Replies

4. UNIX for Dummies Questions & Answers

copy all files and folders and cjange or remove ownership

So tried: cp -r -p test1/ user@machine:///srv/www/vhosts/domain.co.uk/httpdocs/backup/ but this didn't work either :( Anyone able to help with this? Many thanks Mr M (3 Replies)
Discussion started by: misterm
3 Replies

5. Shell Programming and Scripting

alternate lines

Hi, I'm new to Unix. I want to read the all the lines from a text file and write the alternate lines into another file. Please give me a shell script solution. file1 ----- one two three four five six seven newfile(it should contain the alternate lines from the file1) ------- one... (6 Replies)
Discussion started by: pstanand
6 Replies

6. Shell Programming and Scripting

Alternate way for echo.

Hi, Is there any other command echo does. if I am doing this operation for each line in my file. So its taking very long time to process more than 1000 records. Is there any alternative way to write the above if statement (5 Replies)
Discussion started by: senthil_is
5 Replies

7. UNIX for Dummies Questions & Answers

Copy all the files with time stamp and remove header,trailer from file

All, I am new to unix and i have the following requirement. I have file(s) landing into input directory with timestamp, first i want to copy all these files into seperate directory then i want to rename these files without timestamp and also remove header,trailer from that file.. Could... (35 Replies)
Discussion started by: ksrams
35 Replies

8. HP-UX

How to remove alternate boot disk (vg00) in mirror

How do we remove mirror (vg00) in itanium system having 11.23 version. (2 Replies)
Discussion started by: jeelans
2 Replies

9. Shell Programming and Scripting

Copy Files then with option of remove

hi, i encounter a quite a new things for me. lets sat my test.txt modified date is 6th of July 2005. then i execute the following command: "cp test.txt testfolder/test.txt" when i go to execute "ls -l" command... the test.txt modified date become today date! but if i execute: ... (5 Replies)
Discussion started by: maldini
5 Replies
Login or Register to Ask a Question