Shell script to loop through files

Hi Team,

I am new to shell scripting. I have the below requirement

1) Say if i am searching for 20160815 in a directory /dir

2) Now i need to get the files present in dir whose time stamp in greater than or equal to 20160815

3) Then i need to find the string 20160815 from the set of files received in point 2

4) Now i need to get the final file name as output

I visited many forums to understand how to loop through files. Please help me in this regard

Thanks and Regards
Raj

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework and Coursework forum under special homework rules.

If this is not homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If this is not homework, please explain what "searching for 20160815 in a directory" means. Are you looking for a file named 20160815 ? Are you looking for a file with a name containing the string 20160815 ? Are you looking for a file containing the string 20160815 ? Are you looking for a file in the directory /dir or are you looking for a file in a directory somewhere in the file hierarchy rooted in /dir ?

Standards conforming filesystems have at least three timestamps (last file status change time, last access time, and last file modification time). For which time stamp are you looking?

How do you "find the string 20160815 from the set of files received in point 2"? Are you looking for a file named 20160815 ? Are you looking for a file with a name containing the string 20160815 ? Are you looking a file containing the string 20160815 ?

1 Like

Hi Don

I am working for a retail firm. During test phase i need to get the file which is having a particular string

1) Say if i am searching for 20160815 in a directory /dir
Here i am referring to string '20160815' which is actually a date in YYYYMMDD format

2) Now i need to get the files present in dir whose time stamp in greater than or equal to '20160815'
I mean to say , i want to search the for the files which are generated on 20160815 (ex : file1.txt),20160816 (file2.txt),20160817 (file3.txt) and so on
Here i am looking for a created date > = 20160815

3) Then i need to find the string '20160815' from the set of files received in point 2
file1.txt, file2.txt,file3.txt

4) Now i need to get the final file name as output
Say the string is present in file3.txt. I need to output it

I visited many forums to understand how to loop through files. Please help me in this regard

Thanks and Regards
Raj

I'm afraid your answers to Don Cragun's question didn't help too much. Paraphrasing questions or statements sometimes helps to achieve a common understanding, but it didn't add any relevant and important information, and in this case posting more structures and details had put us in a position to deal with your quite vague explanations and circumscriptions.

1) Yes, understood, but please look the last paragraph in post#2.
2) Just guessing now: you are looking at files' contents, as the file name "file1.txt" doesn't contain the search string, and assuming that string represents the "creation date". Is that the ONLY string in the file that has this structure (8 digits possibly in YYYYMMDD format)? Where resp. when to stop matching (Today? 1.January? 2017? 2020?)?
3) What is different here from item 1) ?
4) "The string" actually is sort of a range of dates (mayhap with an open end). So why not use the result of 3) [or even 1) ] ?

Please rephrase your request more precisely, and supply input data (directory structure to be searched, and a representative file sample).
Or, simply, wouldn't just a grep with a smartly fitted regex do what you need?

1 Like