Deleting the contents of a folder older than X hours

Every day a new .zip file is uploaded to a folder and at mid-night the zip file is to be extracted into a /data/ folder, inside a date-named folder.

# This should extract the contents of a zip file into the /data/ folder into a date based folder
/usr/bin/unzip -a -o /path/to/traveldata/traveldata.zip -d /path/to/traveldata/data/`date +"%Y-%m-%d"`;

Once this is complete, I need to delete old folders within my /data/ folder older than X hours. (It will be set to 48 hours, but for the purposes of my test I've specified 0 hours).

In my example I've been able to remove folders, but it removes the parent /data/ folder too!

I don't want this to happen, rather I just want to delete the sub-folders (or children of /data/).

# I want to delete any folders found inside the data folder older than 0 hours.
find /path/to/data/ -ctime 0 -type d -exec rm -Rf {} \;

If it is not possible to do this, how do I append the date to the extracted filename?

Thanks.

The find is also finding the data folder. You may try something like:

 
find /path/to/data -type d ! -name data -ctime 0 -exec /bin/rm -Rf "{}" \;

Thanks I will try it right now and report here.

Thanks again.

Sorry but its still removing the data folder.

find /path/to/traveldata/data/ -type d ! -name data -ctime 0 -exec /bin/rm -Rf "{}" \;

I get the following output;

NB: The folder does exist before execution of shell script.

Try adding the -depth option:

[rick data]$ pwd
/path/to/traveldata/data
[rick data]$ ll
total 12
drwxrwxr-x 2 rick rick 4096 2009-03-11 08:00 2009-03-09
drwxrwxr-x 2 rick rick 4096 2009-03-11 08:00 2009-03-10
drwxrwxr-x 2 rick rick 4096 2009-03-11 08:00 2009-03-11
[rick data]$ cd
[rick ~]$ find /path/to/traveldata/data -depth ! -name data -type d -ctime 0 -exec /bin/rm -Rf "{}" \;
[rick ~]$ cd -
/path/to/traveldata/data
[rick data]$ ls -la
total 8
drwxrwxr-x 2 rick rick 4096 2009-03-11 08:00 .
drwxrwxr-x 3 rick rick 4096 2009-03-11 07:53 ..
[rick data]$

Hi there.

I tried adding the -depth parameter, I even copy/pasted your code. But its still removing the /data/ folder.

[code]
find /path/to/traveldata/data/ -depth ! -name data -type d -ctime 0 -exec /bin/rm -Rf "{}" \;
find: /path/to/traveldata: No such file or directory
[.code]

What O/S & Shell are you working on?

Can you post a complete trace of what you're doing, similar to what I did in my previous reply?

I don't know. And I'm not sure how to check.

I am using PUTTY on a Windows platform to the server.

This is the best I can do, I don't know how to replicate everything you did in your post.

deals2@something... [~inbounddata/data]# ls -la
drwxrwxrwx 2 deals2 deals 2 4096 Mar 11 13:23 ./
drwxr-xr-x 4 deals2 deals2 4096 Mar 11 13:23 ../

I think that's what you mean.

I've been able to remove files, but when I try to remove any sub folders, it removes the data folder.

I may just extract the contents of the zip file into a /data/ folder and then use PHP or SH to work with the latest .csv file

Note that I did check for spelling (as I use a inbounddata folder and not a traveldata folder) as specified earlier.

Just ensure that your path is correct.

Instead of

find /path/to/traveldata/data . . .

you should use something like

 
find ~inbounddata/data . . .

Maybe someone else can see something I've overlooked, but what I gave you worked as you requested on my LINUX system under KSH.

I've checked the path, I even tried it three times in different folders -- but it still removes the data folder.

find ~/inbounddata/data/ -depth ! -name data -type d -ctime 0 -exec /bin/rm -Rf "{}" \;

How did you do that debug output thing?

Here is the debug output you requested. Usernames/hosts changed for security purposes;

someuser@somehost [~/inbounddata]# pwd
/home/someuser/inbounddata
someuser@somehost [~/inbounddata]#
someuser@somehost [~/inbounddata]# ll
total 6080
drwxr-xr-x  4 someuser someuser    4096 Mar 11 15:23 ./
drwx--x--x 16 someuser someuser    4096 Mar 11 15:04 ../
-rw-------  1 someuser someuser      11 Mar  5 00:00 .ftpquota
drwxrwxrwx  2 someuser someuser    4096 Mar  5 21:21 backup/
drwxr-xr-x  2 someuser someuser    4096 Mar 11 15:23 data/
-rw-r--r--  1 someuser someuser 6179399 Mar 11 01:03 anotherfile.zip
-rw-r--r--  1 someuser someuser   11567 Feb 27 15:52 traveldata.zip
someuser@somehost [~/inbounddata]# cd
someuser@somehost [~]# find ~/inbounddata/data/ -depth ! -name data -type d -ctime 0 -exec /bin/rm -Rf "{}" \;
someuser@somehost [~]# cd -
/home/someuser/inbounddata
someuser@somehost [~/inbounddata]# ls -la
total 6076
drwxr-xr-x  3 someuser someuser    4096 Mar 11 15:26 ./
drwx--x--x 16 someuser someuser    4096 Mar 11 15:04 ../
-rw-------  1 someuser someuser      11 Mar  5 00:00 .ftpquota
drwxrwxrwx  2 someuser someuser    4096 Mar  5 21:21 backup/
-rw-r--r--  1 someuser someuser 6179399 Mar 11 01:03 anotherfile.zip
-rw-r--r--  1 someuser someuser   11567 Feb 27 15:52 traveldata.zip
someuser@somehost [~/inbounddata]#

To identify the OS that you are running:

 uname -a 

To identify the shell that you are using:

 echo $0 

For testing purposes, I removed the "rm -rf" and replaced it with an echo statement - so I can see what the command is doing without expending too much time re-extracting stuff.

find /path/to/data/ -depth ! -name data -type d -ctime 0 -exec echo {} > /path/to/test \;

To see what the find command plans to remove:

cat /path/to/test
uname -a

Produces:

echo $0

Produces:

find /path/to/data/ -depth ! -name data -type d -ctime 0 -exec echo {} > /path/to/test \;

With my setup produces:

And finally...

cat /path/to/test

Produces;

cat ~/inbounddata/data/
cat: /path/to/inbounddata/data/: Is a directory

Try this:

$ find /path/to/data/* -name data -type d -ctime 0 -exec rm -rf {} \;

:smiley:

I tried your code twice and it never deleted anything.

But I noticed you used a * at the end of your /data/ and tried in on a previousily mentioned code and it seems to be working;

find /path/to/inbounddata/data/* -depth -type d ! -name data -ctime 0 -exec /bin/rm -Rf "{}" \;

This deleted the date-named folder created by the following code:

/usr/bin/unzip -a -o /path/to/inbounddata/traveldata.zip -d /path/to/inbounddata/data/$(date +"%Y-%m-%d")

This seems to have cracked the problem! Yey! (I'm going to do some more testing just to make sure!)

For the record, the echo should have been directed to a file (in my example, I called it "test"), not to a directory (in your response).
"cat" the file to see a list of the files / directories that are found.

It is simply a testing mechanism - so that when you test with other flags (like adding the asterisk in the solution above), you don't have to keep recreating the data.

I'm glad that you seem to have found a solution!

  • Avron

My command work under Solaris. I think you are under Mandriva 2009 ?
So, the recursive option of the 'rm' command is '-R' and not '-r'.