2

I am new to Ubuntu and I do not have alot of experience with Linux/Unix, so please bear with me while I try to get through this.

I have a camera system that records footage and takes screen shots and stores them in a folder. The problem is that the new files do not overwrite the older files, so the file system fills up and all new files are discarded.

So I installed Scheduled Tasks in the Ubuntu GUI and created a task to run every day. Here is the task:

find /media/dvr/* -type f -mtime 0 -exec rm -rf {} \; 

I have the -mtime set to 0 for testing purposes, it will be set to 7 if I can get this to work.

The problem I have is there is a lost+found folder in the /dvr folder. When it gets to that folder it says I do not have permission to view it and it never finishes its search. I have used sudo to get in and change the ownership and rwx permissions, but that still doesn't help.

Is there a way to accomplish what I want to do by using find? Is there a more efficent way of cleaning out the /dvr/ folder?

Like I said, new to this and trying to get better. Any help would be greatly appreciated!

César
  • 807
  • 2
  • 13
  • 30
Nick
  • 21
  • 1
  • 2

3 Answers3

4

Just do:

find /media/drv -name lost+found -prune -o -type f -exec echo '{}' ';'

In general it is a very very bad idea to redirect stderr to /dev/null. The whole point of stderr is to show unexpected errors. If you are expecting a certain error then construct your command to avoid it.

Jay
  • 41
  • 2
  • find has the `-print` option so you don't have to exec echo there. It should also be noted that the default option is to print, but be careful because of `-prune` - when that is in place, an explicit `-print` is required, otherwise it doesn't actually prune anything from the output, it prints everything that matched. – Josip Rodin Sep 30 '15 at 12:56
2

The find command does not exit on permission problems as far as I know, it may just not be finding any files for a different reason. What I can suggest is to eliminate stderr so that you don't get misleading messages in the output, and to test the command using echo replacing rm -rf, i.e.,

find /media/drv/ -type f -mtime 7 -exec echo {} \; 2> /dev/null

This should list the files that would be removed in your final version with rm -rf. If you really want to skip the lost+found directory (which should not be necessary), use:

find /media/drv/ -type f -mtime 7 -not -wholename "*lost+found*" -exec echo {} \; 2> /dev/null
pablomme
  • 5,440
  • 2
  • 24
  • 31
  • There are ample reasons people may want to skip over lost+found. For example, you have a partition where an unprivileged user is allowed to save files (in the partition root), and a cron job that wants to clean those files up. You obviously want to run the cron job as the same unprivileged user, but it will fail to descend into the lost+found directory and produce spurious errors. Eliminating all stderr output because of that one specific circumstance is not a good idea. – Josip Rodin Sep 29 '15 at 17:25
-2

Setting the permissions correctly should do it. What does ls -ld /media/dvr/lost+found say? Also drop the * at the end of /media/dvr/. You want find to start its search in /media/dvr/, and when you add the *, the shell replaces that string with the name of every file it finds in /media/dvr/.

You also can just delete the lost+found directory.

psusi
  • 37,033
  • 2
  • 68
  • 106
  • 2
    I think it is probably better to use `-not` option on `find` than to delete the lost+found directory, it will just get recreated when fsck runs anyway. See http://serverfault.com/questions/6753/what-happens-if-i-delete-lostfound – Richard Holloway Jan 20 '12 at 14:44