How to delete subdirectories that are more than 1 day old AND have NO files on them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete subdirectories that are more than 1 day old AND have NO files on them
# 1  
Old 07-21-2011
How to delete subdirectories that are more than 1 day old AND have NO files on them

Hi Experts ,

At a particular dir , How to delete subirectories that are more than 1 day old AND have NO files in them.
PS : -empty option is not working at my Sun OS version.
Thanks,

A

Last edited by ajaypatil_am; 07-21-2011 at 07:51 AM..
# 2  
Old 07-21-2011
Code:
find .. -type d -mtime +1 | while read d; do
  [ -z "`ls "$d"`" ] && echo rmdir "$d"
done

Try and if it works remove "echo".

PS It is unclear what it means - "1 day old" for directories. Maybe you want "ctime".
This User Gave Thanks to yazu For This Post:
# 3  
Old 07-21-2011
thanks i mean the dirs which have lastupdated time stamp of NOT today.
e,g
Code:
drwxrwxr-x   2 4096 Jul 20 13:32 ACIFG021
drwxrwxr-x   2 4096 Jul 20 17:46 ACIFG022
drwxrwxr-x   2 4096 Jul 21 07:59 ACIFG023
drwxrwxr-x   2 4096 Jul 21 08:28 ACIFG024
drwxrwxr-x   3 4096 Jul 21 11:54 ACIFG025

the command should delete ACIFG021 and ACIFG022 if there any no files in any of the subdirs under them ...the command should not delete todays (21st july) dirs ACIFG023,ACIFG024 and ACIFG025.

Hope it is clear now.
Thanks,
A

Last edited by Scott; 07-21-2011 at 08:19 AM.. Reason: Code tags
# 4  
Old 07-21-2011
Code:
find mydir -depth -empty -type d -exec rmdir {} \;

# 5  
Old 07-21-2011
where are you checking if file exists in that dir or not ? is it [ -z "`ls "$d"`" ] ?
Quote:
Originally Posted by yazu
Code:
find .. -type d -mtime +1 | while read d; do
  [ -z "`ls "$d"`" ] && echo rmdir "$d"
done

Try and if it works remove "echo".

PS It is unclear what it means - "1 day old" for directories. Maybe you want "ctime".
# 6  
Old 07-21-2011
Quote:
is it [ -z "`ls "$d"`" ] ?
Yes.
This User Gave Thanks to yazu For This Post:
# 7  
Old 07-26-2011
thanks it worked ...2 more questions ....
1) now i want to delete ALL the dirs , suddirs in a particular dir ....i am using rm -rf dirname but it is taking very long time ...is there any faster way ? will following script faster ?

find . -type d | while read d; do

rm -rf "$d"
done

2) why rmdir -rf is not working for me ?.see below

> rmdir -rf DSF_DECOM
rmdir: illegal option -- r
rmdir: illegal option -- f
Usage: rmdir [-ps] dirname ...
> rmdir -r DSF_DECOM
rmdir: illegal option -- r
Usage: rmdir [-ps] dirname ...
> rmdir DSF_DECOM
rmdir: directory "DSF_DECOM": Directory not empty



Thanks,
A

Quote:
Originally Posted by yazu
Yes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Delete records that do not belong to that day

i have a requirement to delete records that do not belong to that day. For example in a file that came on July 31st ,2018 there are records that belong to Aug 1st,2018 as well and I want to find and delete those records. I want to delete anything with 01-Aug-2018. I have several files like that. I... (6 Replies)
Discussion started by: Priya
6 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. Shell Programming and Scripting

Delete file to day

friends how I can delete files from a directory for the current date? (2 Replies)
Discussion started by: tricampeon81
2 Replies

4. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

5. AIX

Want to delete directory, subdirectories and all files which are older than 7 days

how do i remove sub directories of a directory and all files which are older than 7 days by a single command in AIX. pls help me. I am using command as #find /gpfs1/home/vinod/hpc/ -depth -type d -mtime +7 -exec rm -rf {} \; so i want to delete all sub directories and all files from the... (1 Reply)
Discussion started by: vinodkmpal
1 Replies

6. UNIX for Dummies Questions & Answers

Move the files between Current day & a previous day

Hi All, I have a requirement where I need to first capture the current day & move all the files from a particular directory based on a previous day. i.e move all the files from one directory to another based on current day & a previous day. Here is what I am trying, but it gives me errors.... (2 Replies)
Discussion started by: dsfreddie
2 Replies

7. Shell Programming and Scripting

Delete all the files and subdirectories for the year 2006

Hi I have lot of files and subdirectories inside a directory which are created in the years 2006, 2007, 2008, 2009 and 2010. I want to delete all the files and subdirectories belonging to the year 2006 alone. How can I do that ? (5 Replies)
Discussion started by: samsungsamsung
5 Replies

8. Shell Programming and Scripting

How to delete files which more than one Day old?

I have a directory /opt/targets which generates more than 1000 files per day and I want to delete all the files which are more than 1 day old...ie 24hrs+ Please help.. (3 Replies)
Discussion started by: sunilrk07
3 Replies

9. Shell Programming and Scripting

Not able to delete the files in day wise...

Dear All, I am not able to remove the files in my AIX 5.3 server. My files List /iims-home/data/uat1/batch/staging -rw-r--r-- 1 iims iims 5743 Jun 12 09:04 ErrorReport2008-05-20 09-04-18.doc -rw-r--r-- 1 iims iims 191213683 Jun 12 09:05... (1 Reply)
Discussion started by: bvijaycom
1 Replies

10. Shell Programming and Scripting

delete files one day old in current month only

i want to delete files that are one day old condition is files should be of current month only ie if iam running script on 1 march it should not delete files of 28 feb(29 if leap year :-)} any modifications to find $DIR -type f -atime +1 -exec rm -f{}\; (4 Replies)
Discussion started by: maverick
4 Replies
Login or Register to Ask a Question