# Shell script to loop through files

**URL:** <https://community.unix.com/t/shell-script-to-loop-through-files/364340>\
**Category:** UNIX for Beginners Q & A\
**Created:** [January 3, 2017, 2:23am UTC](https://community.unix.com/t/shell-script-to-loop-through-files/364340 "2017-01-03T02:23:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajendra\_Kalepu](https://community.unix.com/letter_avatar/rajendra_kalepu/32/6_449c533f586939b6e5f246632a46421f.png) [@Rajendra\_Kalepu](https://community.unix.com/u/Rajendra_Kalepu)\
**Post date:** [January 3, 2017, 2:23am UTC](https://community.unix.com/t/shell-script-to-loop-through-files/364340/1 "2017-01-03T02:23:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [January 3, 2017, 3:09am UTC](https://community.unix.com/t/shell-script-to-loop-through-files/364340/2 "2017-01-03T03:09:52Z")

</div>

> [@rajendra kalepu](#):
>
> 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](http://www.unix.com/homework-coursework-questions/) under [special homework rules](http://www.unix.com/homework-coursework-questions/113751-draft-rules-homework-coursework-questions-forum.html).

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](http://www.unix.com/homework-coursework-questions/113751-draft-rules-homework-coursework-questions-forum.html) 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` ?

---

<div class="post-metadata">

**Author:** ![Rajendra\_Kalepu](https://community.unix.com/letter_avatar/rajendra_kalepu/32/6_449c533f586939b6e5f246632a46421f.png) [@Rajendra\_Kalepu](https://community.unix.com/u/Rajendra_Kalepu)\
**Post date:** [January 3, 2017, 3:52am UTC](https://community.unix.com/t/shell-script-to-loop-through-files/364340/3 "2017-01-03T03:52:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [January 3, 2017, 5:10am UTC](https://community.unix.com/t/shell-script-to-loop-through-files/364340/4 "2017-01-03T05:10:44Z")

</div>

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?
