Is there a way on Linux bash to highlight all files modified in last N mins of current path - linux

I see a lot of similar questions, but answers of those questions don't quite meet my needs. The most common answer is to use command like find . -mmin -60 -maxdepth 1. But this command only outputs matched files, the thing I want is to output all files like what ls -al does, and highlight (like different color) all files which are modified in last N minutes. I don't know if it is possible to combine find and grep to achieve this goal, thank you for your help in advance!!

Assuming your version of find supports the printf action and you would like to highlight with, e.g., a leading ***, the following should work:
find . -maxdepth 1 -mmin -60 -printf '*** %p\n' -o -print
If you prefer colors, and if your terminal supports it, you can use ANSI escape codes. Example for green:
find . -maxdepth 1 -mmin -60 -printf '\033[32m%p\033[0m\n' -o -print
Explanations:
If the file or directory was modified less than 60 minutes ago it is the printf action that will we executed, else it is the print action.
By default find tests and actions are combined with logical AND. -o is the logical OR. It has lower precedence than AND. Here find stops as soon as one of two ORed terms evaluates as true.

Related

Linux count files with a specific string at a specific position in filename

I have a directory which contains data for several years and several months.
Filenames have the format yy/mm/dd, f.e.
20150415,
20170831,
20121205
How can I find all data with month = 3?
F.e.
20150302,
20160331,
20190315
Thanks for your help!
ls -ltra ????03??
A question mark is a wildcard which stands for one character, so as your format seems to be YYYYmmDD, the regular expression ????03?? should stand for all files having 03 as mm.
Edit
Apparently the files have format YYYYmmDDxxx, where xxx is the rest of the filename, having an unknown length. This would correspond with regular expression *, so instead of ????03?? you might use ????03??*.
As far as the find is concerned: the same regular expression holds here, but as you seem to be working inside a directory (no subdirectories, at first sight), you might consider the -maxdepth switch):
find . -name "????03??*" | wc -l // including subdirectories
find . -maxdepth 1 -name "????03??*" | wc -l // only current directory
I would highly advise you to check without wc -l first for checking the results. (Oh, I just see the switch -type f, that one might still be useful too :-) )

Shell script to find recently modified files [duplicate]

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.
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
Should be to your liking
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.
Another, more humanist way, is to use -newermt option which understands human-readable time units.
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 /<directory> -newermt "-24 hours" -ls
find /<directory> -newermt "1 day ago" -ls
find /<directory> -newermt "yesterday" -ls
You can do that with
find . -mtime 0
From man find:
[The] time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to
match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.
On GNU-compatible systems (i.e. Linux):
find . -mtime 0 -printf '%T+\t%s\t%p\n' 2>/dev/null | sort -r | more
This will list files and directories that have been modified in the last 24 hours (-mtime 0). It will list them with the last modified time in a format that is both sortable and human-readable (%T+), followed by the file size (%s), followed by the full filename (%p), each separated by tabs (\t).
2>/dev/null throws away any stderr output, so that error messages don't muddy the waters; sort -r sorts the results by most recently modified first; and | more lists one page of results at a time.
For others who land here in the future (including myself), add a -name option to find specific file types, for instance: find /var -name "*.php" -mtime -1 -ls
This command worked for me
find . -mtime -1 -print
Find the files...
You can set type f = file
find /directory_path -type f -mtime -1 -exec ls -lh {} \;
👍

Find file according to the access time using "grep" and "find" commands

My goal is to find all text files with extension .log, which have the last access more than 24 hours ago and contain the required text.
Here is what I have already tried:
find / *.log -mtime +1 -print | grep "next" *.log
but this doesn't work.
Question is: how can I reach the goal I have described above?Maybe some ways to modify my find expression?
The problem with your command is that you are running the grep on the output of the find command - which means you are running it on the file names, not content (actually, since you have the *.log at the end, you run it on all *.log files, completely ignoring what your find command found). also, you need -name in order to filter only the .log files.
you can use the -exec flag of find to execute a command on each of the files that matches your find criteria:
find / -name "*.log" -mtime +1 -exec grep 'next' \{};
Try with xargs:
find / -name "*.log" -mtime +1 | xargs grep "next"
But also, note what the find manual says about the arg to -atime which also applies to -mtime. That is, your mtime as specified probably doesn't get the time period you want.
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 accessed at least two days ago.

find files which have been modified in the last 30 minutes in Linux

how to find files based upon time information, such as creation, modified and accessed. It is useful to find files before a certain time, after a certain time and between two times. what command in Linux would i have to use ?
I understand to find setuid files on linux computers i would have to use :
find / -xdev ( -perm -4000 ) -type f -print0 | xargs -0 ls -l
How do i check for files which have been modified in the last 30 minutes. (I created a new file called FILE2)
Just add -mtime -30m. I might be wrong about the actual syntax, but you get the idea. See man find.
Answer on your question is
find . -cmin -30 -exec ls -l {} \;

find printf unable to print newlines or carriage returns

I am making a script to check and list the outputs of certain files in our job folder. I am wanting to check files that have been there today (or in the past 24 hours I guess).
Currently I am doing the following:
find /folder/jobfolder/rrz* -type f -mtime 0
so I get something like this:
/folder/jobfolder/rrzabc_1234.lis
/folder/jobfolder/rrzdef_4567.lis
/folder/jobfolder/rrzgre_8901.log
ideally I would like to use the printf command to add the date to it so I can see if it was today and separate it line by line. If I do
find /folder/jobfolder/rrz* -type f -mtime 0 -printf %p\t%AD\n It does not take use the escape characters.
/folder/jobfolder/rrzabc_1234.lis/folder/jobfolder/rrzdef_4567.lis/folder/jobfolder/rrzgre_8901.log
Is there a better way to approach this? also being able to use -iname to match capital or lowercase letters might be helpful
you are missing quotes; try
-printf '%p\t%AD\n'
so that bash does not interpret the \

Resources