Find files that changed today linux

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.

Читайте также:  Графические оболочки linux unity

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

Читайте также:  Установка sane astra linux

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.

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 Recent or Today’s Modified Files in Linux

In this article, we will explain two, simple command line tips that enable you to only list all today’s files.

One of the common problems Linux users encounter on the command line is locating files with a particular name, it can be much easier when you actually know the filename.

However, assuming that you have forgotten the name of a file that you created (in your home folder which contains hundreds of files) at an earlier time during the day and yet you need to use urgently.

Below are different ways of only listing all files that you created or modified (directly or indirectly) today.

1. Using the ls command, you can only list today’s files in your home folder as follows, where:

  1. -a – list all files including hidden files
  2. -l – enables long listing format
  3. —time-style=FORMAT – shows time in the specified FORMAT
  4. +%D – show/use date in %m/%d/%y format
# ls -al --time-style=+%D | grep 'date +%D'

Find Recent Files in Linux

In addition, you can sort the resultant list alphabetically by including the -X flag:

# ls -alX --time-style=+%D | grep 'date +%D'

You can also list based on size (largest first) using the -S flag:

# ls -alS --time-style=+%D | grep 'date +%D'

2. Again, it is possible to use the find command which is practically more flexible and offers plenty of options than ls, for the same purpose as below.

  1. -maxdepth level is used to specify the level (in terms of sub-directories) below the starting point (current directory in this case) to which the search operation will be carried out.
  2. -newerXY , this works if timestamp X of the file in question is newer than timestamp Y of the file reference. X and Y represent any of the letters below:
    1. a – access time of the file reference
    2. B – birth time of the file reference
    3. c – inode status change time of reference
    4. m – modification time of the file reference
    5. t – reference is interpreted directly as a time

    This means that, only files modified on 2016-12-06 will be considered:

    # find . -maxdepth 1 -newermt "2016-12-06"

    Find Today

    Important: Use the correct date format as reference in the find command above, once you use a wrong format, you will get an error as the one below:

    # find . -maxdepth 1 -newermt "12-06-2016" find: I cannot figure out how to interpret '12-06-2016' as a date or time 

    Alternatively, use the correct formats below:

    # find . -maxdepth 1 -newermt "12/06/2016" OR # find . -maxdepth 1 -newermt "12/06/16"

    Find Todays Modified Files in Linux

    You can get more usage information for ls and find commands in our following series of articles on same.

    In this article, we explained two important tips of how to list only today’s files with the help of ls and find commands. Make use of the feedback form below to send us any question(s) or comments about the topic. You can as well inform us of any commands used for the same goal.

    Источник

    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