Purge X old days files and Print count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Purge X old days files and Print count
# 1  
Old 08-15-2017
Purge X old days files and Print count

Hello All,


I am trying to purge X old days of files from directory & Sub directories
Code:
./2016-01-13/1500/abc.txt 
./2016-01-14/1605/caf.txt
./2016-01-14/1605/caf2.txt
./2016-01-14/1606/eaf.txt
..... 
./2017-08-1/1701/

Should also remove directories and sub directories too

Expected output:
Should send out an email saying
Purged X days older files with stats like
1(Number of files deleted): 2016-01-13
2 : 2016-01-14

Currently, I got a find commands one tells counts in directories
Code:
find -maxdepth 1 -mtime +10 -type d | sort | while read -r dir; do n=$(find "$dir" -type f | wc -l); printf "%4d : %s\n" $n "$dir"; done

Regards,
Krus
# 2  
Old 08-15-2017
using bash this is one way -- example :

Code:
#!/bin/bash
cd /starting/path
find -maxdepth 1 -mtime +10 -type d | sort | 
  while read -r dir
  do 
     n=0
     find "$dir" -type f -exec (( n++ )) -exec rm {} \;
     printf "%4d : %s\n" $n "$dir"
  done

# 3  
Old 08-15-2017
Thanks, Jim.

Code:
     find "$dir" -type f -exec (( n++ )) -exec rm {} \;

how to test without -exec rm {} ?

Regards,
Karthik
# 4  
Old 08-15-2017
Jim,
find -exec can run shell scripts but not statements of the current shell.
# 5  
Old 08-15-2017
Hi Jim,

Code:
#!/bin/bash
cd /starting/path
find -maxdepth 1 -mtime +10 -type d | sort | 
  while read -r dir
  do 
     n=0
     find "$dir" -type f -exec (( n++ )) \; #-exec rm {} \;
     printf "%4d : %s\n" $n "$dir"
  done

Whats wrong in above script?

How to test script without rm command. And if I commented
Code:
-exec rm {}

then i'm getting an error. anything i'm missing?

-Krux
# 6  
Old 08-15-2017
Try
Code:
-exec echo rm {}

# 7  
Old 08-15-2017
Code:
# GNU only
# find . -type d -mindepth 1 -maxdepth 1 -mtime +10 -print
# portable
find . -type d \! -name . -prune -mtime +10 -print | sort |
while read dir
do
  n=$(
    find "$dir" -type f -mtime +10 -exec true rm -f {} \; -print | grep -c .
  )
  if [ $n -gt 0 ]
  then
    echo "$n files deleted in $dir"
  fi
  find "$dir" -depth -type d -empty -exec true rmdir {} \;
  if [ \! -d "$dir" ]
  then
    echo "$dir deleted"
  fi
done

Remove true to really do the deletions.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

2. UNIX for Dummies Questions & Answers

How to count previous days files

Hi i know how to get no of files in some particular folder by following command which includes today and previous days files also ls |wc -l but i needed a command in which i can list previous one or two days files only. (4 Replies)
Discussion started by: rahiljavaid
4 Replies

3. Shell Programming and Scripting

To create a script and schedule which purge 30 days old files

Hi Friends, Very new in Unix and i got a requirement like writing a script and schedule it, so that it removes 30 days old files from all the log locations of a unix box. Suppose i have a unix server ltbamdev1 and in this server i have a mount point opt/bam. In this mount point i have 3... (1 Reply)
Discussion started by: duos
1 Replies

4. Shell Programming and Scripting

count identical strings print last row and count

I have a sorted file like: Apple 3 Apple 5 Apple 8 Banana 2 Banana 3 Grape 31 Orange 7 Orange 13 I'd like to search $1 and if $1 is not the same as $1 in the previous row print that row and print the number of times $1 was found. so the output would look like: Apple 8 3 Banana... (2 Replies)
Discussion started by: dcfargo
2 Replies

5. Shell Programming and Scripting

Purge files and archive

Hi Friends, I have an urgent requirement. I have many files huge in size which has occupied almost the entire disk space. The files are not being moved to the archived folder. But at present I need to purge those files, new to shell scripting, not sure how to proceed. Eg. Directory... (3 Replies)
Discussion started by: unx100
3 Replies

6. Shell Programming and Scripting

Shell script to check the files hourly and purge

I'm new to shell scripting... i have been given a task.. can any one help in this regard.... 1) Check hourly for files in <destination-path><destination-file-template><destination-file-suffix> for files older than <destination-file-retention> days and purge. It should then check... (1 Reply)
Discussion started by: satishpabba
1 Replies

7. Shell Programming and Scripting

print as well as count the files found by find command

I want the output of the find command to be printed and also the total files found by it. Can someone help in this. Obviously $ find . -type f | wc -l will not output the files found but only the count. I want both. There can be millions and trillions of files so dont want the output of find... (3 Replies)
Discussion started by: amicon007
3 Replies

8. UNIX for Dummies Questions & Answers

Find and purge a files in a dir

HI All, I have recuirement to purge the files in a directory . In that directory i an having many sub-directory . When i use find command like find ~/work/test/insert -name "*.*" -mtime +12 it is listing the file not accesed before 12 , It also takes the subdirectories inside the... (7 Replies)
Discussion started by: arunkumar_mca
7 Replies

9. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies

10. Shell Programming and Scripting

Purge files, keep 3 versions of last 4 days

Hello, I currently generate a file every 15 minutes for 12 hours a day. I would like to clean the directory on a daily basis. I only want to keep the latest 3 versions for the last 4 days in the directory. Any suggestions? Thanks, Barbara (7 Replies)
Discussion started by: blt123
7 Replies
Login or Register to Ask a Question