0

I'm trying to archive some older files, but don't want to touch all the files which are currently in use. What's the best way to ask find to exclude "in use" files?

Right now I'm working on this command, but I'm asking if there's something better (my find expression is slightly more complicated, but take it as an example)

find /tmp/ -type f \( -exec fuser -s {} \; -o -print \)
MaxChinni
  • 101
  • 2
  • Echo `lsof` output to a file or environment variable. Archive only those files which are not in the `lsof` output. – boardrider Jul 20 '15 at 12:01
  • @boardrider thank you for your suggestion, but your solution seems more complex then mine. It involves at least a `grep` filter between the `find` command and the archive one. In my solution I could perform any command through a `-exec` expression inside the `find`. – MaxChinni Jul 20 '15 at 21:33
  • I think your solution gives the files `in use` while your question asked for files `not in use.` – boardrider Jul 21 '15 at 10:59
  • @boardrider no it doesn't. The `-o` means if a file is in use, do nothing, but if a file is not in use (`fuser` returns with `1`), then go on with the expression which is `-print` – MaxChinni Jul 21 '15 at 14:22
  • I stand corrected. – boardrider Jul 22 '15 at 11:11

1 Answers1

0

I think the answer to is there anything slightly less complicated? is no.

I tried your example and it did the job...

$ find . -type f \( -exec fuser -s {} \; -o -print \)
./a_temp_file
./another_temp_file
$ ( sleep 10  ) > file_open_for_10_secs &
[2] 31969
$ find . -type f \( -exec fuser -s {} \; -o -print \)
./a_temp_file
./another_temp_file
$ 
[2]+  Done                    ( sleep 10 ) > file_open_for_10_secs
$ find . -type f \( -exec fuser -s {} \; -o -print \)
./file_open_for_10_secs
./a_temp_file
./another_temp_file

Obviously you should probably also look for files last written to over a certain length of time ago as you don't want to muck about with temporary files that are in use as part of a script, but otherwise I don't see a simpler solution.

MoopyGlue
  • 1
  • 2