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:
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.
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.
once the script displays right results replace ls {} with rm {}
This User Gave Thanks to 47shailesh For This Post:
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.
once the script displays right results replace ls {} with rm {}
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?
This User Gave Thanks to neutronscott For This Post:
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?
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)
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)
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)
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)
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)
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)
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)
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)
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)