bitxo
Posts: 2
Joined: Thu May 25, 2017 2:45 pm

deleting files

Thu May 25, 2017 2:54 pm

hi guys,
i'm trying to delete all files that a survellance camera generates on my pi. All those files are being moved to a tmp folder and i want to delete them when the folder reach 4gb. i don't want to delete those files older than an specified time of creation. any help?
thxs

jahboater
Posts: 5926
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: deleting files

Thu May 25, 2017 7:30 pm

Look at opendir(), readdir(), stat() (see "man opendir" for example and man fstat) also statvfs()
Then remove() or unlink()
Finish with closedir()
Last edited by jahboater on Thu May 25, 2017 7:44 pm, edited 1 time in total.

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: deleting files

Thu May 25, 2017 7:39 pm

jahboater wrote:Look at opendir(), readdir(), stat() (see "man opendir" for example and man fstat)
Then remove() or unlink()
I note that this is not the C/C++ forum, so it is likely that we're not looking for a C-level solution.

Instead, the OP should be looking at "man find" and going from there.

Note that "find" implements a "little language" that allows you to specify which files you want to perform an action upon. Once you get "find" to display the right set of names, then you can pipe the output of "find" into "xargs rm".
If this post appears in the wrong forums category, my apologies.

jahboater
Posts: 5926
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: deleting files

Thu May 25, 2017 7:47 pm

It is though a "General Programming" discussion, so I assumed some sort of programming language might be in order - and C is an obvious choice.
I do agree that "find" is entirely appropriate too!

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: deleting files

Thu May 25, 2017 7:51 pm

jahboater wrote:It is though a "General Programming" discussion, so I assumed some sort of programming language might be in order - and C is an obvious choice.
I do agree that "find" is entirely appropriate too!
I get that, but shell is pretty much the obvious language choice for this task.
If this post appears in the wrong forums category, my apologies.

bitxo
Posts: 2
Joined: Thu May 25, 2017 2:45 pm

Re: deleting files

Sat May 27, 2017 3:15 pm

thanks guys, i was thinking something like:

find -type f -size 4gb /media/usb/tmp -exec rm -rf /media/usb/tmp/*

but it doesn't work.

i want to delete the content of that folder only when it reaches 4gb

....

Martin Frezman
Posts: 1009
Joined: Mon Oct 31, 2016 10:05 am

Re: deleting files

Sat May 27, 2017 3:50 pm

Your request is a little odd (not that there's anything wrong with that). It seems odd to want to delete *everything* when the size reaches a certain level; usually people want to delete only the oldest files, keeping at least some recent files. But given that that is what you want, it is possible that 'find' is not the best tool for the job.

Note also that 'find' is kind of a weird language - you have to do a fair amount of reading and experimenting in order to become comfortable with it - especially if you are using it to delete files. So, if you don't need to learn 'find', it's probably better to leave that for another day.

That said, something like this (bash shell code) may be to your liking:

Code: Select all

set -- $(du -sk /path/to/directory)
[ $1 -gt 4000000 ] && rm -rf /path/to/directory
If this post appears in the wrong forums category, my apologies.

Return to “General programming discussion”