How can we use a variable value in find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can we use a variable value in find command
# 1  
Old 04-22-2010
Question How can we use a variable value in find command

Hi All:

I want to write a shell script to store OS current date - 1 day value in a variable.

Current Date is: 22042010 and new value is 21042010

I have multiple files having date value in name in ddmmyyyy format for multiple day, so I want to find all the files based on the date variable.

following is the list of files exists in /oracle/app/xml
Code:
CUSTOMER_04-05042010.XML
CUSTOMER_06-06042010.XML
CUSTOMER_10-10042010.XML
CUSTOMER_21-21042010.XML
CUSTOMER_20-20042010.XML
ACCOUNT_20-21042010.XML
ACCOUNT_20-20042010.XML
ACCOUNT_20-18042010.XML
ACCOUNT_20-16042010.XML

The find command shoule be retrieve files: CUSTOMER_20-21042010.XML and ACCOUNT_20-21042010.XML and copy to a new folder.

I wrote script like below
Code:
DTDD=`date +"%d"`
DDNDD=$(($DTDD-1))`date +"%m%Y"`
echo $DDNDD
cd /oracle/app/xml/DAILY
rm *.XML
cd /oracle/app/xml
var=`find . -name '*$DDNDD.XML' |wc -l`
var=`expr $var \* 1`
echo $var
  if [ $var -gt 0 ]
then
cp `find . -name '*$DDNDD' ` DAILY/
else 
echo "No files found to be copied"
fi

I have also written below loop to extract the date value from file name and its working fine:
Code:
 
for i in `ls *.XML`
 do
echo $i | cut -d"." -f1|awk '{print " " $1".XML " }' >> /oracle/app/xml_copy.log
#         echo $i | cut -d"-" -f2|awk '{print $1 }' | cut -d"." -f1|awk '{print $1}'
done

But it I always returned No files found to be copied. Smilie

I am not sure that whether I can use the variable in find command or not.

Experts help needed now.

Regards,

Hassan

Last edited by Scott; 04-22-2010 at 01:02 PM.. Reason: Code tags, please...
# 2  
Old 04-22-2010
Several issues in the script.
Main problem is that the variable in the find line is between single quote characters. It will not get expanded.
Other issue is that when there is more than one file the find output would contain more than one filename. This is not good practice because it fails if filenames contain spaces and can generate a command which is too long.

Assuming you really mean to "cp" (not "mv") the files, here is a method broadly in your style.

Code:
echo $DDNDD
cd /oracle/app/xml/DAILY
rm *.XML  2>/dev/null
#
counter=0
cd /oracle/app/xml
find . -name \*${DDNDD}\.XML -print | while read filename
do
      cp -p "${filename}" DAILY
      counter=`expr ${counter} + 1`
done
if [ ${counter} = 0 ]
then
    echo "No files found to be copied"
fi


Last edited by methyl; 04-22-2010 at 02:13 PM.. Reason: Corrections. And junk errors from "rm"
This User Gave Thanks to methyl For This Post:
# 3  
Old 04-22-2010
There are a couple areas for improvement of your shell script. However, to address your primary concern, yes you can use variables in the find command. However, to allow those variables to be interpreted, you will need to double-quote the find pattern:

Code:
var=`find . -name "*$DDNDD.XML" |wc -l`

...

cp `find . -name "*$DDNDD" ` DAILY/

One thing I notice is that you may want to pipe the output of your first find command to a variable, then create $var from an echo command piped to wc -l. In your second command, you can use something like:

Code:
echo $foundFileList | xargs -iFILE cp FILE DAILY/

This saves you having to execute the same find twice.
This User Gave Thanks to turk451 For This Post:
# 4  
Old 04-23-2010
Hi

Thanks for your support dears:

The code is looking like below

Code:
# Store OS current date in a variable then subtract <n> days and store in new variable
DTDD=`date +"%d"`
DDNDD=$(($DTDD-1))`date +"%m%Y"`
#echo $DDNDD
# Go to destination directory and remove the old files
cd /oracle/app/aml/xml/DAILY
rm *.XML 2>/dev/null
# Go back to the source directory and find the file according to the $DDNDD value
cd /oracle/app/aml/xml
var=`find . -name "*$DDNDD.XML" |wc -l`
var=`expr $var \* 1`
# If any file found accoring to the above given parameter then copy the files in DAILY folder
if [ $var -gt 0 ]
  then
# Below command will copy the files into DAILY directory
      cp `find . -name "*$DDNDD*" ` DAILY/
# Below for LOOP will create log files with File names and number of files that has been copied into DAILY folder
      echo "==============================================================" >> /oracle/app/aml/copy_xml.log
      echo "Script Execution Date         :" `date` >> /oracle/app/aml/copy_xml.log
      echo $var "file found for Data Date   :" $DDNDD >>  /oracle/app/aml/copy_xml.log
      echo "Files Listing                 :" >> /oracle/app/aml/copy_xml.log
      echo "==============================================================" >> /oracle/app/aml/copy_xml.log
        cd /oracle/app/aml/xml/DAILY
        for i in `ls *.XML`
        do
        echo $i | cut -d"." -f1|awk '{print " " $1".XML " }' >> /oracle/app/aml/copy_xml.log
# Following remarked line would retrieve the date value from file name
#         echo $i | cut -d"-" -f2|awk '{print $1 }' | cut -d"." -f1|awk '{print $1}'
        done
      echo "=====================================================" >> /oracle/app/aml/copy_xml.log
      echo "  " >> /oracle/app/aml/copy_xml.log
# When there is no file exists accorting to the given criteria then update the log as below
  else
      echo "===========================================================" >> /oracle/app/aml/copy_xml.log
      echo "Script Execution Date      :" `date` >> /oracle/app/aml/copy_xml.log
      echo $var "file found for Data Date :" $DDNDD >>  /oracle/app/aml/copy_xml.log
      echo "===========================================================" >> /oracle/app/aml/copy_xml.log
      echo " " >>/oracle/app/aml/copy_xml.log
 fi
# Go to the Destination directory and chmod to 777 as Automizer will pick the file from this folder
cd /oracle/app/aml/xml/DAILY/
chmod 777 *.XML 2>/dev/null

Now I have seen a problem regarding subtracting the <n> days from date.
As If the date is 01-APR-2010 then this script will subtract one day and new value will be zero i.e. 00042010 and the files of 31-MAR-2010 will not be copied. The same will heppen for 1st day of every month.
How would this situation handled?

Similarly when I used
Code:
 DTDD=`date +"%d"`
echo $DTDD
DDNDD=$(($DTDD-21))`date +"%m%Y"`
echo $DDNDD

It returned
Code:
 23
2042010

The leading zero is not displaying means the files of day 2, daye 12, day 22 will be copied and this will also create the problem.
I want to know date is there is any statement/command /procedure to directly deduct <n> days from date i.e. we use
Code:
select sysdate - 1 from dual

in oracle and then change the date format in "%d%m%y"

---------- Post updated at 01:43 PM ---------- Previous update was at 11:01 AM ----------

Hi All:

I am able to find one script on net at below address to resolved my error.

http://www.williamrobertson.pwp.blue...shift_date.txt

Although it is a too complex script to understand for persone like me who is a new in AIX scripting but very very helpful. This script is using date in "YYYYMMDD" format so I changed my script as below

Code:
# Store OS current date in a variable then call another script shift_dates.sh to subtract or add
##  <n> days and store in new variable
todays_date=$(date '+%Y%m%d')
newdate=$(./shift_dates.sh $todays_date -2)
#echo $newdate
yy=$(echo $newdate|cut -b 1-4)
mm=$(echo $newdate|cut -b 5-6)
dd=$(echo $newdate|cut -b 7-8)
DDNDD=$dd$mm$yy
#echo $DDNDD
# Go to destination directory and remove the old files
cd /oracle/app/aml/xml/DAILY
rm *.XML 2>/dev/null
# Go back to the source directory and find the file according to the $DDNDD value
cd /oracle/app/aml/xml
var=`find . -name "*$DDNDD.XML" |wc -l`
var=`expr $var \* 1`
# If any file found accoring to the above given parameter then copy the files in DAILY folder
if [ $var -gt 0 ]
  then
# Below command will copy the files into DAILY directory
      cp `find . -name "*$DDNDD*" ` DAILY/
# Below for LOOP will create log files with File names and number of files that has been copied into DAILY folder
      echo "==============================================================" >> /oracle/app/aml/copy_xml.log
      echo "Script Execution Date         :" `date` >> /oracle/app/aml/copy_xml.log
      echo $var "file found for Data Date   :" $DDNDD >>  /oracle/app/aml/copy_xml.log
      echo "Files Listing                 :" >> /oracle/app/aml/copy_xml.log
      echo "==============================================================" >> /oracle/app/aml/copy_xml.log
        cd /oracle/app/aml/xml/DAILY
        for i in `ls *.XML`
        do
        echo $i | cut -d"." -f1|awk '{print " " $1".XML " }' >> /oracle/app/aml/copy_xml.log
# Following remarked line would retrieve the date value from file name
#         echo $i | cut -d"-" -f2|awk '{print $1 }' | cut -d"." -f1|awk '{print $1}'
        done
      echo "==============================================================" >> /oracle/app/aml/copy_xml.log
      echo "  " >> /oracle/app/aml/copy_xml.log
# When there is no file exists accorting to the given criteria then update the log as below
  else
      echo "===========================================================" >> /oracle/app/aml/copy_xml.log
      echo "Script Execution Date      :" `date` >> /oracle/app/aml/copy_xml.log
      echo $var "file found for Data Date :" $DDNDD >>  /oracle/app/aml/copy_xml.log
      echo "===========================================================" >> /oracle/app/aml/copy_xml.log
      echo " " >>/oracle/app/aml/copy_xml.log
 fi
# Go to the Destination directory and chmod to 777 as Automizer will pick the file from this folder
cd /oracle/app/aml/xml/DAILY/
chmod 777 *.XML 2>/dev/null

Regards,

Last edited by lodhi1978; 04-23-2010 at 03:39 AM.. Reason: Adding more issue
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

String variable as part of expression in find command

Hi, I am new in scripting, and I am currently working on a script that will look for other files in a certain directory and exclude some file type. this works fine:Find_File2Exclude=`find ${paths} -maxdepth 1 -type f \( ! -iname '*.out' ! -iname '*.auc' ! -iname '*.cps' ! -iname '*.log' ! -iname... (4 Replies)
Discussion started by: kedd05
4 Replies

2. Shell Programming and Scripting

Grep command to find string in Variable in file2

Hi , I am executing 2 queries and output is saved in file1.txt and file2.txt example of file1.txt Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,, Testing Excel _.xlsx,/Lab/Development and... (3 Replies)
Discussion started by: Sunil Mathapati
3 Replies

3. Shell Programming and Scripting

Variable inside -name option for find command

Hi, I am not able to get output for find command if there are variables defined inside -name option . Please check below example 1) ###VARIABLES DEFINED process="fast" temperature="125c" voltage="0p935v" 2) I don't get output for below find command find -L <PATH> -type f \( -name... (2 Replies)
Discussion started by: gujrathinr
2 Replies

4. Shell Programming and Scripting

Output of find command to variable?

Hi, I'd like to assign the output of the find command to a variable. What I need is to run the find command, and if it returns zero files, the program exits. so i'm trying to assign the output of the find command to the $var1 variable....and then if this is less than one, I echo a... (2 Replies)
Discussion started by: horhif
2 Replies

5. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

6. Shell Programming and Scripting

To store the file name o/p from find command in to a variable

Hi How to use a variable to store the filename of a file which was found by the 'find' command. can this be practical-->var = find . -name "filename.dat" Please help.. (1 Reply)
Discussion started by: ayyappaas
1 Replies

7. Shell Programming and Scripting

Using variable to store of find command

Hello Experts I am newbie to unix and writing one script to make archive files Problme i am facing is : I have used find command to find the type of files and I am storing find command results in a variable. When I echo the variable I can see that path is printed properly but when i am... (5 Replies)
Discussion started by: mitsyjohn
5 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Variable for -name causing issue in Find command

Hi there, I'm trying to find files that are greater then 30 days old, zip them and move to a different directory. I'm encountering an issue passing a variable (FilesToFind) to name within the find command. Here's the code I'm running: #! /usr/bin/sh FileDir=/home/ariba... (2 Replies)
Discussion started by: ParNone
2 Replies

10. Windows & DOS: Issues & Discussions

Setting a variable to result of FIND command

I am working on a batch script where a filter is placed on a directory, and the files that come out of that filter have to be copied into another directory. More specifically, I am trying to set the results of a FIND command to a variable, so that I may access this variable / file later. The... (2 Replies)
Discussion started by: JP Favara
2 Replies
Login or Register to Ask a Question