LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 06-28-2010, 03:39 AM   #1
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Rep: Reputation: 0
cron to remove all files within directory except one file


Hi All,
I'm a newbie here, so please help

I would like to create a cronjob that will delete all files within a directory 1 hours after it is created to the folder

I found this cron

find /path/to/file/* -ctime +1 -exec rm {} \; but it's deleted all files.
I want to make an exception, all file should be deleted except one file (letsay file a.zip)

How can I do that?

Thank you
 
Old 06-28-2010, 04:02 AM   #2
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
There is 2 different approaches, ether you can match all file that you want to delete with a regexp (or name) or you can exclude the one you don't want with a regexp (or name).

Example of excluding a specific file:
find /path/to/file/* -not -iname "a.zip" -ctime +1 -exec rm {} \;

Hope this helps

Last edited by rical; 06-28-2010 at 04:04 AM.
 
Old 06-28-2010, 04:04 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Hi and welcome to LinuxQuestions!
Quote:
Originally Posted by restarea View Post
I found this cron
Where did you find it? This is not a "cron", it's just a find command line (very dangerous if you don't really know how it works, because of the -exec rm part). Also it is malformed.

Look at this instead:
Code:
find /path/to/dir -mtime +1 -type f ! -name a.zip -exec echo rm {} \;
First it specifies just a directory as search path (it will be descended recursively). Second I assume you know the difference between ctime (that is not the creation time), mtime and atime and also what +1 really mean for time tests in the find command. Third, it uses echo before the rm command for testing purposes: it does not actually removes anything before you've checked the results and re-run the command again without the echo statement. This is a good practice. On the contrary, what if you accidentally had left a space between "/" and "path/to/file"? It would have removed all your root directory tree and/or your user's home in a bunch of seconds!

Finally, to answer your question, you might add the -name test specifying the name of the file you want to exclude from the search results, using the negation "!" operator, as in my example above. Hope this helps.
 
Old 06-28-2010, 04:38 AM   #4
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
wow thanks for fast response @colucix and rical

Quote:
Where did you find it? This is not a "cron", it's just a find command line (very dangerous if you don't really know how it works, because of the -exec rm part). Also it is malformed.
mmmm I found it somewhere after a google-ing, I put that line on my cron field at cpanel

@colucix I'll try it first and let you know the result
Thanks
 
Old 06-28-2010, 05:13 AM   #5
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by colucix View Post
Hi and welcome to LinuxQuestions!

Second I assume you know the difference between ctime (that is not the creation time), mtime and atime and also what +1 really mean for time tests in the find command.
@colucix I think -ctime -1 means :
all files were created 1 hour before. Is that correct? sorry
 
Old 06-28-2010, 05:32 AM   #6
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Quote:
Originally Posted by restarea View Post
@colucix I think -ctime -1 means :
all files were created 1 hour before. Is that correct? sorry
Not really. -ctime is not the creation time, since most linux filesystems don't store that information. An exception is the recent ext4, but most applications don't take it in account yet.

Please, take a look here for a little but exhaustive explanation of time test in GNU find. As you can see the n, -n or +n specifications are multiples of 24-hours, hence -1 means less than 24 hours ago (but you also have to take rounding effects in account).

If you really want to look for files modified less than 1 hour ago, you may consider -newer. First you may create an empty file with the proper timestamp, then look for more recent files and finally remove the newly created temporary file. Example:
Code:
touch -t $(date -d "- 1 hour" +%Y%m%d%H%M) /tmp/my_dummy_file
find /path/to/dir -type f -newer /tmp/my_dummy_file ! -name a.zip -exec echo rm {} \;
rm /tmp/my_dummy_file
 
Old 06-28-2010, 06:03 AM   #7
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
thx @colucix

btw, this is my condition

I set up a web space where my friend and I will upload files via ftp or via web then sharing them with other friends. Some kinda private file hosting.
And I want any files uploaded after one hours should be deleted automatically to save space

I tried your previous suggestion but looks like it's not working.
The files are still there

Thanks
 
Old 06-28-2010, 06:28 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Ok, sorry. So you want to remove files older than one hour, don't you? That is after one hour from the upload, they should be deleted. This should do the work:
Code:
#
#  Create an empty file with timestamp "1 hour ago"
#
touch -t $(date -d "- 1 hour" +%Y%m%d%H%M) /tmp/my_dummy_file
#
#  Find all files 1 or more hours old (exclude some of them based on their name)
#
find /path/to/dir -type f ! -newer /tmp/my_dummy_file ! -name file_to_exclude -exec echo rm {} \;
#
#  Make clean (remove the temporary empty file)
#
rm /tmp/my_dummy_file
Note the negation before -newer, now. Please, test the above from the command line (it should just print out the resulting rm commands without actually remove anything) and see if the result is satisfactory.

Note that you can exclude more than one file from the search using the -o operator in the following way:
Code:
find /path/to/dir -type f ! -newer /tmp/my_dummy_file ! \( -name first_to_exclude -o -name second_to_exclude \) -exec echo rm {} \;
and add more of them inside the escaped parentheses. You can also use globbing (*) to match files with a common pattern or the -regexp test, as previously suggested by rical.

Finally if you want to run this through cron I suggest to put the statements inside a script and execute it from the crontab, but - if you prefer - you can always put it in one command line, e.g.
Code:
0 * * * * touch -t $(date -d "- 1 hour" +\%Y\%m\%d\%H\%M) /tmp/my_dummy_file && find /path/to/dir -type f ! -newer /tmp/my_dummy_file ! -name file_to_exclude -exec rm {} \; && rm /tmp/my_dummy_file
 
Old 06-28-2010, 06:43 AM   #9
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
Quote:
Originally Posted by restarea View Post
thx @colucix

btw, this is my condition

I set up a web space where my friend and I will upload files via ftp or via web then sharing them with other friends. Some kinda private file hosting.
And I want any files uploaded after one hours should be deleted automatically to save space

I tried your previous suggestion but looks like it's not working.
The files are still there

Thanks
You probably could do it with a script.
Code:
#!/bin/bash
time_stamp=`stat -c '%Y' $1`
time_stamp=$((`date +%s`-$time_stamp))
if [ $time_stamp -gt 3600 ];then
        # Older then 1h, remove
        rm -r $1
fi
and execute it (ON YOUR OWN RISK) with

find /path/to/file/ -not -iname "a.zip" -exec ./script.sh {} \;
 
Old 06-28-2010, 07:09 AM   #10
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
@colucix
thank you for your kindness and help

so this is what I wrote :

Code:
touch -t $(date -d "- 1 hour" +\%Y\%m\%d\%H\%M) /home/xxx/public_html/dummy_file && find /home/xxx/public_html/upload -type f ! -newer /home/xxx/public_html/dummy_file ! -name file_to_exclude -exec rm {} \; && rm /home/xxx/public_html/dummy_file

is that correct?

is the dummy_file folder created automatically or I have to create it manually?


Thanks

Last edited by restarea; 06-28-2010 at 07:11 AM.
 
Old 06-28-2010, 07:32 AM   #11
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Yes, it looks correct. The folder containing the dummy file must be existent, on the contrary you have to create it manually. Indeed, the touch command creates only non-existent files (not directories).

Two notes: 1. don't forget to put the actual name of the file to exclude in your command line, 2. note the escaped % in the date command: they are necessary only in crontab entries, since the % sign has a special meaning there (see man 5 crontab for details), 3. I would actually test the cron job again using echo before rm and sending the output (along with any standard error) to a file for further checking, for example:
Code:
0 * * * * touch blah blah && find blah blah > $HOME/cron.log 2>&1 && rm blah blah
In this way you can avoid surprises. Am I a bit paranoid?
 
Old 06-28-2010, 08:15 AM   #12
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I've tested it and looks it works properly
but i got this warning msg in my email
rm: cannot remove 'home/xxx/public_html/dummy_file' : Is a directory
 
Old 06-28-2010, 08:36 AM   #13
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,362

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Most likely you've previously created a directory with that name. The rest works properly, because the touch command changes the timestamp of existing directories, as well. Maybe I misunderstood your last question but I meant that dummy_file should be a regular file (inside a certain directory). Now you can simply rmdir the dummy_file directory and let the cron job do its work.
 
1 members found this post helpful.
Old 06-28-2010, 12:53 PM   #14
restarea
LQ Newbie
 
Registered: Jun 2010
Posts: 7

Original Poster
Rep: Reputation: 0
it works!!!
thanks alot @colucix and also @rical
 
  


Reply

Tags
cron


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to remove all files from a directory which are 24 hr old siris Programming 8 04-10-2010 01:23 AM
How to remove a directory with files in it. Paul F. Linux - Newbie 8 05-06-2009 12:00 PM
cron script - No such file or directory mokelly1 Linux - Newbie 5 10-27-2006 02:31 PM
Remove directory w/ files kfrancisco Linux - Software 13 10-02-2003 01:40 AM
Remove Files from the Directory .... shib_s Linux - Security 2 08-28-2001 05:53 PM


All times are GMT -5. The time now is 05:25 PM.

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