Help in script to check file name with todays date


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Help in script to check file name with todays date
# 1  
Old 07-11-2013
Help in script to check file name with todays date

I am trying to include a snippet in my script to check if the file created is having today's date.

eg: File name is : ABC.YYYYMMDD-nnn.log

The script should check if 'YYYYMMDD' in the above file name matches with today's date.
Can you please help me in achieving this.
Thanks in advance!!
# 2  
Old 07-11-2013
Code:
ls "ABC.$( date +%Y%m%d )-nnn.log"

# 3  
Old 07-12-2013
Thanks for the reply, but I need to check if the 'YYYYMMDD' field in the filename equals to todays date.
I can take today's date into a variable and do a comparison, but how to compare it to only a part of the file name ?Smilie
Ex:File name: 'ABC.YYYYMMDD-nnn.log'
# 4  
Old 07-12-2013
Use parameter substitution to extract specific part of file name and then compare.

Assuming file name is stored in variable: file
Code:
f_dt="${file#*.}"
f_dt="${f_dt%-*}"

s_dt="$( date +%Y%m%d )"

if [ "$f_dt" = "$s_dt" ]
then
        # Action if true
else
        # Action if false
fi

# 5  
Old 10-24-2013
Hi Yoda, am learning shell script , may u please let me know how this command is working what does mean of # with variable and % in variable.

"${file#*.}"
# 6  
Old 10-24-2013
# - Delete the shortest part of a pattern (dot) that matches in the beginning of variable $file.

Code:
file=ABC.YYYYMMDD-nnn.log
$ echo ${file#*.}
YYYYMMDD-nnn.log

% - Delete the shortest part of a pattern (-) that matches at the end part of variable $file.
Code:
file=ABC.YYYYMMDD-nnn.log
$ echo ${file%-*}
ABC.YYYYMMDD

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to check current date file is created and with >0 kb or not for multiple directories

Hi All, I am new in scripting and working in a project where we have RSyslog servers over CentOS v7 and more than 200 network devices are sending logs to each RSyslog servers. For each network devices individual folders create on the name of the each network devices IP addresses.The main... (7 Replies)
Discussion started by: Pinaki
7 Replies

2. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

3. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

4. Shell Programming and Scripting

Script to check file with previous date

Hi all,I need your help to create the script.I need a script to check the ZIP file at network location with yesterday date name. ZIP file creation date is current date, but name of the zip file is previous date. for example file name "20110216.zip" created today 201102017.I just want to return the... (1 Reply)
Discussion started by: deepaksingla
1 Replies

5. Shell Programming and Scripting

Perl Script to execute todays date.

Hi Folks, I have created a script last month to retrive files thru FTP and cronjob was running fine till yesterday. But the naming convention of the daily file is Filename_<date>.xml where date is YYYYMMDD. But today i have received file name as Filename_20110232.xml :( Part of my Perl... (4 Replies)
Discussion started by: Sendhil.Kumaran
4 Replies

6. Shell Programming and Scripting

Perl Script to check file date and size

Hi guys, i am new to perl. I started reading the perl documents and try to come up with some logic. I am trying to create a script that would go into a location, search for todays files, then searches for all .txt files from today. If todays not found, its an error If file size is less... (26 Replies)
Discussion started by: DallasT
26 Replies

7. Shell Programming and Scripting

Shell script help to eliminate files of todays date

Hi I am very new to shell scripting and have written a script (below). However the directory I am searching will contain a file with a .trn extension each day which I want to eliminate. Each day the file extension overnight will change to trx, if this fails I want to know. Basically what I... (2 Replies)
Discussion started by: richM
2 Replies

8. Linux

Comparing the file drop date with todays date

Daily one file will dropped into this directory. Directory: /opt/app/jt/drop File name: XXXX_<timestamp>.dat.gz I need to write a script which checks whether the file is dropped daily or not. Any idea in the script how can we compare timestamp of the file to today's date?? (3 Replies)
Discussion started by: Rajneel
3 Replies

9. UNIX for Dummies Questions & Answers

Get yesterdays date given todays date

Hi Guys. I am very new to UNIX. I need to get yesterdays and tommorows date given todays date. Which command and syntax do i use in basic UNIX shell. Thanks. (2 Replies)
Discussion started by: magikminox
2 Replies

10. UNIX for Dummies Questions & Answers

file date check script

I am creating a KSH script and need to check the filedate against the system date. I can get the sys date w. date command, and I was able to get the filedate w. the awk command but when I compare them w. an if condition statement I get syntax error. Not sure what's wrong, and other suggestions on... (4 Replies)
Discussion started by: jaxconsultant
4 Replies
Login or Register to Ask a Question