Help with find in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with find in a script
# 1  
Old 03-15-2010
Help with find in a script

Hi,

I am trying to find files in a directory. I am passing a part of the file name as parameter 3 in my script. but my find command doesn't see
to take it correctly

Code:
find $IRC_PATH -type f -name `${3}`*.gz -print

Here $3 is part of file name I am passing as parameter. My files will look like this

Code:
MINUTES.REF_STG.20100303.txt.tar.gz


I am passing $3 as MINUTES.REF_STG. But my find command above doesn't see to get the file names correctly. I mean its not printing
any files even though they exist in that directory. I am in the correct directory as well.

how should i change the above find command for it to pick files with the names like MINUTES.REF_STG.20100303.txt.tar.gz

Last edited by pludi; 03-15-2010 at 02:34 PM..
# 2  
Old 03-15-2010
Replace the backticks (`) with double quotes ("), and extend them to include the "*.gz" part, and you should be good to go.
# 3  
Old 03-15-2010
Thanks Pludi. It's working now.
# 4  
Old 03-15-2010
Code:
find . -type f -name "${3}.*gz" -print

Code:
leo@lein:~/Escritorio/shit$ ls -1
MINUTES.REF_STG.12156.txt.tar.gz
MINUTES.REF_STG.15553.txt.tar.gz
MINUTES.REF_STG.16522.txt.tar.gz
MINUTES.REF_STG.17451.txt.tar.gz
MINUTES.REF_STG.19918.txt.tar.gz
MINUTES.REF_STG.20100303.txt.tar.gz
MINUTES.REF_STG.24481.txt.tar.gz
MINUTES.REF_STG.32272.txt.tar.gz
MINUTES.REF_STG.9846.txt.tar.gz
MINUTES.REF_STG.9900.txt.tar.gz
showlines.sh
leo@lein:~/Escritorio/shit$ cat showlines.sh 
#!/usr/bin/bash
find . -type f -name "${3}.*gz" -print
leo@lein:~/Escritorio/shit$ bash showlines.sh blabla blabla MINUTES.REF_STG
./MINUTES.REF_STG.20100303.txt.tar.gz
./MINUTES.REF_STG.16522.txt.tar.gz
./MINUTES.REF_STG.12156.txt.tar.gz
./MINUTES.REF_STG.9900.txt.tar.gz
./MINUTES.REF_STG.15553.txt.tar.gz
./MINUTES.REF_STG.32272.txt.tar.gz
./MINUTES.REF_STG.17451.txt.tar.gz
./MINUTES.REF_STG.19918.txt.tar.gz
./MINUTES.REF_STG.24481.txt.tar.gz
./MINUTES.REF_STG.9846.txt.tar.gz
leo@lein:~/Escritorio/shit$

# 5  
Old 03-15-2010
In Bash, specify positional parameters with $1 .. $9 or ${1} .. ${n} with n being more than 2 digits. You have `${3}` which tells Bash to execute parameter 3 and return that interpreted value.

Also "set -x" is your friend!

Code:
#!/bin/bash

set -x

one=$1

one=${1:-First_Default}

two=${2:-Second_Default}

thr=${3:-Third_Default}

nam="$thr*.gz"

echo "find $one -type f -name $nam -print"

#also
echo "find $one -type f -name $3*.gz -print"

#and
echo "find $one -type f -name ${3}*.gz -print"

# 6  
Old 03-15-2010
I am extending the script to delete files older than 20 days. I am passing that as parameter too.

Quote:
find $IRC_PATH -type f -name "${3}*.gz" -mtime +"{4}" -exec rm -rf {} \;
how come this is throwing out error
Quote:
find: invalid argument `+{4}' to `-mtime'
I am running korn shell

---------- Post updated at 01:53 PM ---------- Previous update was at 01:48 PM ----------

i think i got it. Missed $.

But my find is recursively going through all sub-directories. Is there a way to limit it to only the present directory.
# 7  
Old 03-15-2010
You should have:

Code:
$IRC_PATH -type f -name "${3}*.gz" -mtime +"${4}" -exec rm -rf {} \;

no?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with better find script

hi everyone i tried to write my first script on unix solaris the target is to find every file had size above 200M and save it to log file with size and path i can do this but i still don't like it #!/bin/bash find / type f -size +200M -ls | log.txt is there better way to do this !!... (3 Replies)
Discussion started by: mondo32
3 Replies

2. UNIX for Dummies Questions & Answers

Perl Script:how to find how many parameters are required to run the script

How to find how many parameters are required to run a Perl script? (1 Reply)
Discussion started by: Lakshman_Gupta
1 Replies

3. Shell Programming and Scripting

How to find a script?

I need to find the script named jip.sh how can i search it ? (13 Replies)
Discussion started by: rafa_fed2
13 Replies

4. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

Need help to find script

Hello, I have a log file which is called datafile.info. This file contains data from a script but I can't find the script. I have tried to grep the file name but I am only getting the output files back. I have also tried find but no luck again. Is there a way to find the script... (2 Replies)
Discussion started by: INHF
2 Replies

6. Shell Programming and Scripting

Find script

Hello, as I'm doing my first steps in Linux I'm trying to create bash file that will search file\s in folders by using the Find command. can u help me with the script? it should be : myfind.sh thanx! (2 Replies)
Discussion started by: SimonBASH
2 Replies

7. Shell Programming and Scripting

Need help in a find script

i have a below script that will give me the filename along with the their size if the file size is more than 1 GB for j in `find /ednadtu3/u01/pipe -type f -size +1048576` do du -g $j done -------------------------------------------------------------------------- the above script is... (3 Replies)
Discussion started by: ali560045
3 Replies

8. Shell Programming and Scripting

Getting a script to find another script via lookup service

Hi, I'm trying to write a basic script which will measure throughput at a node on a network, and pass the results on to a manager script (running on another node on the same network). I presume that I need to use some sort of naming service, so that the manager can publish its location. From what I... (2 Replies)
Discussion started by: zeppelin147
2 Replies

9. Shell Programming and Scripting

a find script

I wrote this find script for all those questions about 'files created in the last three hours' and 'files created more than 2 minutes ago'. This script *will* recurse into subdirectories. As always, please suggest/make changes as you see fit. #!/usr/bin/ksh #set -x # unhash... (3 Replies)
Discussion started by: blowtorch
3 Replies

10. Shell Programming and Scripting

how to find Script file location inside script

I have to find out the file system location of the script file inside script. for example a script "abc.sh" placed anywhere in the file system when executed shold tell by itself the location of it. example #pwd / #./abc this is / #cd /root #./abc this is /root #cd / #/root/abc this... (10 Replies)
Discussion started by: asami
10 Replies
Login or Register to Ask a Question