How to find the files that are created in the last hour in unix - unix

How to find the files that are created in the last hour in unix

If the dir to search is srch_dir then either
$ find srch_dir -cmin -60 # change time
or
$ find srch_dir -mmin -60 # modification time
or
$ find srch_dir -amin -60 # access time
shows files whose metadata has been changed (ctime), the file contents itself have been modified (mtime), or accessed (atime) in the last hour, respectively.
ctime is non-unintuitive and warrants further explanation:
ctime:
Unlike mtime, which is only related to the contents inside a file, changed timestamp indicates the last time some metadata of a file was changed. ctime refers to the last time when a file’s metadata.
For example, if permission settings of a file were modified, ctime will indicate it.

UNIX filesystems (generally) don't store creation times. Instead, there are only access time, (data) modification time, and (inode) change time.
That being said, find has -atime -mtime -ctime predicates:
$ man 1 find
...
-ctime n
The primary shall evaluate as true if the time of last change of
file status information subtracted from the initialization time,
divided by 86400 (with any remainder discarded), is n.
...
Thus find -ctime 0 finds everything for which the inode has changed (e.g. includes file creation, but also counts link count and permissions and filesize change) less than an hour ago.

check out this link and then help yourself out.
the basic code is
#create a temp. file
echo "hi " > t.tmp
# set the file time to 2 hours ago
touch -t 200405121120 t.tmp
# then check for files
find /admin//dump -type f -newer t.tmp -print -exec ls -lt {} \; | pg

find ./ -cTime -1 -type f
OR
find ./ -cmin -60 -type f

sudo find / -Bmin 60
From the man page:
-Bmin n
True if the difference between the time of a file's inode creation and
the time find was started, rounded up to the next full minute, is n
minutes.
Obviously, you may want to set up a bit differently, but this primary seems the best solution for searching for any file created in the last N minutes.

Check out this link for more details.
To find files which are created in last one hour in current directory, you can use -amin
find . -amin -60 -type f
This will find files which are created with in last 1 hour.

Related

I need to check the logs from current time to before 2 hours

I need to check the logs from current time to before 2 hours, can you please suggest me a command
For listing all logs in the current directory within last 2 hours (120 mins)
find . -mmin -120 -maxdepth 1
for logs within in last 1 day in the current directory.
find . -mtime -1 -maxdepth 1
You can use the find command to find all files that have been modified after a certain period of time.
Source : https://unix.stackexchange.com/questions/10041/how-to-get-only-files-created-after-a-date-with-ls

How to check if a file is older than 1 week and then move to archive folder

How to check if a file is older than 1 week and then move to archive folder.Using some if condition we should be checking the file is exists which is 1 week older.Then try to move the same to archive
This should work:
find <path> -atime +1w -exec mv {} <dest_dir> \;
Argument -atime controls last access time, you may want to use ctime (creation timestamp) or mtime (last modification timestamp). +1w means more than one week.
Argument exec let find execute some command on the files that meet the criterion. {} is a place to be filled with the path of the files found.
Read the manual for more criteria.

difference between mtime +0 and mtime 0

I an using mtime with find . I am duing it for first time .
I see a script which moves file from one location to other.
`find . \ -mtime +0 -exec mv {} target \ ;`
I want to understand does +0 means only for file created before 24 hrs?
Now as these files are moved to target i also want to modify there permission to one appropiate for everyone to read. So i used the command
`find target -mtime 0 -exec chmod 644 {} \;`
I want to really get hole of difference between usage of +0 , 0 . Does 0 only signified between now and 24 hr while +0 is from 24 and more old ? My main purpose here is to modify the permission of all files in target dir where i have just moved them . As they can be many i jst want to run one command and do it all.
I recommend that you consult the documentation prior to asking for help. The man-age for find explains numeric test:
+n for greater than n,
-n for less than n,
n for exactly n.
and clarifies their interpretation with regard to file timestamp tests -atime, -ctime, and -mtime:
-mtime n
File's data was last modified less than, more
than or exactly n*24 hours ago. See the com-
ments for -atime to understand how rounding af-
fects the interpretation of file modification
times.
-atime n
File was last accessed less than, more than or
exactly n*24 hours ago. When find figures out
how many 24-hour periods ago the file was last
accessed, any fractional part is ignored, so to
match -atime +1, a file has to have been ac-
cessed at least two days ago.
As you can see, +0 means older than 24 hours and 0 within the last 24 hours.
+n for greater than n days,
-n for less than n days,
n for exactly n days.
+0 - all files older than now , which will be all files in your directory
-0 - all files newer than now , means no files
0 - all files modified now - you will get /root/.bash_history if you are doing it in /root folder as it gets modified instantly
for 24hrs you can use +1,-1,1
+1 will be all files which are older than 1 day
-1 will be all files which are old up to 1 day
1 will be all files which are exact 1 day old

finding files in unix based on creation/modification date

How to find files on a unix server which were created/modified in previous month?
for ex. If the current month is Jul then the list of files which were created/modified in Jun should get displayed.
One way is to execute that command.
ls -laR | grep monthName where montName could be Jan,Feb,Mar, and so on ... (remember to change working directory to directory that you're interested in. Also notice that this method is recursive so all sub-directories will be inspected
With this you also retrieve all file permission and so on...
I'm sure that will be better ways (if them jump into my mind, I'll edit this post) but since I'm in coffee break, this is the faster that I find.
In order to find files modified in the previous month, you will need to use find with a set range, for example:
cd / (if you are wanting to start from the root)
find . -type f -mtime +26d -mtime -56d -print
You should adjust your range to include the dates that you wish to include.
All the best to you!
monthToFind=`date -d "1 months ago" "+%Y-%m"`
find . -printf "%TY-%Tm %p\n" | egrep "^$monthToFind " | sed "s/^$monthToFind //g"
This will be slower than using a time range in find. But the time range is hard to determine, and quickly becomes invalid, possibly even while the command is executing.
Unfortunately this will miss files modified last month when they were also modified this month. I don't know of a way to determine these files.

How to count number of lines in the files which created today

Well,I m trying to list the number of files created today and the count the number of lines in those files.I have to do it in unix.Please suggest how to write script for this.
To find the number of lines:
find / -type f -ctime 0 -mtime 0 -print0 | xargs -0 wc -l
This is almost what you want. There is no file created time in Unix, this is approximation with both file status changed time and file modified time.
If you would like to search only in certain directory, replace / with /path/to/your/dir.
To find the number of files:
find / -type f -ctime 0 -mtime 0 | wc -l
This will find files (-type f) in /path modified in the last 24 hours (-mtime -1 means modified in the last 1 day) and run wc -l to count the number of lines. {} is a placeholder for the file names and + means pass all the file names to a single invocation of wc.
find /path -mtime -1 -type f -exec wc -l {} +
Note that -ctime as suggested in other answers is change time, not creation time. It is the last time a file's owner, group, link count, mode, etc., was changed. Unix does not track the creation time of a file.
find . -maxdepth 1 -daystart -ctime 0 -type f | xargs wc -l
You'll need to change the maxdepth argument value if you need to look deeper.
To count the number of files changed today:
find . -daystart -type f -ctime -1 | wc -l
find finds all the files (-type f) in the current directory (.) created* (-ctime) more recently (-) than one (1) day since the start of this day (-daystart). wc counts the number of lines (-l) in find's output.
To count the lines in those files:
find -daystart -type f -ctime -1 -print0 | wc -l --files0-from=-
The first part is the same, except that find separates the filenames using nulls (-print0). wc counts the lines (-l) in the null-separated files (--files0-from=) on its standard input (-).
* ctime is not actually the creation time, but the time when the file's status was last changed. I don't think the filesystem holds on to the actual creation time.
Determining when a file was created reliably is hard. The mtime is when it was modified, the ctime is when the inode data was changed (change of permissions, for example), the atime is when the file data was last accessed. Usually, the mtime is surrogate for the create time; when a file is created, it records the creation time (as does the ctime and atime), but if the file is subsequently modified, the mtime records the time when the contents of the file was last modified.
find . -mtime -1 -print0 | xargs -0 wc -l
Find all the files under the current directory with a modification time less than 24 hours old and send the names to 'wc -l' - allowing for spaces and other odd characters in the file names.

Resources