Before I delete any file in Unix, How can I check no open file handle is pointing to that file?


 
Thread Tools Search this Thread
Operating Systems Solaris Before I delete any file in Unix, How can I check no open file handle is pointing to that file?
# 1  
Old 10-05-2010
Before I delete any file in Unix, How can I check no open file handle is pointing to that file?

I know how to check if any file has a unix process using a file by looking at 'lsof <fullpath/filename>' command.

I think using lsof is very expensive. Also to make it accurate we need to inlcude fullpath of the file.

Is there another command that can tell if a file has a truely active open file handle pointing to it ? or if a file has a runtime PID associated with it?
# 2  
Old 10-05-2010
fuser, which is more commonly available than lsof, does this, putting the pids on stdout and the file names and flags on stderr, so you can get the pid list easily. For example:

Code:
$ for p in $( fuser . 2>/dev/null )
do
 echo
 ps -fp $p
 echo
 ptree $p
done
$


Last edited by DGPickett; 10-05-2010 at 01:12 PM..
# 3  
Old 10-05-2010
Nothing bad happens if you delete a file in use. It just persists on disk until the last thing using it quits, then is deleted for good.

There's no faster way to query the kernel about open files, than querying the kernel about open files. fuser works roughly the same way as lsof, so isn't really a slimmer/better workaround.
# 4  
Old 10-05-2010
If you want to delete a file and not have lost space if some program holds it forever, truncate it first. However, if the program appends with a high file position, all the space is reallocated and written to zeros, so there is some virtue in ensuring nobody has it open to write before you delete it.
# 5  
Old 10-05-2010
Quote:
Originally Posted by DGPickett
However, if the program appends with a high file position, all the space is reallocated and written to zeros
This is slightly incorrect, at least when using UFS or ZFS. If the writing program is seeking to the previous end of the file location, the space from the beginning of the file to that new location isn't filled with zeroes but unallocated, leading to the creation of a sparse file. If you read the unallocated data, the OS will return "virtual" zeroes but they use no space at all on the disk.
# 6  
Old 10-05-2010
Quote:
Originally Posted by jlliagre
This is slightly incorrect, at least when using UFS or ZFS. If the writing program is seeking to the previous end of the file location, the space from the beginning of the file to that new location isn't filled with zeroes but unallocated, leading to the creation of a sparse file. If you read the unallocated data, the OS will return "virtual" zeroes but they use no space at all on the disk.
I was taken in by the zeros! You can close it, and there is no space allocated? I guess it is all done in the allocation storage. It is an interesting resource in the 64 bit world, as you could have a very huge, sparse file with just a few blocks. Consider how handy that is in conjunction with mmap64().
# 7  
Old 10-05-2010
Quote:
Originally Posted by DGPickett
You can close it, and there is no space allocated?
This space isn't allocated whether the file is closed or not.
Quote:
I guess it is all done in the allocation storage. It is an interesting resource in the 64 bit world, as you could have a very huge, sparse file with just a few blocks. Consider how handy that is in conjunction with mmap64().
A simpler approach is to mmap /dev/zero which is kind of an infinite sparse file by design.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to check if a file is open in editor?

Hi there! I'm developing a program that allows the user to open and edit files using both an editor and the terminal. Once the user has finished editing the file an update is sent to the logbook that compares the file before and after it was edited - this can only be done if the file is closed (I... (23 Replies)
Discussion started by: cherryTango
23 Replies

2. Shell Programming and Scripting

To check if a file is open and in use (logs are being written to it)

Hello Experts, I need to write a shell script to check if a file is open and something is being written to it. I want to know how OS handles it. I checked with lsof command but it is not working. For a test I did this. while true; do echo `date` >>abc.txt; done then I checked lsof |... (5 Replies)
Discussion started by: shekhar_4_u
5 Replies

3. UNIX for Dummies Questions & Answers

UNIX command to check if file name ends with .tar OR if the file is a tar file

Hello Team, Would you please help me with a UNIX command that would check if file is a tar file. if we dont have that , can you help me with UNIX command that would check if file ends with .tar Thanks in advance. (10 Replies)
Discussion started by: sanjaydubey2006
10 Replies

4. Shell Programming and Scripting

Check file presence and delete other file

Hello, I have file all_file.txt at the end of process this file all_file.txt should be deleted only if there is no file present in dir /all_file/tmp/ or in it's sub directory. can you please help me with the peace of code for this. Thanks (2 Replies)
Discussion started by: kumar30213
2 Replies

5. Shell Programming and Scripting

Need unix commands to delete records from one file if the same record present in another file...

Need unix commands to delete records from one file if the same record present in another file... just like join ... if the record present in both files.. delete from first file or delete the particular record and write the unmatched records to new file.. tried with grep and while... (6 Replies)
Discussion started by: msathees
6 Replies

6. Shell Programming and Scripting

how to open a file and read a file in UNIX

HI can any one tell me how to open and read a file in UNIX actually i have the following script can you please suggest me on this.. LOG,">$log" or NotifyAdmin "Failed to open file $log";... this is the perl statement.. which opens log file. i want to open same log file in unix Thanks... (5 Replies)
Discussion started by: sravan008
5 Replies

7. Shell Programming and Scripting

avoid open file to check field.

Hi Everyone, # cat a.txt 94,aqqc,62345907, 5,aeec,77, # cat 1.pl #!/usr/bin/perl use strict; use warnings; use Date::Manip; open(my $FA, "/root/a.txt") or die "$!"; while(<$FA>) { chomp; my @tmp=split(/\,/, $_); if (index($tmp, "qq") ne -1) { ... (4 Replies)
Discussion started by: jimmy_y
4 Replies

8. UNIX for Advanced & Expert Users

Unable to delete an open file

I am working on a unix server. I killed all the processes with my id on the machine. After that I tried to delete a file, I got an error:- file not removed.Text File busy. Deletion of directory prompted:- Directory not empty. Can anyone help me regarding this...??? Thanks, Vikas (11 Replies)
Discussion started by: vikasrout
11 Replies

9. UNIX for Dummies Questions & Answers

delete open file

hi, I have a little problem. I deleted a file, which was opened in vim editor. it was bad idea... now "df" still display same space. I kill process vim, but maybe file was controlled or opened by another application. Is any eventuallity to find which process has lock file? Command "ps -ax"... (2 Replies)
Discussion started by: delonism
2 Replies

10. Solaris

Error Message: find: cannot open /: Stale NFS file handle

I am using the "find" command below and it respond with the error message " find: cannot open /: Stale NFS file handle" what does it mean? what can I do about it? Need advice, thanks. below are the command that I enter, the respond and the error message: root@ScripServer:/# find / -name... (1 Reply)
Discussion started by: ezsurf
1 Replies
Login or Register to Ask a Question