Linux list more recent files

List all recently changed files (recursive)

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?

5 Answers 5

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

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

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.

  • **/* 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

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.

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.

Читайте также:  Writing kernel module linux

Источник

Find latest files

How do I find out the most recently accessed file in a given directory? I can use the find command to list out all files modified/accessed in last n minutes. But here in my case, I’m not sure when the last file was modified/accessed? All that I need is to list all the files which were accessed/modified very recently among all other sub-files or sub-directories, sorted by their access/modified times, for example. Is that possible?

Your question is unclear. Are you saying you want to take the list of files from find and sort them by date?

7 Answers 7

To print the last 3 accessed files (sorted from the last accessed file to the third last accessed file):

find . -type f -exec stat -c '%X %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' 

To print the last 3 modified files (sorted from the last modified file to the third last modified file):

find . -type f -exec stat -c '%Y %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' 
  • find . -type f -exec stat -c ‘%X %n’ * : prints the last access’ time followed by the file’s path for each file in the current directory hierarchy;
  • find . -type f -exec stat -c ‘%Y %n’ * : prints the last modification’s time followed by the file’s path for each file in the current directory hierarchy;
  • sort -nr : sorts in an inverse numerical order;
  • awk ‘NR==1,NR==3 ‘ : prints the second field of the first, second and third line.

You can change the number of files to be shown by changing 3 to the desired number of files in awk ‘NR==1,NR==3 ‘ .

% touch file1 % touch file2 % touch file3 % find . -type f -exec stat -c '%X %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file3 ./file2 ./file1 % find . -type f -exec stat -c '%Y %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file3 ./file2 ./file1 % cat file1 % find . -type f -exec stat -c '%X %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file1 ./file3 ./file2 % find . -type f -exec stat -c '%Y %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file3 ./file2 ./file1 % touch file2 % find . -type f -exec stat -c '%X %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file2 ./file1 ./file3 % find . -type f -exec stat -c '%Y %n' <> \; | sort -nr | awk 'NR==1,NR==3 ' ./file2 ./file3 ./file1 

@SHW Not sure what you mean. The files are sorted based on the number of seconds passed from January 1st 1970.

Читайте также:  Astra linux mount usb

To help other users: if you want to scan another directory than the current directory, replace the . directly after the find . It took me a couple of minutes to understand it.

These commands break for filenames that contains whitespaces. The culprit is awk and as I don’t know how to use it, simply replacing the awk command with | head -n 3 (to keep the first three results) does the trick. If you stil want to remove the timestamps, chain it with | cut -d’ ‘ -f2-

You could use the recursive switch ( -R ) to ls along with the sort by time switch ( -t ) and the reverse sort switch ( -r ) to list out all the files in a directory tree. This will not sort all the files by their access/modify dates across sub-directories, but will sort them by this date within each sub-directory independently.

Using a command such as this: ls -ltrR .

Example

$ ls -ltrR . total 759720 -rw-r-----@ 1 sammingolelli staff 2514441 Mar 31 2015 restfulapi-120704053212-phpapp01.pdf -rw-r-----@ 1 sammingolelli staff 567808 Apr 7 2015 USGCB-Windows-Settings.xls -rw-r-----@ 1 sammingolelli staff 180736 Apr 7 2015 USGCB-RHEL5-Desktop-Settings-Version-1.2.5.0.xls -rw-r-----@ 1 sammingolelli staff 6474 Apr 8 2015 tap_kp_mavericks.txt ./kerberos: total 5464 -rw-r-----@ 1 sammingolelli staff 37317 Oct 2 13:03 Set_up_Kerberos_instruction_d8.docx -rw-r-----@ 1 sammingolelli staff 2753195 Oct 13 13:49 Keberos configuration with AD 01_09_2014.pdf ./homestarrunner: total 10624 -rw-rw-rw-@ 1 sammingolelli staff 319422 May 10 2000 error_hs.wav -rw-rw-rw-@ 1 sammingolelli staff 53499 Jun 8 2001 sb_duck.mp3 -rw-rw-rw-@ 1 sammingolelli staff 199254 Mar 11 2002 email_sb.wav -rw-rw-rw-@ 1 sammingolelli staff 39288 Mar 25 2002 bubs_dontutalk.mp3 -rw-rw-rw-@ 1 sammingolelli staff 75432 May 6 2002 trash_sb.wav -rw-rw-rw-@ 1 sammingolelli staff 298946 Dec 1 2002 error_sb.wav -rw-rw-rw-@ 1 sammingolelli staff 298686 Dec 1 2002 startup_hs.wav -rw-rw-rw-@ 1 sammingolelli staff 90279 Dec 1 2002 sb_meedlymee.mp3 -rw-rw-rw-@ 1 sammingolelli staff 73561 Dec 1 2002 sb_dubdeuce.mp3 -rw-rw-rw-@ 1 sammingolelli staff 193097 Dec 1 2002 sb_pizza.mp3 -rw-rw-rw-@ 1 sammingolelli staff 30093 Dec 1 2002 sb_stiny.mp3 -rw-rw-rw-@ 1 sammingolelli staff 61858 Dec 1 2002 ss_sadflying.mp3 -rw-rw-rw-@ 1 sammingolelli staff 150142 Dec 1 2002 email_hs.wav -rw-rw-rw-@ 1 sammingolelli staff 68545 Dec 1 2002 bubs_grabbinbutt.mp3 -rw-rw-rw-@ 1 sammingolelli staff 61022 Dec 1 2002 cz_jeorghb.mp3 -rw-rw-rw-@ 1 sammingolelli staff 40124 Dec 1 2002 marzy_nasty.mp3 -rw-rw-rw-@ 1 sammingolelli staff 224116 Dec 1 2002 shutdown_sb.wav -rw-rw-rw-@ 1 sammingolelli staff 260546 Dec 1 2002 shutdown_hs.wav -rw-rw-rw-@ 1 sammingolelli staff 57686 Dec 1 2002 trash_hs.wav 

Источник

Читайте также:  Блокировать ip адрес linux

Find the files that have been changed in last 24 hours

E.g., a MySQL server is running on my Ubuntu machine. Some data has been changed during the last 24 hours. What (Linux) scripts can find the files that have been changed during the last 24 hours? Please list the file names, file sizes, and modified time.

7 Answers 7

To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:

find /directory_path -mtime -1 -ls 

The — before 1 is important — it means anything changed one day or less ago. A + before 1 would instead mean anything changed at least one day ago, while having nothing before the 1 would have meant it was changed exacted one day ago, no more, no less.

The argument to -mtime is interpreted as the number of whole days in the age of the file. -mtime +n means strictly greater than, -mtime -n means strictly less than.

Another, more humanist way, is to use -newermt option which understands human-readable time units (see man find and search for -newerXY ).

Unlike -mtime option which requires the user to read find documentation to figure our what time units -mtime expects and then having the user to convert its time units into those, which is error-prone and plain user-unfriendly. -mtime was barely acceptable in 1980s, but in the 21st century -mtime has the convenience and safety of stone age tools.

Example uses of -newermt option with the same duration expressed in different human-friendly units:

find / -newermt "-24 hours" -ls find / -newermt "1 day ago" -ls find / -newermt "yesterday" -ls 

Источник

Оцените статью
Adblock
detector