How to find a file based on pattern & return the filename if found?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find a file based on pattern & return the filename if found?
# 1  
Old 04-03-2014
How to find a file based on pattern & return the filename if found?

Hi all,

I am a newbie here. I have this requirement to find a file based on a pattern then return the filename if found.

I created a script based on online tutorials. Though, I am stuck & really appreciate if anyone can have a quick look & point me to the right direction?

Code:
#Script starts
#!/bin/sh

pattern=$1
echo "$pattern"

filepath=$2
echo "$filepath"

cd "$filepath"

for FILENAME in "$pattern"
do
  if [ -f "$FILENAME" ]; then
    echo "$FILENAME"
    break
  fi
done

exit $FILENAME

Code:
--Call a script with parameters:
./script.sh "*.txt" "/appl/tmp/"

When I execute it I got 2 errors, directory not found & syntax error near unexpected token `do.

The first error is really weird as when I manually typed & executed “cd /appl/tmp/” it worked fine. Could user permissions play any role in this? I already set the files & directories to '777'.

Next would be the syntax error on token `do. I tried this piece of code (for loop) in an online compiler but that came back fine. Could you spot what is messed up?

Any advice is deeply appreciated! Thanks in advance.

YE
# 2  
Old 04-03-2014
Hello,

You can try with following. Just an easy example.

Code:
cat check_file.ksh
a=`find . -name "*.txt"`
if [[ -n $a ]]
then
    echo "Files have been found."
else
    echo "Files not found."
fi


Thanks,
R. Singh
# 3  
Old 04-03-2014
Thanks R. Singh.

I would need to return the first found filename back to the script caller. How do I achieve that?
# 4  
Old 04-03-2014
The script looks fine except at couple of places
1. in the for loop, $pattern should not be in double quotes
2. You cannot exit with $FILENAME. You have to return a number

If you want to pass File name from the script echo only filename and read it outside the script.
Code:
for FILENAME in $pattern

Code:
exit 0

Code:
#!/bin/sh
pattern=$1
filepath=$2
cd "$filepath"
for FILENAME in $pattern
do
  if [ -f "$FILENAME" ]; then
    echo "$FILENAME"
    break
  fi
done
exit 0

Code:
./script.sh "*.txt" "/appl/tmp/" | read fname
echo $fname

# 5  
Old 04-03-2014
Hello,

Following may help.

Code:
a=`find . -name "*.txt1"`
if [[ -n $a ]]
then
        echo $a "is the file name."
else
        echo "files found."
fi

Output will be as follows.

Code:
./check_check_check_test.txt1 is the file name.

Note: this is just an example.

Thanks,
R. Singh
# 6  
Old 04-03-2014
Thanks for the replies.

Again, I would need to return the first found filename back to the script caller.

For instance a program where I would assign the return value in a variable

E.g.
lv_filename = exec (findfile.sh); --[not unix]

any tips?
# 7  
Old 04-03-2014
Code:
lv_filename=$(./script.sh "*.txt" "/appl/tmp/")

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

2. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

3. Shell Programming and Scripting

Help with ksh-to read ip file & append lines to another file based on pattern match

Hi, I need help with this- input.txt : L B white X Y white A B brown M Y black Read this input file and if 3rd column is "white", then add specific lines to another file insert.txt. If 3rd column is brown, add different set of lines to insert.txt, and so on. For example, the given... (6 Replies)
Discussion started by: prashob123
6 Replies

4. UNIX for Dummies Questions & Answers

Find next line based on pattern, if it is similar pattern skip it

Hi, I am able to get next line if it is matching a particular pattern. But i need a way to skip if next line also matches same pattern.. For example: No Records No Records Records found got it Records found Now i want to find 'Records found' after 'No Records' pattern matches.. ... (5 Replies)
Discussion started by: nagpa531
5 Replies

5. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

6. Shell Programming and Scripting

Delete files based on specific MMDDYYYY pattern in filename

Hi Unix gurus, I am trying to remove the filenames based on MMDDYYYY in the physical name as such so that the directory always has the recent 3 files based on MMDDYYYY. "HHMM" is just dummy in this case. You wont have two files with different HHMM on the same day. For example in a... (4 Replies)
Discussion started by: shankar1dada
4 Replies

7. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

8. Shell Programming and Scripting

To find the line no, where the particular pattern is not found

Hi, suppose i have a txt file containing thye following data 2012156|sb3|nwknjps|BAYONNE|NJ|tcg 201221|094|mtnnjprc:HACKENSACK|NJ|tcg 201222|wn3|mtnnjtc|HACKENSACK|NJ|tcg 2018164|ik4|mtnntc|JERSEY CITY|NJ|tcg 20123482|ik4|mtnnjpritc,JERSEY CITY|NJ|tcg... (3 Replies)
Discussion started by: priyanka3006
3 Replies

9. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question