To find files having filename containing specific date format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To find files having filename containing specific date format
# 1  
Old 04-17-2016
To find files having filename containing specific date format

Hi,

I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing.

Could you please have any idea on this.

Code:
trades_201604040000.out
trades_201604040300.out
trades_201604040600.out

Thanks.

Last edited by Scrutinizer; 04-17-2016 at 09:36 AM.. Reason: code tags
# 2  
Old 04-17-2016
On requests like this you're much more likely to get a response with a shell that is based on the syntax used by the Bourne shell and expanded upon in the POSIX standards.

Why do you have to use tcsh for this?

What have you tried on your own?
# 3  
Old 04-18-2016
Cumbersome in tcsh, you need an external command (sed)
Code:
#!/bin/tcsh
set head="trades_"
set tail=".out"
set nonomatch
foreach file ( ${head}*${tail} )
  if ( -f "$file" ) then 
    set datepart=`echo "$file" | sed 's/^'"$head"'//; s/'"$tail"'$//'`
    echo "$datepart"
  endif
end

Better in bash/ksh/psh:
Code:
#!/bin/bash
head="trades_"
tail=".out"
for file in ${head}*${tail}
do
  if [ -f "$file" ]
  then 
    datepart=${file#$head}
    datepart=${datepart%$tail}
    echo "$datepart"
  fi
done

# 4  
Old 04-20-2016
Thanks for the reply.

Sorry for I am not being clear.

Please note that there may be files like below. I need to only find files having this pattern trades_yyyymmddhh24mm.

Code:
trades_201604040000.out
trades_201604040300.out
trades_201604040600.out
trades_20160406.out
trades_20160407.out

Moderator's Comments:
Mod Comment Please use CODE tags when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 11-29-2016 at 03:38 AM.. Reason: Add CODE and ICODE tags.
# 5  
Old 04-20-2016
Or
Code:
for FN in tr*; do echo ${FN//[^0-9]}; done
201604040000
201604040300
201604040600

# 6  
Old 04-20-2016
Usefor FN in *_????????????.*
# 7  
Old 04-20-2016
Quote:
Originally Posted by MadeInGermany
Cumbersome in tcsh, you need an external command (sed)
Code:
#!/bin/tcsh
set head="trades_"
set tail=".out"
set nonomatch
foreach file ( ${head}*${tail} )
  if ( -f "$file" ) then 
    set datepart=`echo "$file" | sed 's/^'"$head"'//; s/'"$tail"'$//'`
    echo "$datepart"
  endif
end

Better in bash/ksh/psh:
Code:
#!/bin/bash
head="trades_"
tail=".out"
for file in ${head}*${tail}
do
  if [ -f "$file" ]
  then 
    datepart=${file#$head}
    datepart=${datepart%$tail}
    echo "$datepart"
  fi
done

Replace * with ???????????? or more precisely with [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

3. Shell Programming and Scripting

Find files for a specific date

Hi, I am looking to find files of a specific date. I am on Sun Solaris so newermt doesnot work.. I thought of using mtime but not getting how to use it. Please help me with this.. Regards Abhinav (3 Replies)
Discussion started by: abhi1988sri
3 Replies

4. Shell Programming and Scripting

Find files with specific date

Dear all, kindly i have some files with different dates i need to grep word from these files but i need to search in files with date 2012-12-02 not all files in this directory do u have any command (4 Replies)
Discussion started by: maxim42
4 Replies

5. Shell Programming and Scripting

Find files greater than a particular date in filename.

I need a unix command which will find all the files greater that a particular date in the file name. say for example I have files like(filenaming cov : filename.YYDDMMSSSS.txt) abc.201206015423.txt abc.201207013456.txt abc.201202011234.txt abc.201201024321.txt efg.201202011234.txt... (11 Replies)
Discussion started by: lijjumathew
11 Replies

6. Shell Programming and Scripting

find command to filter specific type of files older than certain date.

Hi I need to find the list of files in a directory and to do some specific operations based on the type of files. suppose in a directory am having .dat , .log, .err, .rej file types. i need to filter out .dat and .log only which are older than six months. i used the below query but the... (2 Replies)
Discussion started by: msathees
2 Replies

7. Shell Programming and Scripting

Increment Date in a Specific Format

Hi, This may sound a Basic, but please help I Hava a date in the Given Format yyyy_mm_dd Start_date=2008_07_09 (This is a Fixed Date) I need to Increment this Date in a For loop until it equals Current Date For(Start_date<Current_date) xxxxxxxxxxxxxxxxxxxxxxxxxxxx... (0 Replies)
Discussion started by: dsshishya
0 Replies

8. Shell Programming and Scripting

create filename with 'DD/MM/YYYY' date format

Hi, I can use the following command to create a file with some name then underscore and then date appended to it in the format 'DD-MM-YYYY': touch "newfile_`date '+%d-%m-%Y'`" But it gives me error when I try with the similar command to create a file with the date format 'DD/MM/YYYY'. I... (4 Replies)
Discussion started by: royalibrahim
4 Replies

9. AIX

find files with specific date and time

Hi, I wanna to find files with specific date and time. I know this command: ls -ltr /system1/*.505 | grep 'Jan 18 09:00'. I want the 'Jan 18 09:00' not hard coded, I want it coming from the system date and time. And I want the time varies from 09:00-09:05. Is this possible without heavy... (3 Replies)
Discussion started by: itik
3 Replies

10. UNIX for Advanced & Expert Users

date issue-find prevoius date in a patricular format

Hi , I have written a shell script that takes the current date on the server and stores it in a file. echo get /usr/home/data-`date '+%Y%d'`.xml> /usr/local/sandeep/GetFILE.ini I call this GetFILE.ini file from an sftp program to fetch a file from /usr/home/ as location. The file is in... (3 Replies)
Discussion started by: bsandeep_80
3 Replies
Login or Register to Ask a Question