BASH Scripting Check If a file is being Accessed

I need to use a find script to find files of a certain type then I would like to check if they are being accessed and if they are clear the contents otherwise remove the file. I'm ok with the find part but should I pipe the output to some kind of If statement.
Lico_wAsked:
Who is Participating?
 
woolmilkporcCommented:
Or is it really so that you want to clear open files only?

In this case && is of course correct!

In my version:

find ./ -name "*.log" -type f -mtime +1 | while read file ; do lsof $file >/dev/null && >$file || rm $file; done
0
 
woolmilkporcCommented:
I  assume that this Q is releated to the other one requesting assistance on how to clear a file?

If so, and if you have the lsof utility at hand, you could do:

find ./ -name "*.log" -type f -mtime +1 | while read file ; do lsof $file >/dev/null || >$file; done

lsof returns a "false" returnconde if the file is not open, so we use "||" here.
 
wmp
0
 
Lico_wAuthor Commented:
Ok I think I understand this, we are doing the find, piping it to the while loop, then we're checking if the file is open (lsof $file) then we are copying the contents of a null file to the current file (>/dev/null || >$file).

Please confirm and then I will award points.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
woolmilkporcCommented:
That's quite correct , but "copying the contents of a null file ..." doesn't describe exactly what happens.

>$file just opens a file for output and closes it immediately afterwards, because there is no data to be written to it (as opposed to e.g. echo "This File is Empty!" >$file), thus effectively emptying it.

wmp
0
 
Lico_wAuthor Commented:
Just tested this and it doesn't work. I have logged on with another user and left the file open with vi. I've then logged on as myself and ran the script but it didn't clear the file?
0
 
woolmilkporcCommented:
vi does not keep the file open.

It is read into memory, that's all.
0
 
ckiralCommented:
Hi,

I assume that you want to clear contents of  open files only and then remove if found files are already closed.  

I suggest to write a  little script to make necessary tests on find command's output.
Try this one:
---
#!/bin/bash
#FileCleaner.sh
lsof $1 && > $1 && exit 0;
rm $1
exit 0
---
You can now use this script with your find command as follows:

find ./ -name "*.log" -type f -exec ./FileCleaner.sh '{}' \;

-
As woolmilkporc already mentioned; you can not test it with vi.
In one terminal you can type
cat  > testfile
(It will open a new file handler)
Then type some text in to it. In an  other terminal you should now see your text using command
cat testfile
After that try the find command  with testfile pattern
When command exit; you'll notice that the file is now empty as you requested...

--

0
 
woolmilkporcCommented:
Attention!

lsof returns "1" if the file is not open!

So you should use || instead of &&!

wmp
0
 
ckiralCommented:
i agree;
in the script we clear the content of  the file if it's open..
I don't see any errors here??
---
if file is open then clear the contents and exit...
----
lsof $1 && > $1 && exit 0;
---
So && usage is correct; am i missing something?

0
 
ckiralCommented:
woolmilkporc; in my opininon your version will never evaluate closed file case...
The command will exit as soon as lsof will return false.

Please confirm...
0
 
ckiralCommented:
Please forget my last post about woolmilkporc's version, it will work as expected.
My mistake...
0
 
Lico_wAuthor Commented:
Apologies I've been away for a week, I'm going to test this now and award points for my outstanding questions. Is there a way I can keep a file open so that I can test this? Obviously as you stated you can't use vi but is there another option?
0
 
woolmilkporcCommented:
You could open a file (and empty it if it exists) with

cat > filename

or open an existing file without emptying it (but please be careful - do not destroy a file you need) with

cat >> filename

Terminate "cat" with <Ctrl><C>

wmp
0
 
Lico_wAuthor Commented:
Many thanks for the comments, does exactly what I need it to do and I've also learned a lot!!
0
 
Lico_wAuthor Commented:
One final thing...if I wanted to rm the file i.e. if it ISN'T being accessed how would I do that? Is it just a simple modification on the above or should I post a new question?
0
 
woolmilkporcCommented:
That's what this thing is supposed to do:

find ./ -name "*.log" -type f -mtime +1 | while read file ; do lsof $file >/dev/null && >$file || rm $file; done

The && >$file part means "Empty it if it's being accessed" and the || rm $file part means: remove it if it's not being accessed.

To just remove files not being accessed without the "emptying" part modify like below:

find ./ -name "*.log" -type f -mtime +1 | while read file ; do lsof $file >/dev/null || rm $file; done

wmp



0
 
Lico_wAuthor Commented:
I thought that was the case but seems I had a slight typo preventing the rm bit working, thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.