linux - How to List Files greater than particular timestamp?
2014-07
I have a directory in linux which has a list of log files where log files get auto generated if some job runs. Each log file gets appended with the timestamp like "JobName_TimeStamp"
UPDATED:
job_2014-05-28_15:05:26.log
job_2014-05-28_15:06:58.log
job_2014-05-28_15:07:02.log
job_2014-05-28_15:07:57.log
job_2014-05-28_15:08:00.log
job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log
job1_2014-05-28_15:08:11.log
job1_2014-05-28_15:08:12.log
job1_2014-05-28_15:08:13.log
job1_2014-05-28_15:08:14.log
job1_2014-05-28_15:08:22.log
I wanted to run a linux command to list all files greater than a particular timestamp?
For Example 1 : I will pass two parameters , If the TimeStamp given is "2014-05-28_15:08:00" and Job Name is "job"
I should get the list as
job_2014-05-28_15:08:01.log
job_2014-05-28_15:08:09.log
job_2014-05-28_15:08:10.log
job_2014-05-28_15:08:11.log
job_2014-05-28_15:08:12.log
job_2014-05-28_15:08:13.log
job_2014-05-28_15:08:14.log
job_2014-05-28_15:08:22.log
Example 2 : I will pass two parameters , If the TimeStamp given is "2014-05-28_15:08:11" and Job Name is "job1"
I should get the list as
job1_2014-05-28_15:08:12.log
job1_2014-05-28_15:08:13.log
job1_2014-05-28_15:08:14.log
job1_2014-05-28_15:08:22.log
Any solutions?
Thanks.
The following command, which can easily be scripted, will do it for you:
for i in $(ls *); do
if [[ "job_2014-05-28_15:08:00.log" < "$i" ]]; then
echo $i
fi
done
EDIT:
Suppose you want to do this for just jobs called myjob, then modify the above as follows:
for i in $(ls myjob*); do
if [[ "myjob_2014-05-28_15:08:00.log" < "$i" ]]; then
echo $i
fi
done
Simple find command:
find . -maxdepth 1 -type f -name job1_\* -newer job1_2014-05-28_15:08:11.log
There are 2 assumptions in this example:
- mtime of log files correlates to timestamps in file names
- you can provide particular file name that is the lower bound for all wanted log files
how do I find a phrase/word recursively in a file tree in Linux?
I tried find . -name ./* | grep my_phrase
and I tried grep -r "register_long_arrays" *
grep -r "register_long_arrays" *
will recursively find all occurrences of register_long_arrays
in your current directory.
If you want to combine find
with grep
to limit the types of files searched you should use it like this (this example will limit the search to files ending .txt
):
find . -name '*.txt' -exec grep "register_long_arrays" {} \;
The {}
is replaced with every file that find
finds without you having to worry about escaping problem characters like spaces. Note the backslash before the semicolon. This ensures that the semicolon is escaped and passed to the exec clause (grep) rather than terminating the find command.
If you're still not finding what you want it may be a case issue, (supply the -i
flag to grep to ignore case) or perhaps the content you want is in a hidden file (starting with .
), which *
will not match, in which case supply both *
and .*
as arguments to grep.
find . -type f -exec grep my_phrase {} \;
will work to search all regular files, but it invokes grep
once for every file. This is inefficient and will make the search take noticeably longer on slow computers and/or large directory hierarchies. For that reason, it is better to use xargs
:
find . -type f | xargs grep my_phrase
or, if you have files whose names contain spaces,
find . -type f -print0 | xargs -0 grep my_phrase
Even simpler is to just use the -r
option to grep
:
grep -r my_phrase .
Note the use of . rather than * as the file name argument. That avoids the problem of the expansion of * not including hidden files.
try:
find . -name ./\* | xargs grep my_phrase
xargs
will call grep with on each of the files that find
finds.
You can also try ack. It's fast. If you work with trees of code it's great tool.
Searching for foobar in file contents recursively from current directory:
ack foobar
Searching for files which names match regexp:
ack -g "foo(bar)?"
find . -exec grep my_phrase {} \;
find
generates a list of all files at or below current directory. exec
will execute the command grep my_phrase
for each file found. The \;
is just magic to make it work. Details of how it works can usually be seen with man find
.
You might also want to look at the -H
option of grep
if you want to know not just if it was found but where.