LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get files list (http://www.linuxquestions.org/questions/linux-newbie-8/get-files-list-4175433010/)

shivaa 10-19-2012 01:25 AM

Get files list
 
Hello everyone, on my Solaris machine, I have some log files, that are creaeted on daily basis, and get saved in some specific format, like this:
Code:

$ cd /home/jack/logs/
$ ls -la
logs.20121017-12345
logs.20121017-12345
logs.20121018-12345
logs.20121018-12345
logs.20121018-12345
logs.20121019-12345
logs.20121019-12345 and so on...

I have a daily task to analyze files create on that day i.e. files that are matching that day's format only. Suppose I am analysing them today then my script should consider only file having format 20121019. (Note: I won't prefer listing the files on basis of their modification time as it may list files of previous day).
I want to create a script, which takes such files of today's format i.e. 20121019 and analyse them to get some date. How to achive this? Any help?

druuna 10-19-2012 01:32 AM

The following will get today's date:
Code:

date '+%Y%m%d'
You can fill a variable with the output and use it:
Code:

TDAY="$(date '+%Y%m%d')"
ls -l *.${TDAY}*

Have a look at the date man page, if I remember correctly the options are slightly different on a Solaris box compared to Linux.

shivaa 10-19-2012 01:43 AM

Great. I was thinking of writing a long and (ofcourse boring :-)) code for that, but pb solved with your suggestion!! New code is:
Code:

#!/bin/bash
year=`date +%Y`
month=`date +%m`
date=`date +%d`
format="$year$month$date"

ls -la log.$format*


shivaa 10-19-2012 01:45 AM

Thanks

David the H. 10-20-2012 12:31 PM

Code:

#!/bin/bash
year=`date +%Y`
month=`date +%m`
date=`date +%d`
format="$year$month$date"

Is there any real reason for you to run three separate date commands to do this? It's much less efficient than running a single instance.

If you really need the three values to be separate for other purposes in the script, you can split them in one run:

Code:

read year month date < <( date '+%Y %m %d' )
format="$year$month$date"

I used bash's process substitution to pass the stdin to read, but other techniques can be used instead if you need it to be more portable, such as a here document.

Code:

read year month date <<EOF
$( date '+%Y %m %d' )
EOF
format="$year$month$date"


shivaa 10-22-2012 08:54 AM

Quote:

Is there any real reason for you to run three separate date commands to do this? It's much less efficient than running a single instance.
David, I need to use difference formats in that script, and for that I have declared these variables seperately.
Thanks everyone for your efforts!


All times are GMT -5. The time now is 05:38 AM.