Delete directory on date base


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete directory on date base
# 8  
Old 11-10-2013
Quote:
Originally Posted by RudiC
Doesn't the -mtime +4 test do the job for you?
noop. Not working. Already tried because i want to delete as per pattern

Code:
directory name is -> "Direct 2013 -11-10 53354"

---------- Post updated at 01:15 PM ---------- Previous update was at 10:53 AM ----------

Update: After checking i found that sometime it create two directories in one day with same name but with last number become change, as i mentioned earlier. like below format.

Code:
Direct 2013-09-10 53354
Direct 2013-09-10 53945


Last edited by learnbash; 11-10-2013 at 01:27 PM..
# 9  
Old 11-10-2013
If -mtime +4 criteria does not satisfy by itself your requirement, you have to give find another criteria as an AND as well.

Example:

Code:
find <directory> -name <pattern> -type d -mtime +4 -exec rm -rf '{}' \;

That translate to find any directory AND check that the name match pattern AND that the modified time is more than four days old, if so delete its content and itself

The issue is that you would have to know a pattern which will distinguish both directories with same -mtime. If I am understanding your requirement.
# 10  
Old 11-10-2013
Quote:
Originally Posted by Aia
If -mtime +4 criteria does not satisfy by itself your requirement, you have to give find another criteria as an AND as well.

Example:

Code:
find <directory> -name <pattern> -type d -mtime +4 -exec rm -rf '{}' \;

That translate to find any directory AND check that the name match pattern AND that the modified time is more than four days old, if so delete its content and itself

The issue is that you would have to know a pattern which will distinguish both directories with same -mtime. If I am understanding your requirement.

I don't want to use mtime, i want to search on criteria, lets suppose today date is 2013-11-10 so it keep all directories from 2013-11-10 till 2013-11-07, rest directory first it show and then delete it, My directory name format is "Direct 2013-11-10 53354"
# 11  
Old 11-10-2013
Is this of any help?

Code:
#!/bin/bash

t_mark=`date --date="4 days ago" +"%s"`

ls -d Direct* | while read d
do
    dirname_tokens=(`echo $d`)
    t_epoch=`date --date="${dirname_tokens[1]}" +"%s"`
    [[ "$t_epoch" < "$t_mark" ]] && ls -d "${d}"

done

# 12  
Old 11-10-2013
Quote:
Originally Posted by Aia
Is this of any help?

Code:
#!/bin/bash

t_mark=`date --date="4 days ago" +"%s"`

ls -d Direct* | while read d
do
    dirname_tokens=(`echo $d`)
    t_epoch=`date --date="${dirname_tokens[1]}" +"%s"`
    [[ "$t_epoch" < "$t_mark" ]] && ls -d "${d}"

done

Not working. Dear sir it is showing all dates. I am not searching on date base need to search on date pattern.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. Shell Programming and Scripting

How to append date to filename, but base it on yesterday's date?

Hello, I'd like to write a monthly archive script that archives some logs. But I'd like to do it based on yesterday's date. In other words, I'd like to schedule the script to run on the 1st day of each month, but have the archive filename include the previous month instead. Here's what I... (5 Replies)
Discussion started by: nbsparks
5 Replies

4. Shell Programming and Scripting

Massive Copy With Base Directory

I have a script that I am using to copy around 40-70k files to a NFS NAS. I have posted my code below in hopes that someone can help me figure out a faster way of achieving this. At the end of the script i need to have all the files in the list, copied over to the nas with source directory... (8 Replies)
Discussion started by: nitrobass24
8 Replies

5. Shell Programming and Scripting

Needs to delete particular date files in a directory

Hi Guys, I need help on deleting particular date files in a directory. I have to delete thousands of files with respect to particular date. Could anyone help on this to delete particular date files at a time? Thanks in Advance (2 Replies)
Discussion started by: teddy2882
2 Replies

6. Shell Programming and Scripting

Base directory of the package

Hello, I have an RPM package for which I need to know the base directory /opt/appdir. But if the package is relocated, then it will be different. I need to get the value dynamically. I tried to query the RPM with INSTPREFIXES but couldn't get the value. The only way I see is to do 'rpm -ql RPM... (2 Replies)
Discussion started by: chiru_h
2 Replies

7. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

script to delete contents in a directory by date

Hi , I am trying to make a cron job to delete the contents of a directory in a linux environment.The contents created before 2 days should get deleted by this job.Here is what i have written : /usr/bin/find / -name *.log -ctime +2 -exec /bin/rm -f {} \; Is it correct....If not so,please... (9 Replies)
Discussion started by: d8011
9 Replies

9. Solaris

Missing Base Directory

Hey all, I'm attempting to migrate a zone from one system to another, but I noticed that on the new system ssl BASEDIR was missing. I was wondering if creating the directory path for this would fix the issue when I migrate files over. SSL is installed on the new system but does not show a... (1 Reply)
Discussion started by: em23
1 Replies

10. UNIX for Dummies Questions & Answers

Find command to run only in the base directory

Hi. I'm trying to get my find command to only search in the directory i tell it to, but i don't want it to search in the sub directories as well... For example, i have a /data/files/ and /data/files/old I want to search for all .sav files within /data/files but i don't want it to drill... (4 Replies)
Discussion started by: Stephan
4 Replies
Login or Register to Ask a Question