34

So, I want to display (via ls for example) all files, which were changed in the last seven days. If I'm in my docroot-folder, it should be able to look "deeper".

For example:

File        Last changed
docroot
|- myfile1  30.11.2015
|- myfile2  10.11.2015
|- MySub
   |-sub1   30.11.2015
   |-sub2   10.11.2015

So, the ls (or whatever fits) should output myfile1 and (if possible) MySub/sub1.

Is this doable with one command?

6 Answers 6

56

Of course. From the directory you are in do:

find . -type f -mtime -7 -exec ls -l {} \; 

Add a redirection to it (aka > results.txt to store them into that file).

  • type f does only files and not directories
  • mtime -7 does 7 days ago up to now (+7 would be 'older than 7 days')
  • and it then feeds it to ls to show a long list

You can play with the ls -l part too:

find . -type f -mtime -7 -exec ls -Rl --time-style=long-iso {} \; 
find . -type f -mtime -7 -exec ls -R  --time-style=long-iso {} \; 

will show a tree like method with directories in between the files in long list (1) or short list (2).

3
  • 4
    find has ls option so you could just do find . -type f -mtime -7 -ls
    – heemayl
    Nov 30, 2015 at 9:33
  • Sure but this makes it a bit more generic (I use this method to find files I need to -remove- and can change that command to do it :) )
    – Rinzwind
    Nov 30, 2015 at 10:03
  • 3
    Also it is more appropriate to use find ... -exec ls -l {} + which executes ls -l much more efficiently - fewer times with multiple parameters. This is a standard option of find specified by POSIX. Nov 30, 2015 at 12:30
8

With zsh:

ls -l **/*(.m-7)
  • **/* will look for files recursively starting from current directory

  • (.m-7) is glob qualifier where . indicates regular file, m-7 indicates files that were modified within last 7 days

3

The following command works a dream on Mac OSX - maybe also on ubuntu …

find . -type f -mtime -7 -exec stat -lt "%Y-%m-%d %H:%M:%S" {} \; | cut -d\  -f6- | sort -r

This finds files in the current directory tree which have been modified in the last 7 days, outputs the modification date + time and path, sorted newest first.

Example output:

2018-02-21 22:06:30 ./fmxmlsnippet.xml
2018-02-19 12:56:01 ./diff.html
2018-02-19 12:44:37 ./temp/iDDR/XMSC_fmxmlsnippet.xml
2018-02-18 22:04:05 ./temp/iDDR/XMFD_fmxmlsnippet.xml
2018-02-15 10:18:27 ./xml/iDDR/XML2_fmxmlsnippet.xml
2018-02-15 10:13:29 ./xsl/fmxmlsnippet/XML2_fmCM_AnalyseLayout.xsl
2018-02-15 10:11:36 ./xsl/.DS_Store
2018-02-15 10:10:51 ./xsl/_inc/inc.XML2_fmCM_ReportReferencesToExternalFiles.xsl
2018-02-15 10:10:09 ./xsl/_inc/.DS_Store
2018-02-15 10:07:35 ./xsl/fmxmlsnippet/XML2_fmCM_AnalyseLayout-NoAnchors.xsl
2018-02-15 10:07:35 ./xsl/_inc/inc.XML2_fmCM_AnalyseLayout.xsl

I'd be grateful of any feedback from ubuntu users.

2
  • The only solution that worked well for me on Mac. Thanks!
    – artnikpro
    Jul 2, 2019 at 15:50
  • Your welcome @artnikpro!
    – MrWatson
    Aug 18, 2019 at 11:13
1

Not exactly what was asked for... but much easier to remember...

ls -alRt docroot

or

ls -alRt /path/to/top/level/directory
1

tl;dr

find root.d -type f -newermt "-7 days"

or

find root.d -type f -newermt "$(date -d "$(date)-7days")"

Tested with bash using:

Ubuntu Find
23.04 4.9.0
22.04 4.8.0
20.04 4.7.0

also verified in macOS(darwin) zsh and bash

use the following to find out which versions you have

cat /etc/lsb-release
find --version

Assume the following directory structure and files with these relative modified timestamps

root.d/
├── file_c (-5 years)
└── level_1.d/
    ├── file_a (-45 minutes)
    └── file_b (-1 second)

example 1

find all the files recursively in root.d with a modifed date "newer than" 10 minutes ago

NEWER_THAN="-10 minutes"
find root.d -type f -newermt "$NEWER_THAN"

result

root.d/
└── level_1.d
    └── file_a

example 2

find all the files recursively in root.d with a modifed date "newer than" 10 hours ago and older than ("not newer than") 10 minutes ago

NEWER_THAN="-10 hours"
OLDER_THAN="-10 minutes"
find root.d -type f -newermt "$NEWER_THAN" -not -newermt "$OLDER_THAN"

result

root.d/
└── level_1.d
    └── file_b

example 3

find all the files recursively in root.d with a modifed date older than ("not newer than") 10 days ago

OLDER_THAN="-10 days"
find root.d -type f -not -newermt "$OLDER_THAN"

result

root.d/
└── file_c

considerations

after a few simple tests, I am confident the comparisons are strictly greater/less not equal. relevant discussion another simlar discussion

-newerXY reference
      Succeeds if timestamp X of the file being considered is
      newer than timestamp Y of the file reference.  The letters
      X and Y can be any of the following letters:

      a   The access time of the file reference
      B   The birth time of the file reference
      c   The inode status change time of reference
      m   The modification time of the file reference
      t   reference is interpreted directly as a time

find man page

0

7 days that's 60 seconds*60minutes*24hours*7days = 604800 seconds

Find out current date in seconds (Unix epoch time):

$ date +%s
1448876323

Subtract the 7 days in seconds:

expr $(date +%s) - 604800
1448271548

Now take stat command and print stats for all files in format "name + time in seconds" and use awk to crop off those files whose modification time is greater that that date we calculated

$ stat --printf="%n %Y\n" $HOME/* | awk '$2 > 1448271265 {print $0}'
/home/xieerqi/1448428697574.png 1448429613
/home/xieerqi/1448763343273.png 1448763478
/home/xieerqi/1510DRIVE 1448352453
/home/xieerqi/addRemoveDistribution 1448666843
/home/xieerqi/add-update.awk 1448716356
/home/xieerqi/add-update.sh 1448625092

Particularly of interest are last 3 files, because I know I was working them on less that 7 days ago. Thus I know it works

1
  • 2
    Note that instead of awk '$2 > 1448271265 {print $0}' you can diretly say awk '$2 > 1448271265'. On a true condition, awk prints the current line as a default action.
    – fedorqui
    Nov 30, 2015 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .