find usage with parameters


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find usage with parameters
# 1  
Old 07-30-2009
find usage with parameters

i have this basic piece of code to look for an aging file based on an input parameter.

Code:
pfile=$1
pdir=`pwd`
echo current directory $pdir

for i in `find . "$pfile" -mtime +540`
do
echo aging files $i
done

# put a white space
echo

the above code works and gives me the listing of files. i need the days as parameter too and i have included on the code another variable pday=$2.

Code:
pfile=$1
pday=$2
pdir=`pwd`
echo current directory $pdir

for i in `find . "$pfile" -mtime +$pday`
do
echo aging files $i
done

# put a white space
echo

when i run the code it failed

Code:
$ sh rm_aging_file.sh lpa_load_oscm161*.log 360
current directory /export/home/lpaftdv/data/log
find: 0652-086 Specify a decimal integer for -mtime
Usage: find [-H | -L] Path-list [Expression-list]

how do i include the day parameter to make it work?

thanks,
warren
# 2  
Old 07-30-2009
Hi.

Your original script does not work, actually (not without specifing the -name option with the filename).

Code:
for i in `find $pdir -name "$pfile" -mtime +$pday`

But...

If you pass in a wildcard like "lpa_load_oscm161*.log", then the shell will expand it.

Therefore what you think is $2 (pday) is actually the second filename in the expanded list "lpa_load_oscm161*.log".

You should quote the first argument:

i.e.
Code:
sh rm_aging_file.sh "lpa_load_oscm161*.log" 360


Last edited by Scott; 07-30-2009 at 12:12 PM..
# 3  
Old 07-30-2009
i change the code to this:

Code:
pfile=$1
pday=$2
pdir=`pwd`
echo current directory $pdir

for i in `find -name "$pfile" -mtime +$pday`
do
  echo aging files $i
done

# put a white space
echo

the script failed when i run

Code:
$ sh rm_aging_file.sh "lpa_load_oscm161*.log" 360
current directory /export/home/lpaftdv/data/log
Usage: find [-H | -L] Path-list [Expression-list]

thanks.
# 4  
Old 07-31-2009
You removed the directory...

Code:
find -name "$pfile" -mtime +$pday

Should be
Code:
find . -name "$pfile" -mtime +$pday
or
find $pdir -name "$pfile" -mtime +$pday

# 5  
Old 07-31-2009
i thought that when removing the directory will work since it does not require any parameter for the directory. putting the directory inside the find command it works.

Code:
pfile=$1
pday=$2
pdir=`pwd`
echo current directory $pdir

for i in `find $pdir -name "$pfile" -mtime +$pday`
do
  vfile=`basename $i`
  echo aging files $vfile
  
done

# put a white space
echo

Code:
$ sh rm_aging_file.sh "lpa_load_oscm161*.dat" 360
current directory /export/home/lpaftdv/data/archive
aging files lpa_load_oscm161_20070814.dat
aging files lpa_load_oscm161_20071002.dat
aging files lpa_load_oscm161_20071116.dat
aging files lpa_load_oscm161.dat

$

i have to add a code that if the command returns nothing it will echo a message "no aging files found that are 360 days old".

in pseudocode:
Code:
if `find $pdir -name "$pfile" -mtime +$pday` returns null
   echo "no aging files found that are 360 days old"
else
   ...
end if

how do i check if the `find $pdir -name "$pfile" -mtime +$pday` command returns value or none at all? thanks.
# 6  
Old 07-31-2009
Hi.

Find doesn't return an error when it doesn't find anything, so one solution would be:

Code:
C=0
pfile=$1
pday=$2
pdir=`pwd`
echo current directory $pdir

for i in `find $pdir -name "$pfile" -mtime +$pday`
do
  let C=$C+1
  vfile=`basename $i`
  echo aging files $vfile

done
if [ $C -eq 0 ]; then
  echo "No files found."
  ...
fi

# put a white space
echo

Alternatively, grep does return an error, so you can do this:
Code:
pfile=$1
pday=$2
pdir=`pwd`
echo current directory $pdir

for i in `find $pdir -name "$pfile" -mtime +$pday | grep .`
do
  vfile=`basename $i`
  echo aging files $vfile

done
if [ $? -ne 0 ]; then
  echo "No files found."
  ...
fi
# this will only work if the last command in your loop doesn't return an error


# put a white space
echo

# 7  
Old 07-31-2009
thank you and it works. i used the first example you posted and i was able to get the expected results.

Code:
pfile=$1
pday=$2

vdir=`pwd`
vctr=0
echo current directory $vdir

for i in `find $vdir -name "$pfile" -mtime +$pday`
do
  let vctr=$vctr+1
  vfile=`basename $i`
  echo aging files $vfile
done

if [ $vctr -eq 0 ]; then
  echo "no aging files found that are $pday days old"
fi

# put a white space
echo

Code:
$ sh rm_aging_file.sh "lpa_load_oscm161*.dat" 360
current directory /export/home/lpaftdv/data/archive
no aging files found that are 360 days old

$ sh rm_aging_file.sh "lpa_load_oscm161*.dat" 180
current directory /export/home/lpaftdv/data/archive
aging files lpa_load_oscm161_2008085.dat
aging files lpa_load_oscm161_20080812.dat
aging files lpa_load_oscm161_20080819.dat
aging files lpa_load_oscm161_20080826.dat
aging files lpa_load_oscm161_2008092.dat
aging files lpa_load_oscm161_2008099.dat
aging files lpa_load_oscm161_20080916.dat
aging files lpa_load_oscm161_20080923.dat
aging files lpa_load_oscm161_20080930.dat
aging files lpa_load_oscm161_2008107.dat
aging files lpa_load_oscm161_20081014.dat
aging files lpa_load_oscm161_20081021.dat
aging files lpa_load_oscm161_20081028.dat
aging files lpa_load_oscm161_2008114.dat
aging files lpa_load_oscm161_20081111.dat
aging files lpa_load_oscm161_20081118.dat
aging files lpa_load_oscm161_20081125.dat
aging files lpa_load_oscm161_2008122.dat
aging files lpa_load_oscm161_2008129.dat
aging files lpa_load_oscm161_20081216.dat
aging files lpa_load_oscm161_20081223.dat
aging files lpa_load_oscm161_20081230.dat
aging files lpa_load_oscm161_2009016.dat
aging files lpa_load_oscm161_20090113.dat
aging files lpa_load_oscm161_20090120.dat
aging files lpa_load_oscm161_20090127.dat

$

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find the year and assign to different parameters

Hi All, I have a unix directory and under which the below set of files(Years will change) will be there ##################################### TEST_DETAIL_HCR_ABC2015_T01152015.csv TEST_DETAIL_HCR_ABC2014_T01152015.csv TEST_DETAIL_HCA_ABC2013_T01152015.csv I need to assign years to... (2 Replies)
Discussion started by: weknowd
2 Replies

2. Solaris

Usage of -prune and -name in find

I am into cd /home/work/amey/history-*/ Under amey I have directories history, history-1, history-2 and under history-2 I have got 2 files 3 and 2. When I run the find command I get the below o/p. find /home/work/amey/history-*/. -name . -o -prune -type f /home/work/amey/history-1/.... (1 Reply)
Discussion started by: ameyrk
1 Replies

3. UNIX for Dummies Questions & Answers

Usage of find command

I need to find a file that has been modified in last 3-4 hours. mtime tells us about file modified in n days. Is there any way I can check for hours or minutes file modified or created before. (5 Replies)
Discussion started by: ankush_mehra
5 Replies

4. Shell Programming and Scripting

Passing wildcard parameters to find via a variable

I have a script to fix permissions which is made up of blocks like: FS_ROOT=/home/shared/Photos FS_EXCLUDE=( \( -path */.webviews -o -path */.thumbnails \) -prune -o ) find $FS_ROOT ${FS_EXCLUDE} -type d -not -perm 2770 -exec chmod 2770 "{}" \; That fragment works as expected, but no matter... (3 Replies)
Discussion started by: mij
3 Replies

5. Shell Programming and Scripting

Find Usage solved

How can I find the biggest file in a branch? I tried find / \*.\* | xargs du | sort -n 1,1 | head 1 but shell do nothings :( ---------- Post updated at 03:07 PM ---------- Previous update was at 02:41 PM ---------- Solved: find / -name \*.\* | xargs du | sort -nr | head -n 1 (0 Replies)
Discussion started by: Guccio
0 Replies

6. AIX

How to monitor the IBM AIX server for I/O usage,memory usage,CPU usage,network..?

How to monitor the IBM AIX server for I/O usage, memory usage, CPU usage, network usage, storage usage? (3 Replies)
Discussion started by: laknar
3 Replies

7. HP-UX

how can I find cpu usage memory usage swap usage and logical volume usage

how can I find cpu usage memory usage swap usage and I want to know CPU usage above X% and contiue Y times and memory usage above X % and contiue Y times my final destination is monitor process logical volume usage above X % and number of Logical voluage above can I not to... (3 Replies)
Discussion started by: alert0919
3 Replies

8. Shell Programming and Scripting

using a vaiable parameters and values to find and tar

This one took HOURS and HOURS of my life. Hopefully this post will save someone the same grief. I am using "-regex" with "find" via a variable. If I echo the command that is constructed, it looks OK. When I paste the echoed text it to the command line it runs fine. But inside the script it... (1 Reply)
Discussion started by: jjminkle
1 Replies

9. UNIX for Advanced & Expert Users

How to Find AIX system parameters

System Parameters: maximum number of processes = 2048 Network parameters: ipqmaxlen - 512 rfc1323 - 1 sb_max - 1310720 (2*655360) tcp_recvspace - 65536 tcp_sendspace - 65536 Udp_recvspace - 655360 Udp_sendspace 65536 (1 Reply)
Discussion started by: R00tSc0rpi0n
1 Replies

10. Shell Programming and Scripting

Is it possible to find parameters?

I have a shell script and i pass 3 parameters to it. Just give you an example test.sh a=y/n b=y/n c=y/n Pass y/n to a, b and c then it will decide what to do. test.sh a=y b=n c=n What i would like to do is rather then passing 3 parameter just pass a=y (only a is doing something like... (2 Replies)
Discussion started by: J_ang
2 Replies
Login or Register to Ask a Question