How to search directory for specific file?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to search directory for specific file?
# 1  
Old 04-17-2012
How to search directory for specific file?

I am new to Unix scripting and would like some help. Here is my scenario:
1) I have a text files that contains two fields: file name and retention period in months:
Code:
File1 36
file2 24
File3 12

2) The directory I am searching contains sequential files.
3) I need to be able to take the file name from step1 and search the directory to find a match on the file name. If a match is found, need to check to find out if the file time stamp is older than the retention period. If it is older, then I will need to delete it.
4) I have this script that I inherited from a person who wrote it. The problem is I am not getting accurate results.

Code:
    #set -xv
    >size
    >files_deletion
    #######searching the file which is present in rule############
    STAT=/prod/STAT/seqfiles
    ###########searching the files falling under retention policy##################
    #rules.txt contains all the file name ,ret period
    cat /test/mkamal/ret/rulesx.txt |while read name ret_period
    do
      period=`echo "30.5 * ${ret_period}"`
      all_files=`find $STAT -name $name -exec ls -l {} \;`
      file=`find $STAT -name $name -mtime +"$period" -exec ls -l {} \;`
      if [ -s ${file} ]
      then
        echo "$all_files,$ret_period">>size
      else
        echo "$file,$ret_period">>size 
        echo "$file $ret_period">>files_deletion
      fi
    done

Any help would be greatly appreciated.
Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-17-2012 at 06:24 PM..
# 2  
Old 04-17-2012
This is what the above code is doing:
  • Reading filename and retention from a file.
  • Doing a month to day conversion with wrong syntax.
  • Not sure what the use of all_files variable, later checking and storing if the filename is older than retention, which is dependent on incorrect value of period.
  • Then checking if nonempty filename exist greater than retention then store some info in file else store some other info in additional files. Do you need log of what files were deleted ?

There is no code to delete the obsolete file.

Code:
STAT=/prod/STAT/seqfiles
cat /test/mkamal/ret/rulesx.txt | while read name ret_period
do
    period=`echo "30.5 * ${ret_period}" | bc `
    find $STAT -name $name -mtime +"$period" -exec ls {} \;`
done

once the script displays right results replace ls {} with rm {}
This User Gave Thanks to 47shailesh For This Post:
# 3  
Old 04-18-2012
Thank you very much 47shailesh. I have tried the code you provided me and I am still not getting correct results. I got this error message while script was executing syntax error on line 1, teletype which I believe have to do with the bc command. Also, I am getting results with newer files that should not be removed.
This script was given to me to fix. I am a mainframe programmer with very little unix experience. The whole idea for this script is we have a text file with file names (like CM.WM449M.P010.M01.PREMMAST.M1007) and retention period in months. For this file, the period is 36 months.
I need to use the text files with the file names and retention period and match each file name with the directory where the actual files reside. If there is a match on the file name, then need to check how old the file is, if it is older than the retention period, for the above example, 36 months (1098 days), I need to move it to different folder so we can free up space. Does that make sense? Any help would be greatly appreciated.
Quote:
Originally Posted by 47shailesh
This is what the above code is doing:
  • Reading filename and retention from a file.
  • Doing a month to day conversion with wrong syntax.
  • Not sure what the use of all_files variable, later checking and storing if the filename is older than retention, which is dependent on incorrect value of period.
  • Then checking if nonempty filename exist greater than retention then store some info in file else store some other info in additional files. Do you need log of what files were deleted ?
There is no code to delete the obsolete file.

Code:
STAT=/prod/STAT/seqfiles
cat /test/mkamal/ret/rulesx.txt | while read name ret_period
do
    period=`echo "30.5 * ${ret_period}" | bc `
    find $STAT -name $name -mtime +"$period" -exec ls {} \;`
done

once the script displays right results replace ls {} with rm {}
# 4  
Old 04-18-2012
What shell, which OS?
# 5  
Old 04-18-2012
Quote:
Originally Posted by neutronscott
What shell, which OS?
ksh shell on Solaris box. Does that help? I am all new to the unix world
# 6  
Old 04-18-2012
Was hoping to be able to use GNU date .. I see a stray backtick in 47's code at the end of the find command, otherwise it should produce results. Are you certain they are incorrect, what do you use to verify? Also the cat there is useless use of cat.

You're creating a file size which would end up being identical to the input file ... was it meant to include size information as well?

Code:
stat=/prod/STAT/seqfiles
while read name ret_period; do
    days=`echo "30.5 * ${ret_period}" | bc`
    old=`find "$stat" -name "$name" -mtime +"$days" -print`
    if [ -n "$old" ]; then
        echo "$old $ret_period" >> files_deletion
    fi
done < "/test/mkamal/ret/rulesx.txt"

This User Gave Thanks to neutronscott For This Post:
# 7  
Old 04-18-2012
Hi neutronscott, Thank you very much for your response. I copied your script like this
#!/usr/bin/ksh
#set -xv
>size
>files_deletion
STAT=/prod/STAT/seqfiles
while read name ret_period; do
days=`echo "30.5 * ${ret_period}" | bc`
old=`find "$STAT" -name "$name" -mtime +"$days" -print`
if [ -n "$old" ]; then
echo "$old $ret_period" >> files_deletion
done < "/test/mkamal/ret/rulesx.txt"
I tried to execute it and I got this message on the screen
ksh: ./search1: cannot execute
Then on the command line, I entered the script line by line, after entering the last line, I got this error message
ksh: syntax error: `done' unexpected
Any thoughts what I did wrong?


Quote:
Originally Posted by neutronscott
Was hoping to be able to use GNU date .. I see a stray backtick in 47's code at the end of the find command, otherwise it should produce results. Are you certain they are incorrect, what do you use to verify? Also the cat there is useless use of cat.

You're creating a file size which would end up being identical to the input file ... was it meant to include size information as well?

Code:
stat=/prod/STAT/seqfiles
while read name ret_period; do
    days=`echo "30.5 * ${ret_period}" | bc`
    old=`find "$stat" -name "$name" -mtime +"$days" -print`
    if [ -n "$old" ]; then
        echo "$old $ret_period" >> files_deletion
    fi
done < "/test/mkamal/ret/rulesx.txt"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to search in specific directory using find?

Hi, Is there any way to use find command and search only specific subdirectories in a directory. for example /home/d1 /home/d2 /home/d3 i want to search in the following directories /home /home/d1 /home/d2 i do not want the find command to search the /home/d3 directory. (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

3. UNIX for Dummies Questions & Answers

Search for a specific String in a log file for a specific date range

Hi, I have log file which rolls out every second which is as this. HttpGenRequest - -<!--OXi dbPublish--> <created="2014-03-24 23:45:37" lastMsgId="" requestTime="0.0333"> <response request="getOutcomeDetails" code="114" message="Request found no matching data" debug="" provider="undefined"/>... (3 Replies)
Discussion started by: karthikprakash
3 Replies

4. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

5. Shell Programming and Scripting

Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as (7 Replies)
Discussion started by: manigrover
7 Replies

6. Shell Programming and Scripting

Urgent request to consider:Search specific name in a file and fetch specific entries

Hi all, I have 2 files, One file contain data like this FHIT CS CHRM1 PDE3A PDE3B HSP90AA1 PTK2 HTR1A ESR1 PARP1 PLA2G1B These names are mentioned in the second file(Please see attached second file) as # Drug_Target_X_Gene_Name:(Where X can be any number (1-1000) (1 Reply)
Discussion started by: manigrover
1 Replies

7. UNIX for Advanced & Expert Users

allow user to use sudo cp on a specific directory and only a specific file

Is there a way to allow a user to use sudo cp on a specific directory and only a specific file? (6 Replies)
Discussion started by: cokedude
6 Replies

8. UNIX for Dummies Questions & Answers

How to search all the files in a directory for a specific string

Hi Guys, I want to search the content of all the files (of a particular type like .txt) in a directory for a specific string pattern. Can anyone help me? Thanks (7 Replies)
Discussion started by: mwrg
7 Replies

9. Shell Programming and Scripting

Search for a file in specific directory

I have to search a file in a prticular directory. filename will be passed through command line. The directory may contain subdirectory. i.e. suppose directory in /u03/appl (it can hard coded in script). This directory may contain subdirectory. $ scriptname.sh filename output should be... (2 Replies)
Discussion started by: jadoo_c2
2 Replies

10. UNIX for Dummies Questions & Answers

Linux shortcutkey to search specific file from a list of directory?!

Hi, I'm the new user of linux/unix. Can I ask that anybody know how to use the linux/unix shortcut key to search a specific file from a list of directory? For example, I know the file name that I want to search. But I forget which directory or location is my desired file put.Got any shortcut... (7 Replies)
Discussion started by: patrick87
7 Replies
Login or Register to Ask a Question