Visit Our UNIX and Linux User Community

How to get Find command work with a variable passing "*" value?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get Find command work with a variable passing "*" value?
# 1  
Old 08-15-2006
How to get Find command work with a variable passing "*" value?

Hi guy,

I have a problem to pass a variable containing '*' value to FIND command.
below is the script. It doesn't work by submit below command: name '*.txt'
or name *.txt

I've tried either optn="-name '$2'" or optn="-name $2"., and there is no luck.

### (script name = 
 #!/bin/ksh -x
 #$1 - name of option
 #$2 - pattern of filename or age of file

if [ "$1" == "name" ]; then
    optn="-name '$2'"
    optn="-mtime +$2"
find . -type f $optn -ls

Thanks in advance
# 2  
Old 08-15-2006
Updated to take care of "*", here is the code and sample use of the script:
admin@unix1:/home/admin/scripts$./find1 name "*.txt"

admin@unix1:/home/admin/scripts$cat find1
### (script name =
 #!/bin/ksh -x
 #$1 - name of option
 #$2 - pattern of filename or age of file
tmp=`echo "$2" | sed 's/\"//'`
if [ "$1" == "name" ]; then
find . -type f $optn "$tmp" -print


Last edited by tayyabq8; 08-15-2006 at 11:32 AM.. Reason: Updated to take "*" as argument
# 3  
Old 08-15-2006
pls read the Rules and dont' cross-post.

Threads merged!

Last edited by vgersh99; 08-15-2006 at 12:07 PM..
# 4  
Old 08-15-2006

Thanks Shereenmotor,

find . -type f $optn "$tmp" doesn't work for me.

find . -type f $optn "$2" works fine.

I expect that $optn can pass more combined options (ie. -name "*.txt" -mtime +300) to FIND command.
# 5  
Old 08-16-2006
Yes you are right "$2" should work also, because I was under the impression that $2 will have the value "*.txt" ie with quotes and it can create problem, therefore I stored the value of $2 in tmp variable after removing any quotes, but in fact tmp is not required here at all, you can remove that.
I expect that $optn can pass more combined options (ie. -name "*.txt" -mtime +300) to FIND command.
I don't understand exactly what you are trying to do here with passing same parameters which you can pass directly to find in the same fashion? It is almost useless to write code for the same function which is already provided in the command, we can do many things with parameters and find command but can you pls explain what is your exact requirement and why you need it?

# 6  
Old 08-16-2006
To shereenmotor:

My idea is to create an utility in a shell script for the user removing their old files. The utility leads the user to select an option from the screen manu to remove the file based on the age of file, pattern of file name or both. So the use is able to enter the paramater values. In the script, a string of option for FIND command will be assembled and be passed to the command line. Thanks again.

Previous Thread | Next Thread
Test Your Knowledge in Computers #438
Difficulty: Medium
TypeScript is a free and open-source subset of JavaScript developed by Microsoft.
True or False?

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

2. Shell Programming and Scripting

Problem with "find" and "grep" command

I want to list all files/lines which except those which contain the pattern ' /proc/' OR ' /sys/' (mind the leading blank). In a first approach I coded: find / -exec ls -ld {} | grep -v ' /proc/| /sys/' \; > /tmp/list.txt But this doesn't work. I got an error (under Ubuntu): grep:... (5 Replies)
Discussion started by: pstein
5 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. OS X (Apple)

Neither "which" nor "find" commands work

hi there, could use some basic PATH advice, i think, or something find sometimes work, but which hasn't ever seemed to. for years! what am i doing wrong that the commands which and find rarely work? they used to work on the workstations i used ages ago... running 10.5.8 because i... (4 Replies)
Discussion started by: zensnob
4 Replies

5. Shell Programming and Scripting

store the output of "find" command in a variable?

I intend to find the path/full location of a file(filename given by user thru "read filenme") using "find" or any other command and then store it's output in a variable for some other processing. But struggling to put all things together (i.e finding the fully qualified location of that file and... (4 Replies)
Discussion started by: punitpa
4 Replies

6. Shell Programming and Scripting

passing the output of cmd from "eval" to a variable

Hello, I need help with the eval command. I have been building a lengthy cmd using eval, and I need to create $var from the output of the cmd. Here is what I have. Out=/dfezz1/output.txt Node="'LPAR Info:'" Gr3p0=" |grep" Printc=" prtconf" Output1=" 1>>$Out 0>&1" Cat1="cat... (8 Replies)
Discussion started by: dfezz1
8 Replies

7. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies

8. Shell Programming and Scripting

passing a list of dynamic names to a "PS" command in shell script?

Hi, I am new to shell script. This is my first post .I have written a small script which returns list of names starts with "ram" in /etc/passwd .Here is that:- #!/bin/ksh NAME_LIST="name_list.txt" cat /dev/null > $NAME_LIST evalcmd="cat /etc/passwd | grep "^ram?*" | cut -d: -f1" eval... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

Featured Tech Videos