Linux list modified files

Shell Script — Get all files modified after

I’d rather not do this in PHP so I’m hoping a someone decent at shell scripting can help. I need a script that runs through directory recursively and finds all files with last modified date is greater than some date. Then, it will tar and zip the file(s) keeping the path information.

You could use any of the decent shell scripting languages, eg python, ruby, perl without resorting to php.

@garrow This would actually be fairly simple in PHP using a RecursiveDirctoryIterator encaspulated in a FilterIterator.

9 Answers 9

find . -mtime -1 | xargs tar --no-recursion -czf myfile.tgz 

where find . -mtime -1 will select all the files in (recursively) current directory modified day before. you can use fractions, for example:

find . -mtime -1.5 | xargs tar --no-recursion -czf myfile.tgz 

Converting an arbitrary timestamp into a fractional, relative amount of time seems neither correct nor particularly convenient. The solution below using -newermt is much nicer.

When trying to extract any files with changes in a structure with directories, the directories themselves are marked modified causing the entire folder to be archived. Add -type f to only select files. This maintains the folder structure, but only backs up files with changes.

@BrianHenk user104848 changed their name, I’m pretty sure you mean this answer from (currently) Jason Luther though

The question asks to find files by an arbitrary date. This answer is how to find files by an arbitrary number of days in the past. I don’t think this should be the accepted answer without adding a step to convert a date into a day delta.

If you have GNU find , then there are a legion of relevant options. The only snag is that the interface to them is less than stellar:

  • -mmin n (modification time in minutes)
  • -mtime n (modification time in days)
  • -newer file (modification time newer than modification time of file)
  • -daystart (adjust start time from current time to start of day)
  • Plus alternatives for access time and ‘change’ or ‘create’ time.

The hard part is determining the number of minutes since a time.

One option worth considering: use touch to create a file with the required modification time stamp; then use find with -newer .

touch -t 200901031231.43 /tmp/wotsit find . -newer /tmp/wotsit -print rm -f /tmp/wotsit 

This looks for files newer than 2009-01-03T12:31:43. Clearly, in a script, /tmp/wotsit would be a name with the PID or other value to make it unique; and there’d be a trap to ensure it gets removed even if the user interrupts, and so on and so forth.

Instead, you can use -newermt ‘2009-01-03T12:31:43’ to avoid the need to create a file just for reference.

@MichaelMior: Interesting — the ‘find’ on MacOS X 10.7.2 supports -newermt (documented under -newerXY since there are multiple options; X = m, Y = t in this case). The GNU find on the (almost archaic) Linux systems I use does not support the options, so it is a relatively recent arrival. Thanks for the heads up.

Читайте также:  Стандартные пароли кали линукс

-newermt ‘2009-01-03T12:31:43’ looks awesome, but sadly it seems to interpret everything in the wrong timezone: lists.gnu.org/archive/html/bug-findutils/2012-12/msg00009.html

You can do this directly with tar and even better:

tar -N '2014-02-01 18:00:00' -jcvf archive.tar.bz2 files 

This instructs tar to compress files newer than 1st of January 2014, 18:00:00.

The implementations of tar that come with OS X and FreeBSD only support —newer which is the long option for -N in GNU tar .

This is awesome. I could only get it to work with bzip, not using -xcvf and a gz extension. but I’m no expert.

This will work for some number of files. You want to include «-print0» and «xargs -0» in case any of the paths have spaces in them. This example looks for files modified in the last 7 days. To find those modified before the last 7 days, use «+7».

find . -mtime -7 -print0 | xargs -0 tar -cjf /foo/archive.tar.bz2 

As this page warns, xargs can cause the tar command to be executed multiple times if there are a lot of arguments, and the «-c» flag could cause problems. In that case, you would want this:

find . -mtime -7 -print0 | xargs -0 tar -rf /foo/archive.tar 

You can’t update a zipped tar archive with tar, so you would have to bzip2 or gzip it in a second step.

Regarding the second point, you can use a gzip-enabled tar command, like Gnu tar’s -z option, to update zipped archives, too.

As I said above on Jonathan’s answer, this will grab the folders which have changes causing all of their files to be imported. Add -type f to only select files. This maintains the folder structure, but only backs up the files with changes/added recently.

This should show all files modified within the last 7 days.

find . -type f -mtime -7 -print 

Pipe that into tar/zip, and you should be good.

I would simply do the following to backup all new files from 7 days ago

tar --newer $(date -d'7 days ago' +"%d-%b") -zcf thisweek.tgz . 

note you can also replace ‘7 days ago’ with anything that suits your need

Can be : date -d’yesterday’ +»%d-%b»

Or even : date -d’first Sunday last month’ +»%d-%b»

well under linux try reading man page of the find command

something like this should

 find . -type f -mtime -7 -print -exec cat <> \; | tar cf - | gzip -9 

You can get a list of files last modified later than x days ago with:

Then you just have to tar and zip files in the resulting list, e.g.:

tar czvf mytarfile.tgz `find . -mtime -30` 

for all files modified during last month.

«-mtime x» matches files modified exactly «x» days ago, use «-mtime -x» to match files modified less than «x» days ago.

This script will find files having a modification date of two minutes before and after the given date (and you can change the values in the conditions as per your requirement)

PATH_SRC="/home/celvas/Documents/Imp_Task/" PATH_DST="/home/celvas/Downloads/zeeshan/" cd $PATH_SRC TODAY=$(date -d "$(date +%F)" +%s) TODAY_TIME=$(date -d "$(date +%T)" +%s) for f in `ls`; do # echo "File -> $f" MOD_DATE=$(stat -c %y "$f") MOD_DATE=$ # echo MOD_DATE: $MOD_DATE MOD_DATE1=$(date -d "$MOD_DATE" +%s) # echo MOD_DATE: $MOD_DATE DIFF_IN_DATE=$[ $MOD_DATE1 - $TODAY ] DIFF_IN_DATE1=$[ $MOD_DATE1 - $TODAY_TIME ] #echo DIFF: $DIFF_IN_DATE #echo DIFF1: $DIFF_IN_DATE1 if [[ ($DIFF_IN_DATE -ge -120) && ($DIFF_IN_DATE1 -le 120) && (DIFF_IN_DATE1 -ge -120) ]] then echo File lies in Next Hour = $f echo MOD_DATE: $MOD_DATE #mv $PATH_SRC/$f $PATH_DST/$f fi done 

For example you want files having modification date before the given date only, you may change 120 to 0 in $DIFF_IN_DATE parameter discarding the conditions of $DIFF_IN_DATE1 parameter.

Читайте также:  Postgresql создать таблицу linux

Similarly if you want files having modification date 1 hour before and after given date, just replace 120 by 3600 in if CONDITION .

Источник

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.

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.

Читайте также:  Запрос адреса dhcp linux

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.

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.

Источник

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