Need files exactly 3 to 7 days old


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need files exactly 3 to 7 days old
# 1  
Old 07-28-2016
RedHat Need files exactly 3 to 7 days old

Hi,

I need files exactly 3 to 7 days old.

Today is 28th July 2016.

I have two files one dated 25th July and other 21st July which are 3 to 7 days old.
Code:
94702    1 -rw-r--r--   1 m1083 user1         26 Jul 25 13:00 ./Report_0751.txt
94751    1 -rw-r--r--   1 m1083 user1        128 Jul 21 13:00 ./Report_0710.txt

Below is what i tried

Code:
find . -mtime +2 -a -mtime -7 -ls

But instead of displaying both the files it shows only one file dated 21st.

My understanding of the command is-> find files more than 2 days old and not more than 7 days old -> which means both the files dated 25th and 21st.

Can you please tell me why i dont see both the files as output of the find command ?

Moto: I will use the same solution to find files exactly 7 days old.

Last edited by mohtashims; 07-28-2016 at 03:45 PM..
# 2  
Old 07-28-2016
At what time of day did you search for files less than 7 days old? (Was it before or after the 1pm timestamp on both of your files?)
# 3  
Old 07-28-2016
find command does NOT use calendar days.

Code:
$ date
Thu, Jul 28, 2016  8:30:10 PM
$ date +%s
1469759421 # this NOW on my computer, the number of seconds since midnight on Jan 1 1970.

1469759421 -86400 =1469673021  # this is one day ago.  It is 8:30 yesterday evening.

This is how find works.
One way to get calendar days is to create two dummy files
use the touch command NOTE: I am using the date on my system, you make it fit yours:
Code:
touch -t 201607210000 dummy1  # seven days ago at the very start of the day
touch -t 201607252359 dummy2  # three days ago at the END of the day i.e., 23:59
find /path/to/files -type f - \( -newer dummy1 -a !-newer dummy2 \)

# 4  
Old 07-29-2016
I would consider making two dummy files to set the start and end date/times that you are interested in and then use the -newer flag of find to search between them.


I hope that this helps,
Robin
# 5  
Old 07-29-2016
-mtime 7 means the files that are between 7*24*60*60 seconds and 8*24*60*60 seconds old.

If you want a certain calendar day then you must run this at midnight.

Last edited by rbatte1; 07-29-2016 at 11:42 AM.. Reason: Added ICODE tags for the flags
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

Delete files 30 days old

Will this work to delete files 30 days old in $backupDir or is there a better way to do it? find $backupDir -type f -mtime +30 -exec rm {} \; (2 Replies)
Discussion started by: wyclef
2 Replies

2. Shell Programming and Scripting

How to create zip/gz/tar files for if the files are older than particular days in UNIX or Linux?

I need a script file for backup (zip or tar or gz) of old log files in our unix server (causing the space problem). Could you please help me to create the zip or gz files for each log files in current directory and sub-directories also? I found one command which is to create gz file for the... (4 Replies)
Discussion started by: Mallikgm
4 Replies

3. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

4. UNIX for Dummies Questions & Answers

Removing certain files after certain days..

Hi guys.. Currently me using AIX 5.3.0.0 I have these files generated by Oracle in /tmp folder.. # date Mon Sep 26 11:53:31 BEIST 2011 # pwd /tmp # df -g . Filesystem GB blocks Free %Used Iused %Iused Mounted on /dev/hd3 10.00 3.65 64% 62479 7% /tmp... (2 Replies)
Discussion started by: mushr00m
2 Replies

5. UNIX for Dummies Questions & Answers

Files older than 50 days

Hi All, OS :- HP-UX wm5qa B.11.23 U ia64 1119805695 unlimited-user license I need to search files older than 50 days. I've used following command in order to search desired files, I also discoverd, it's showing today's files as well. Do you have any clue with this ? wmqa1> find .... (4 Replies)
Discussion started by: alok.behria
4 Replies

6. Shell Programming and Scripting

help with removing files which are n days old

Hi , I need to remove files from a specific directory which are 7 days old using folowing find comand as: find /home/dir1/log -name "run*.log" -mtime +6 -prune -exec rm -f {} \; but my script should traverse to /home/dir1/log and them delete the specified files in the directory. i.e... (2 Replies)
Discussion started by: Navatha
2 Replies

7. 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

8. UNIX for Dummies Questions & Answers

How to delete files over 30 days

I have a directory that contains files. I would like the command that deletes all files that are over 30 days old. Delete files based on creation date and not modified. (2 Replies)
Discussion started by: GEBRAUN
2 Replies

9. Shell Programming and Scripting

Deleting files over 7 days old

Simple question I am sure - but one that I dont know the answer to nevertheless.. I have a directory that I store log files in - but I wish to run a daily script in cron to delete all files in this directory that have not been modified within the last 7 days... Can anyone help me? (10 Replies)
Discussion started by: frustrated1
10 Replies

10. UNIX for Dummies Questions & Answers

How to delete files which are 7 days old

Hi all, how to write a script that will indentify the files in a directory which are 7 days old and delete those files. Thanks in advance Cheers Arunava (8 Replies)
Discussion started by: arunava_maity
8 Replies
Login or Register to Ask a Question