LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 10-19-2012, 01:25 AM   #1
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Rep: Reputation: 282Reputation: 282Reputation: 282
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?
 
Old 10-19-2012, 01:32 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
1 members found this post helpful.
Old 10-19-2012, 01:43 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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*
 
Old 10-19-2012, 01:45 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks

Last edited by shivaa; 10-19-2012 at 01:47 AM.
 
Old 10-20-2012, 12:31 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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"
 
Old 10-22-2012, 08:54 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,778
Blog Entries: 4

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
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!
 
  


Reply

Tags
script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] find - filter files matching a list of files hashbang#! Programming 6 11-25-2010 05:01 PM
Need advice on a script to search many files for list of terms, append hits to list jimmy the saint Programming 1 07-11-2010 03:59 AM
Kate: Append files to existing list and enable the Recent List AGazzaz Linux - Software 2 10-13-2009 08:27 PM
Downloading a list of files from a remote server using a list ralcocer Programming 2 02-11-2009 01:02 PM
How to list all files recursively, in a non-broken list? nyle Linux - Newbie 1 12-16-2008 10:52 PM


All times are GMT -5. The time now is 08:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration