Deleting the contents of a folder older than X hours


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting the contents of a folder older than X hours
# 8  
Old 03-11-2009
Quote:
What O/S & Shell are you working on?
I don't know. And I'm not sure how to check.

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

Quote:
Can you post a complete trace of what you're doing, similar to what I did in my previous reply?
This is the best I can do, I don't know how to replicate everything you did in your post.

Code:
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.
# 9  
Old 03-11-2009
Just ensure that your path is correct.

Instead of

Code:
find /path/to/traveldata/data . . .

you should use something like

Code:
 
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.
# 10  
Old 03-11-2009
I've checked the path, I even tried it three times in different folders -- but it still removes the data folder.

Code:
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;

Code:
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]#

# 11  
Old 03-11-2009
To identify the OS that you are running:
Code:
 uname -a

To identify the shell that you are using:
Code:
 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.
Code:
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:
Code:
cat /path/to/test

# 12  
Old 03-11-2009
Code:
uname -a

Produces:
Quote:
Linux server1.somesite.com 2.6.18-92.1.18.el5.028stab060.2PAE #1 SMP Tue Jan 13 12:31:30 MSK 2009 i686 i686 i386 GNU/Linux
Code:
echo $0

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

With my setup produces:
Quote:
find ~/inbounddata/data -depth ! -name data -type d -ctime 0 -exec echo {} > ~/inbounddata/data \;
-bash: /path/to/inbounddata/data: Is a directory
And finally...

Code:
cat /path/to/test

Produces;

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


Last edited by worchyld; 03-11-2009 at 03:55 PM.. Reason: security purposes
# 13  
Old 03-11-2009
Try this:
Code:
$ find /path/to/data/* -name data -type d -ctime 0 -exec rm -rf {} \;

Smilie
# 14  
Old 03-11-2009
Quote:
Originally Posted by Goldorakk
Try this:
Code:
$ find /path/to/data/* -name data -type d -ctime 0 -exec rm -rf {} \;

Smilie
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;

Code:
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:

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!)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

Deleting Files Older than 1 hours.

How to Deleting Files Older than 1 hours. Base on SunOS. this file gen every 1 min. -rw-r--r-- 1 nobody nobody 4960 Jan 27 02:02 23_201301270201.log -rw-r--r-- 1 nobody amudu 2325 Jan 27 02:03 33_201301270202.log -rw-r--r-- 1 nobody amudu 3255 Jan 27 02:03... (2 Replies)
Discussion started by: ooilinlove
2 Replies

3. Shell Programming and Scripting

files older than few hours

Hi All I need to know the command which can be used to list the files which are 3 hours old so that it can be deleted. (3 Replies)
Discussion started by: mskalyani9
3 Replies

4. Shell Programming and Scripting

Deleting files older than 6 hours

Hi All, I am using the below script to find all the files in a folder which are older than 6 hours and delete all those files, but some how I am not getting the required output. find $HOME/Log -type f -name "*.log" -amin +360 -exec rm *.* {} \ can any one please check and let me know... (13 Replies)
Discussion started by: subhasri_2020
13 Replies

5. Shell Programming and Scripting

Find files older than 8 hours

I need a script to find files older than 8 hours... I know i can use mmin but the same is not working...the same only support mtime... This is the script i created..but the same is only giving 1 hour old..as I have given dt_H as 1 only...but if i give 8..it can go in -(negative)..how to get the... (5 Replies)
Discussion started by: cotton
5 Replies

6. AIX

how to find files older than 2 hours

I need help to find files in a directory that are older than 2 hours. Any help would be great. (3 Replies)
Discussion started by: pt14
3 Replies

7. Solaris

Deleting Files Older than 24 hours

Hi, I am using Solaris Box, I need to delete file(cookies.html) from the path(/usr/temp) which are older than 24 hours(I want in hours, not in days) Can u provide the command for the above query (7 Replies)
Discussion started by: mazhar803
7 Replies

8. Shell Programming and Scripting

removing files after 6 hours or older

What is the command to remove files that are generated 6 hours or older? The find and remove tells only how to remove if the file is one day old or more. Appreciate quick reply. Thanks (3 Replies)
Discussion started by: gthokala
3 Replies

9. UNIX for Dummies Questions & Answers

Finding only those files older than 2 hours

I need to write a program that will only remove those files that are older than 2 hours. Is there some variation of find . -mtime ? -name '*' that I can use? Thanks as always for your help. Regards, Dave :) (2 Replies)
Discussion started by: mh53j_fe
2 Replies

10. Shell Programming and Scripting

Finding files older than 2 hours

I want to write a sh script that will find files older than 2 hours and tar them. I've had a look at the find man page but can't see how to do it by hours. Help please. Thanx (1 Reply)
Discussion started by: ianf
1 Replies
Login or Register to Ask a Question