List 2 weeks older file on specific directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List 2 weeks older file on specific directory
# 1  
Old 12-10-2014
List 2 weeks older file on specific directory

Ive been a vocal of FIND command even before. Command below doesnt really give me the file that is older than two weeks.. Is there a script that will list me the log files that i want like for this date December 10, 2014, it shud list me the date between November 26, 2014 and below.

When i run that command, it gives me Novemebr 11, we all know nvember 11 is more than two weeks ago.


Code:
find /tmp/logs* -type f -mtime +14 -exec ls -lrt {} \;

# 2  
Old 12-10-2014
Quote:
Originally Posted by kenshinhimura
Ive been a vocal of FIND command even before. Command below doesnt really give me the file that is older than two weeks.. Is there a script that will list me the log files that i want like for this date December 10, 2014, it shud list me the date between November 26, 2014 and below.

When i run that command, it gives me Novemebr 11, we all know nvember 11 is more than two weeks ago.


Code:
find /tmp/logs* -type f -mtime +14 -exec ls -lrt {} \;

that command seems to work fine for me ... shows me files from Nov 17, Nov 3, and older,
but not a file from Nov 28.

Can you show some ls -ltr commands and getfacl commands on the files you believe it should pull back .. and show the command running and not picking them up?

(setup a smaller test directory if you need to .. Smilie )

[edit]
Code:
 
touch -d "2014-12-01 13:12:01" test_dec.txt
touch -d "2014-11-28 13:12:01" test_nov28.txt
touch -d "2014-11-17 13:12:01" test_nov17.txt
touch -d "2014-11-01 13:12:01" test_nov1.txt
ls -ltr
-rw-r-----   1 user    group          0 Nov  1 13:12 test_nov1.txt
-rw-r-----   1 user    group          0 Nov 17 13:12 test_nov17.txt
-rw-r-----   1 user    group          0 Nov 28 13:12 test_nov28.txt
-rw-r-----   1 user    group          0 Dec  1 13:12 test_dec.txt
find . -type f -mtime +14 -exec ls -ltr {} \;
-rw-r-----   1 user    group          0 Nov 17 13:12 ./test_nov17.txt
-rw-r-----   1 user    group          0 Nov  1 13:12 ./test_nov1.txt

[/edit]

Last edited by Ditto; 12-10-2014 at 03:56 PM.. Reason: working sample
# 3  
Old 12-11-2014
Quote:
Command below doesnt really give me the file that is older than two weeks
find /tmp/logs* -type f -mtime +14 -exec ls -lrt {} \;
Well, I disagree. 11. November fits exactly into the range you specified to find.
c.f. man find:
Quote:
-mtime n
File's data was last modified n*24 hours ago. See the comments for -atime to understand how rounding affects the interpretation of file modification times.

Numeric arguments can be specified as

+n for greater than n,
# 4  
Old 12-11-2014
Quote:
Originally Posted by RudiC
Well, I disagree. 11. November fits exactly into the range you specified to find.
c.f. man find:
Can you show an example? I read over the man page and couldn't see any reason it should exclude a date 29 days ago, when you indicate 14
Smilie

That seems like a pretty generous round off to me.
# 5  
Old 12-11-2014
I disagreed with the quoted sentence from post#1, which means I'm d'accord with your (Ditto's) statement. -mtime points to a given point in time n *24 hours ago, +n opens that to a range larger than n, meaning everything older than that point in time.
# 6  
Old 12-11-2014
mtime works days made of epoch seconds, see the stat structure for st_mtime.
There are 86400 seconds per day. -mtime 14 means EXACTLY, to the second, 86400*14 seconds ago. -mtime +14 means any file mtime equal to or greater than 86400*14,
-mtime -14 means less than 86400*14.


Some ways to get file by filetime using find (in the context of post #1)

With:
Today = Dec 12, two weeks ago = Nov 27

These examplesrefer to mtime, the last time the file was opened and written to. NOT
when it was created, but it usually works out to the same thing in most
users view. This is using POSIX find. Linux find is slightly different but
will work with these examples.

Note: touch examples are accurate to the minute only. touch -t syntax seems to vary.
Check your system.
Note: some filesystems have more precision on filetimes than others.

Note: "start" and "end" are dummy files in the current directory, a
directory that you can write in. I personally use dummy, dummy1, and
dummy2 so I know what they are and why they are empty. start and end are
clearer for this example. IMO.

1. get files last written exactly before Nov 27 at midnight
Code:
touch -t 201411270000 end
find /path -type f ! -newer end

2. get files written after Nov 27, i.e., from the first second of Nov 28 -> present

Code:
# you should add seconds to the touch -t value
touch -t 201411272359 start
find /path -type f -newer start

3. Using mtime for files older than some number of days. mtime uses days
based on 86400 seconds per day. STARTING RIGHT NOW. So if you execute this
at 1500 you will get files which will have filetime of 1521 (and greater)
on what seems to be the wrong day. mtime does understand the calendar.

Code:
# older files more then 14 * 86400 seconds old.
find /path -type f -mtime +14
# newer - files written after (the time and date of) 84600 * days in the past
find /path -type f -mtime -14

4. get files from two past times - a range Nov 7 -> Nov 20.
No files from Nov 6 and earlier, and no files dated Nov 21 -> now

Code:
# mind the touch seconds
touch -t 201411070000 start
touch -t 201411202359 stop
find /path -type f \( -newer start -a  ! -newer end \)

# 7  
Old 12-11-2014
Quote:
Originally Posted by RudiC
I disagreed with the quoted sentence from post#1, which means I'm d'accord with your (Ditto's) statement. -mtime points to a given point in time n *24 hours ago, +n opens that to a range larger than n, meaning everything older than that point in time.
Ah, must have misread you then .. no worries Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to list all folders in a specific directory

The below bash is trying to list the folders in a specific directory. It seems close but adds the path to the filename, which basename could strip off I think, but not sure why it writes the text file created? This list of folders in the directory will be used later, but needs to only be the... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. Shell Programming and Scripting

Move log files with date and delete older than 3 weeks

I have written a script which generate one logfile on every sunday and thursday I want to move the older log files into /tmp directory befor generating new one so i used mv command like mv usr/sbin/appl/logfile.txt usr/sbin/appl/tmp 2) But when i move this file to /tmp it will... (1 Reply)
Discussion started by: Nakul_sh
1 Replies

3. Shell Programming and Scripting

[SOLVED] Pull lines older than N weeks

There is another post in the forums that is similar to what I am trying to do, however, the thread is closed. So, I am creating this new one to see if someone could help. I am trying to use the code Ahamed posted, and tweak it. With the info from the forum, I recreated the scenario the person... (8 Replies)
Discussion started by: karstenjhilton
8 Replies

4. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

5. Shell Programming and Scripting

Delete the files older than 3 weeks in a particular directory.

Hi, Friends, I am writing a script to delete all the files which are there for more than 3 weeks. I have tried this : find /home/appl/backup -type f -mtime +21 -exec rm -f {} \; But i am not sure if it deletes only the files in specified directory or all the directorinies in the provieded... (3 Replies)
Discussion started by: rajsharma
3 Replies

6. UNIX for Dummies Questions & Answers

Linux shortcutkey to search specific file from a list of directory?!

Hi, I'm the new user of linux/unix. Can I ask that anybody know how to use the linux/unix shortcut key to search a specific file from a list of directory? For example, I know the file name that I want to search. But I forget which directory or location is my desired file put.Got any shortcut... (7 Replies)
Discussion started by: patrick87
7 Replies

7. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

8. Shell Programming and Scripting

how to check whether the given file is 5 weeks older than current date

HI, I need to check whether the given file is 5 weeks older than current date ?? Can anyone give me the script for this ?? (1 Reply)
Discussion started by: risshanth
1 Replies

9. Shell Programming and Scripting

List directory 7 days older

Say folder archive/ contains many folder each created on a day. this folder may contain files. i want to write a script to delete all the folder inside archive/ which are 7 days older. i used the below script for the reason. find archive -mtime +7 -type d -exec rm -r {} \; pls suggest me if... (3 Replies)
Discussion started by: krishnarao
3 Replies

10. Solaris

List all files that contain a specific directory

I need to list all files and subdirectories that contain "oradata". For example, I have several files in several different directories that contain "oradata". I.e. /u07/oradata/1.dbf /u09/unix/whatever/oradata/2.xxx That is, whatever file on the system that contains a directory called... (7 Replies)
Discussion started by: Sat510
7 Replies
Login or Register to Ask a Question