16

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?

30

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

  • 4
    find has ls option so you could just do find . -type f -mtime -7 -ls – heemayl Nov 30 '15 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 '15 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. – pabouk Nov 30 '15 at 12:30
5

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

1

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

ls -alRt docroot

or

ls -alRt /path/to/top/level/directory
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

  • 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 '15 at 16:09
0

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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.