Find command with Metacharacter (*) Should match exact filename


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find command with Metacharacter (*) Should match exact filename
# 1  
Old 12-26-2016
Find command with Metacharacter (*) Should match exact filename

Hi,
Below is list of files in my directory.

Code:
-rw-rw-r--. 1 Roots Roots 0 Dec 26 06:58 12345_kms_report.csv
-rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12346_kms_report.csv
-rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 12347_kms_report.csv
-rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 123478_kms_report.csv
-rw-rw-r--. 1 Roots Roots 0 Dec 26 06:59 123478_ks_report.csv
-rw-rw-r--. 1 Roots Roots 0 Dec 26 07:00 12346_ks_report.csv


Now I would like to display only the filename start with 12347 .this number will be come from out parameter to shell script from database function call.

So I tried below command to find all the files only should match with this number.

Code:
echo $l_file_id
12347

find /home/Roots -maxdepth 1 -type f -name "$l_file_id*".csv

It should only match "
Code:
12347_kms_report.csv

" ,But it also match below files

Code:
123478_kms_report.csv
123478_ks_report.csv

.

Because I'm using * in my find command. Is there any way get only match
Code:
12347_kms_report.csv

# 2  
Old 12-26-2016
of course it will match both, as your pattern is found in both...
If you are so sure you will have _ as next char you should have included it also in that way on the requested match will appear
So the arguments of -name should be:
Code:
 -name "$l_file_id"_*.csv

# 3  
Old 12-26-2016
Either
Code:
ls "$l_file_id"_*.csv

where the * is outside the quotes so the shell expands it,
or
Code:
find /home/Roots -maxdepth 1 -type f -name "${l_file_id}_*".csv

where the shell lets find expand the *
The braces separate the _ from the variable name. Another method is
Code:
find /home/Roots -maxdepth 1 -type f -name "$l_file_id""_*".csv

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find the exact process using ps aux command?

Please do not post a technical question in the @How to contact....' forum. I have moved this for you. Hello Everyone, Please help me on this, Requirement here is to check whether the process is running using the process id. For the below scenario, I m trying to grep 1750 process id to... (3 Replies)
Discussion started by: Hari A
3 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Match exact String with sed command

I have a workaround to the problem i m posting, however if someone wants to look at my query and respond ... i will appreciate. This is in reference to this thread -> https://www.unix.com/shell-programming-and-scripting/267630-extract-between-two-exact-matched-strings.html I have data.txt as... (11 Replies)
Discussion started by: mohtashims
11 Replies

4. Shell Programming and Scripting

Find the exact and best match between 2 files

Dear Forum, File1: Reference 4474189 United Kingdom Mobile 4474188 United Kingdom Mobile 4474187 United Kingdom Mobile 447 United Kingdom 93 AFGHANISTAN 0093 1907 ALASKA 001907 355 ALBANIA 00355 35568 ALBANIA MOBILE 0035568 35569 ALBANIA MOBILE 0035569 213 ALGERIA 00213 2137 ALGERIA... (2 Replies)
Discussion started by: yahyaaa
2 Replies

5. UNIX for Advanced & Expert Users

Regex to match Exact port number (netstat command)

Hi All, We have this regex:\\*.*?(.600).*?.(LISTEN|ESTABLISHED) OS = Solaris 10 The purpose of this regex is to match the ports in output of "netstat -an" and report if any ports between 6000-6009 are getting used. The only problem is if I have something like this (sample output as... (6 Replies)
Discussion started by: sk2code
6 Replies

6. Shell Programming and Scripting

Help me with s script to find exact match

Hi, im extracting data from oracle DB. As the data is case sensitive, i have to extract the data which doesn't match exactly. im poor in unix scripting, can someone plz help me with a script. Here are the details. Need to compare the second column of the each line of the file1.csv with the data in... (5 Replies)
Discussion started by: JSKOBS
5 Replies

7. Shell Programming and Scripting

what is the find command to find exact dir from the root

I want to find a dir called STOP from the root.so what is the find command. Thanks & Regards Rajkumar (1 Reply)
Discussion started by: rajkumar_g
1 Replies

8. Shell Programming and Scripting

How to find lines that match exact input and count?

I am writing a package manager in BASH and I would like a small snippet of code that finds lines that match exact input and count them. For example, my file contains: xyz xyz-lib2.0+ xyz-lib2.0 xyz-lib1.5 and "grep -c xyz" returns 4. The current function is: # $1 is the package name.... (3 Replies)
Discussion started by: cooprocks123e
3 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. UNIX for Dummies Questions & Answers

using sed or grep to find exact match of text

Hi, Can anyone help me with the text editing I need here. I have a file that contains the following lines for example: (line numbers are for illustration only) 1 Hello world fantasy. 2 Hello worldfuntastic. 3 Hello world wonderful. I would like to get all those lines of text that... (5 Replies)
Discussion started by: risk_sly
5 Replies
Login or Register to Ask a Question