Linux get last modified file

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 

Источник

How to Find Last Modified Files in Linux?

This tutorial explains how to find last modified files in Linux using different commands and according to custom needs.

After reading this tutorial you’ll know how to execute the following tasks:

  • How to find files modified in a specific day range
  • How to find last modified specific file type (e.g mp4, png)
  • Finding files modified before / after X minutes
  • How to find files modified in a specific date
  • Finding modified files recursively
  • Search omitting files or directories
  • Find files by access date

Finding last day modified files in Linux:

To start, let’s search files modified less than a day ago. To find files modified a day ago you can use the commands find and newermt used in the following example.

Читайте также:  Linux application running on windows

The find command is used to search files. The newermt command compares files timestamp with the argument passed, in this case “1 day ago”. Then, the ls command is passed to list the files.

To find last day modified files, you can also use the mtime command together with find. By specifying the option 0 as in the example below, mtime will return all files modified in the last 24 hours.

Find Last Modified Specific File Type in Linux:

You can use a wildcard to limit your search to a specific file type. In the following example, find and newermt are instructed to list all mp4 files modified a day ago.

cc lang=”bash” width=”100%” height=”100%” escaped=”true” theme=”blackboard”]$ find /home/linuxhint/*.mp4 -newermt “1 day ago” -ls[/cc

In the following example, find and newermt are used to find all .png images less than 15 days old.

Finding Last Hour Modified Files in Linux:

The following example combines the find command with the mmin command. We can use the mmin command to specify minutes. In the example below, the find and mmin commands will print all files under the /root directory, whose modifications are less than 60 minutes old.

Contrary to the previous example in which files modified in the past 60 minutes were found. You can also use +mmin to search files modified after X minutes. For example, the following command will show files modified 60 minutes ago or more.

Finding Files Modified on a Specific Date in Linux:

You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.

Then you can combine ls -lt with grep to print all files which were modified on a specific date.

Find Last Modified Files Recursively:

Previous examples are useful to find last modified files

The command below can be used to print last modified files recursively.

Search File by Date Omitting Files or Directories:

Contrary to the previous example, you can search files omitting directories. For this purpose, you need to implement the -type flag with the option f (file) as shown in the following example. As a result, you’ll see only final files and no directories.

You can also search directories only and the output will omit files. For this, just replace the f with a d after the -type flag.

Find Files by Access Date:

You also may want to find unmodified files by access date. For this purpose, you can use the atime command. It is similar to the mtime command explained before, but instead of identifying files by modification, it can display files by access. With this command you can learn the last accessed files and directories in the system.

Читайте также:  Linux command line command history

The following command shows all files accessed in the past 10 days.

Like the previous command, you can also use the d option to show only directories:

If you don’t specify a type, atime will show all files and directories:

In the following example, find and atime are used to find files and directories with modification older than 20 days.

As with previous examples, you can also limit the listing to files or directories with the -type flag.

Conclusion:

As you can see, Linux offers different methods to find files according to modification time. Any Linux user level can easily learn those methods to search files with a single command. Finding files by modification or access within a system is part of the basic knowledge a Linux user needs.

I hope this tutorial was useful. Keep following Linux Hint for more Linux tips and tutorials.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

I can’t seem to find how to print out the date of a file. I’m so far able to print out all the files in a directory, but I need to print out the dates with it. I know I need to attach a date format with the echo of the entry, but all I can’t find the correct format.

echo "Please type in the directory you want all the files to be listed" read directory for entry in "$directory"/* do echo "$entry" done 

10 Answers 10

Isn’t the ‘date’ command much simpler? No need for awk, stat, etc.

Also, consider looking at the man page for date formatting; for example with common date and time format:

It looks like BSD (or at least OS X’s) date ‘s doesn’t have this. Its -r is just used to provide a timestamp to format. You’ll have to use GNU date to get this functionality.

In macOS 10.13 date [-r seconds | filename] is fully documented in the man page, and works as expected.

%y time of last modification, human-readable

for entry in «$directory»/* do stat -c%y «$entry» done Doesn’t work. Prints out stat: missing operand in terminal

Note that on BSD the stat command has a different syntax. My case for FreeBSD: stat -f %Sm -t %F» «%R filename .

Alternatively, you may try also :

On OS X, I like my date to be in the format of YYYY-MM-DD HH:MM in the output for the file.

So to specify a file I would use:

stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename] 

If I want to run it on a range of files, I can do something like this:

#!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i" done 

This example will print out the last time I ran the sudo periodic daily weekly monthly command as it references the log files.

Читайте также:  Astra linux special edition основные компоненты

To add the filenames under each date, I would run the following instead:

#!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i" echo "$i" done 

The output would was the following:

2016-40-01 16:40 /var/log/daily.out 2016-40-01 16:40 /var/log/monthly.out 2016-40-01 16:40 /var/log/weekly.out 

Unfortunately I’m not sure how to prevent the line break and keep the file name appended to the end of the date without adding more lines to the script.

PS — I use #!/usr/bin/env bash as I’m a Python user by day, and have different versions of bash installed on my system instead of #!/bin/bash

In case you haven’t figured out the line break part, the -e flag is available for the echo program. Example usage could be: echo -e «$(stat -f %Sm -t %Y%m%d_%H%M%S $AFile)\t$AFile.» . In your case it could be: echo -e «$(stat -f «%Sm» -t «%Y-%m-%d %H:%M» «$i»)\t$i.» `

Adding to @StevePenny answer, you might want to cut the not-so-human-readable part:

stat -c%y Localizable.strings | cut -d'.' -f1 

For the line breaks i edited your code to get something with no line breaks.

#!/bin/bash for i in /Users/anthonykiggundu/Sites/rku-it/*; do t=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i") echo $t : "$" # t only contains date last modified, then only filename 'grokked'- else $i alone is abs. path done 

If file name has no spaces:

This prints as the following format:

 Dec 21 20:03 a1.out Dec 21 20:04 a.cpp 

If file names have space (you can use the following command for file names with no spaces too, just it looks complicated/ugly than the former):

Ah, I see! That mostly works, except for the files with the spaces in the names. Is there a solution for that?

Actually, I tried it again, and it doesn’t work. turns out I was trying out a directory that had file names without spaces. =/ Edited my answer below

when you say it does not work, can you say what is the error? it works for me when i executed it on my system.

EDITED: turns out that I had forgotten the quotes needed for $entry in order to print correctly and not give the «no such file or directory» error. Thank you all so much for helping me!

 echo "Please type in the directory you want all the files to be listed with last modified dates" #bash can't find file creation dates read directory for entry in "$directory"/* do modDate=$(stat -c %y "$entry") #%y = last modified. Qoutes are needed otherwise spaces in file name with give error of "no such file" modDate=$ #%% takes off everything off the string after the date to make it look pretty echo $entry:$modDate 
/home/joanne/Dropbox/cheat sheet.docx:2012-03-14 /home/joanne/Dropbox/Comp:2013-05-05 /home/joanne/Dropbox/Comp 150 java.zip:2013-02-11 /home/joanne/Dropbox/Comp 151 Java 2.zip:2013-02-11 /home/joanne/Dropbox/Comp 162 Assembly Language.zip:2013-02-11 /home/joanne/Dropbox/Comp 262 Comp Architecture.zip:2012-12-12 /home/joanne/Dropbox/Comp 345 Image Processing.zip:2013-02-11 /home/joanne/Dropbox/Comp 362 Operating Systems:2013-05-05 /home/joanne/Dropbox/Comp 447 Societal Issues.zip:2013-02-11 

Источник

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