I am trying following ...
ls -ltrh | grep 'Dec 2'
but it is displaying files for last year also ..as this dir is full of files from last 3-5 yrs
I only want to files for today.
e.g .
ls -ltrh | grep 'Dec 2'
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text23.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text22.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text21.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text20.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 test.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 test1.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 text3.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 text4.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:37 test11.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:37 text12.txt
CarloM
December 2, 2011, 5:39am
2
Try
ls -ltrh | grep "Dec 2 .*:"
thanks but it is not working...no o/p.
ls -l | perl -ne '/Dec\s+2 [0-9]{2}:[0-9]{2}/ && print $_'
CarloM
December 2, 2011, 5:47am
5
Depending on your OS you might need to use egrep rather than grep. Also, check if your input is
Dec 2
or
Dec 2
EDIT: Hmm, [icode] tags compress spaces!
hi in /ksh korn shell there is no command as ls -h So please tell which shell u are using to run this command
I want to search for all the files for today ...
ls -l | perl -ne '/Dec\s+2 [0-9]{2}:[0-9]{2}/ && print $_'
works but I can not hard code Dec\s+2 everyday ...ideally it should take the date dynamically and show me files only for today.
---------- Post updated at 05:54 AM ---------- Previous update was at 05:52 AM ----------
FYI i am using SunOS
uname -v
Generic_122300-60
find . -type f -mtime -1 -maxdepth 1
i am using sh shell
---------- Post updated at 05:57 AM ---------- Previous update was at 05:55 AM ----------
maxdepth does not work for me
find . -type f -mtime -1 -maxdepth 1
find: bad option -maxdepth
find: path-list predicate-list
ls -l | perl -ne '$x=substr localtime, 4, 6; /$x/ && print $_'
this is displaying 2010 files also
-rw-r----- 1 ajay ajay 0 Dec 2 20:37 test11.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 test1.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 test.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:37 text12.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text20.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text21.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text22.txt
-rw-r----- 1 ajay ajay 0 Dec 2 2010 text23.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 text3.txt
-rw-r----- 1 ajay ajay 0 Dec 2 20:32 text4.txt
CarloM
December 2, 2011, 6:01am
12
ls -ltrh | egrep "`date | cut -c5-10`.*:"
Works for me on a SunOS 5.9 system
ls -l | perl -ne '$x=substr localtime, 4, 6; /$x\s+[0-9]{2}:[0-9]{2}/ && print $_'
Thanks CarloM this is working fine ...will it work at my OS always ...irrespective of if there exists 2009 , 2008 files at dir ...thanks again ,..can u please explain cut -c5-10`.*:" part
---------- Post updated at 06:16 AM ---------- Previous update was at 06:09 AM ----------
CarloM,
One more help required ..if there is no file found for today then I would like to display
"No files for today" ...how to check that condition...
CarloM
December 2, 2011, 6:18am
15
It's basically the same as my original suggestion, it's just generating the 'Dec 2' part of the pattern automatically - date | cut -c5-10
gives characters 5-10 from the date command's response. .*:
is a regular expression for 'any number of any character, followed by a colon'.
EDIT:
ls -ltrh | egrep "`date | cut -c5-10`.*:" || echo "No files"
Thanks balajesuri; this is also working ...can u please explain this...
---------- Post updated at 06:54 AM ---------- Previous update was at 06:22 AM ----------
Thanks CarloM
if there is no file found for today then I would like to display
"No files for today" ...how to check that condition...?
carlom:
It's basically the same as my original suggestion, it's just generating the 'Dec 2' part of the pattern automatically - date | cut -c5-10
gives characters 5-10 from the date command's response. .*:
is a regular expression for 'any number of any character, followed by a colon'.
EDIT:
ls -ltrh | egrep "`date | cut -c5-10`.*:" || echo "No files"
As per man find (linux) :
find . -maxdepth 1 -daystart -mtime 0 -print
This will find all files modified "today"
Perl's function localtime gives the following output:
Fri Dec 2 17:31:17 2011
Substr from 4 to a length of 6 would cut out "Dec 2". And hold this in variable $x.
"/$x\s+[0-9]{2}:[0-9]{2}/" -> Regex which will search strictly for "Dec 2 XX:XX" where XX:XX is the time. If it were "Dec 2 2010" then this regex wouldn't match.
CarloM
December 2, 2011, 7:09am
19
Wrong man find (OpenSolaris) :). SunOS doesn't support -daystart or -maxdepth.
So:
won't work, you don't have "maxdepth", and I doubt you have "daystart".
Then I would:
MARK=$(mktemp)
touch -t $(date +%Y%m%d0000) ${MARK}
find . -type f -newer ${MARK} -print | grep -v '^.\/[^/]*/'
rm ${MARK}