LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-18-2008, 06:24 AM   #1
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Rep: Reputation: 0
Delete old files script


Hello, I'm new in Linux world. I want to figure out how to delete old files from particular folder. Can anyone help me?
My webcamera produces many photos per day and I need to delete photos which are older than five days. Can anyone help me with the script?

And is there some program in linux which can execute this script every day in particular time? I have UBUNTU.

thanx a lot.
 
Old 04-18-2008, 06:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
find -atime +5 -iname '*.jpg' -exec rm {} \;

you could just run that through a cronjob if you wished.

Last edited by acid_kewpie; 04-24-2008 at 02:39 PM.
 
Old 04-18-2008, 03:41 PM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
I'd suggest using ctime instead of atime, just seems more
logical to me.
 
Old 04-24-2008, 04:22 AM   #4
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
hello,
thx for that command ctime but I have problem with that -exec.
it writes me that there is missing argument for -exec.

I tried to use command

find -ctime +4 -iname '*.jpg' -exec rm {}

thanks
 
Old 04-24-2008, 04:34 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
You forgot \; after the {}


Cheers,
Tink
 
Old 04-24-2008, 05:18 AM   #6
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
I found command

find -ctime +4|rm *.jpg

is it correct too? or does it have some side effects?

thx
 
Old 04-24-2008, 06:00 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You left out the xargs command after the pipe character "|".

Code:
find ./ -ctime +5 -print0 | xargs -0 -L 500 rm
You might use this is you had a very large number of results that would cause the shell to run out of memory. The "-L 500" argument limits the number of arguments handled at once. The "-print0" argument to find used the NUL character to separate the results. The "-0" argument to xargs means to use NULs to sepatate the arguments. You would want to do this if the filenames contain whitespace characters such as spaces.
 
Old 04-24-2008, 06:12 AM   #8
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
there are about 2k-3k new files per day. so sometimes I need to delete about 5k of files or more.
and there aren't whitespace characters in names of files.

so what is the best solution for this case?

Last edited by simpi; 04-24-2008 at 06:15 AM.
 
Old 04-24-2008, 06:34 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,384

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
How about you actually respond to Tinksters advice?? he's told you what you did wrong, but you're ignoring his advice it seems.
 
Old 04-24-2008, 07:03 AM   #10
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
when I write there full command what Tinkers wrote it writes me still the same error.
find ctime +2 -iname '*.jpg' -exec rm {}\;
find: missing argument for "-exec"

but it is written in my language.



I didn't ignoring him, I just wanna try some command and syntax I know a little bit. That was the reason why I asked to that command.
 
Old 04-24-2008, 02:25 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
And there's a space between {} and \; ?
Try copy and paste:
Code:
find -ctime +2 -iname '*.jpg' -exec rm {} \;

Cheers,
Tink
 
Old 04-25-2008, 02:37 AM   #12
simpi
LQ Newbie
 
Registered: Apr 2008
Posts: 15

Original Poster
Rep: Reputation: 0
Wink

txh, that works.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write a script to delete files? AGazzaz Linux - General 11 12-05-2007 06:43 AM
Script to delete files from specified folder mrrx7 Solaris / OpenSolaris 4 08-27-2007 03:50 AM
bash script to delete files c0d3 Programming 9 12-05-2004 10:45 PM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 06:26 PM
Script on linux to delete certain files ForumKid Linux - General 2 06-22-2002 01:09 PM


All times are GMT -5. The time now is 07:08 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration