Help in searching a particular string in a file name (not inside the file contents)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help in searching a particular string in a file name (not inside the file contents)
# 1  
Old 08-05-2011
Help in searching a particular string in a file name (not inside the file contents)

Dear Unix Gurus,
I am new to shell scripting and in the process of learing.
I am trying to find whether a file name has today's date in MMDDYYYY format.
I am using the following code and it doesn't seem like working.
Code:
#!/usr/bin/ksh
today=$(date '+%m%d%Y')
echo today: $today
file=`find /opt/feeds -type f -name /opt/feeds/*.* -exec grep $today {} \;`
echo file: $file
if [ "$file" = "" ] ; then 
echo "File has today's date"
else
echo "File has different date"
fi

Please help me.

Last edited by jim mcnamara; 08-05-2011 at 05:18 PM.. Reason: code tags
# 2  
Old 08-05-2011
Code:
today=$(date +%m%m%Y)
ls /opt/feeds/*${today}*

will list all of the file with names like the date - just in the /opt/feed direcotry

For all directories from /opt/feeds on down:
Code:
today=$(date +%m%m%Y)
find /opt/feeds -name  "\*${today}\*"

# 3  
Old 08-05-2011
I am still not able to make the script work. I am not able to get the correct echo message.
I wanted to check only inside the /opt/feeds directory.

Can you please modify the script?

Thanks,
Shankar
# 4  
Old 08-05-2011
Code:
#!/usr/bin/ksh
today=$(date '+%m%d%Y')
echo today: $today
file=`ls /opt/feeds | grep "$today"`
if [ -z "$file" ] ; then
   echo 'file not found'
else
   echo "file found = $file"
fi

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 08-05-2011
Thank you Jim. It works like a charm!!!!

Last edited by shankar1dada; 08-05-2011 at 06:28 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a file inside a .tar.gz file by date

Hi, I would like to ask if there is a way to search for a file inside a .tar.gz file without extracting it? If there is, is there a way to search for that file by date? Thanks! (4 Replies)
Discussion started by: erin00
4 Replies

2. Shell Programming and Scripting

Problem with the awk in searching the contents in a file

Hello all, I am a newbie in awk. I am struggling in this problem for a long.Actually I have two files, filea and fileb. File a is actually a search key through it I have to find the corresponding japanese tag from file b. filea contains the data like this: sm982882 sm1893548 sm2420025... (3 Replies)
Discussion started by: csim_mohan
3 Replies

3. Shell Programming and Scripting

unzip a file then further check contents inside it

Dear all I need to unzip a file then further it has many folders to perform tak inside each folder. What Task I need to perform I'm able to do it but once I unzip a folder then I'm not able to do cd folder/ into it. I have written following code for it for i in $( ls | grep Run_20111016 ) do... (2 Replies)
Discussion started by: Bhalinder
2 Replies

4. Shell Programming and Scripting

Searching inside a .gz file without gunzip

I have a set of folders inside which there may be n number of files. All those files are in .gz extension. Now I need to search all the files without gunzip them. Also I need to read the content of a file if the search pattern is found without gunzip them. (3 Replies)
Discussion started by: realspirituals
3 Replies

5. Shell Programming and Scripting

searching string from one file and check it in the other file

Hello, Could you please help me in the script I have two files: FileOne has the below data: 000004 500000 111234 123456 ################## FileTwo has the below data: 434556 New York 545333 Paris 434344 Paris 111234 Berlin 434322 ... (4 Replies)
Discussion started by: RzY
4 Replies

6. UNIX for Dummies Questions & Answers

Help with searching for a file in a directory and copying the contents of that file in a new file

Hi guys, I am a newbie here :wall: I need a script that can search for a file in a directory and copy the contents of that file in a new file. Please help me. :confused: Thanks in advance~ (6 Replies)
Discussion started by: zel2zel
6 Replies

7. Shell Programming and Scripting

How to grep the contents inside a tar file

Hi All I have searched the possibility of this options everywhere but am unable to find it in any forum. I have a tar file inside which there are n number of files and i dont know them. I need to grep a word inside the tar file and need to know in which file the word resides. > cat a... (2 Replies)
Discussion started by: Whiteboard
2 Replies

8. Shell Programming and Scripting

searching and editing file contents

Can you please help me to edit parts of a file and write into a new file. ===================================== Suppose I have a huge data dump in a file I need to search for a tag in that and cut few lines around that tag in the file. Is there a way to keep track of line numbers and operate on... (18 Replies)
Discussion started by: jayana
18 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. UNIX for Advanced & Expert Users

Searching contents of a file

Is there a way a command or a combination through which i can check the contents of a all files in a directory and get the return as the file names which contains the partiuclar string. (2 Replies)
Discussion started by: thepitzaboy
2 Replies
Login or Register to Ask a Question