search for files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search for files
# 1  
Old 08-04-2010
search for files

Hi Gurus,

I have written the below code to search for files and zip them and archive it.

Code:
#!/bin/ksh
set -x
if [ ! -e find $ADMIN_DIR/SessLogs/ -type f -mtime -5 ]
            then
               echo "No Session Log found for the date range"
               exit
fi
#Check for the existance of Temp directory, if not create it

            if [ ! -d $ADMIN_DIR/Temp ] then
               mkdir $ADMIN_DIR/Temp
            fi

#Zip the files for the date range and move the zipped file to $ADMIN_DIR/Temp Directory

     tar cvf - `find $ADMIN_DIR/SessLogs/ -type f -mtime -5` | gzip -c 1>SessionLogs`date +%Y%m%d`.tgz
     mv $ADMIN_DIR/SessLogs/SessionLogs`date +%Y%m%d`.tgz $INFMT_ADMIN_DIR/Temp
     find $ADMIN_DIR/SessLogs -type f -mtime -5 -exec rm -f {} \;
#Trap the return code
            RC=$?
            echo "Return code for file archival process is " $RC
            if [ $RC -ne 0 ]
            then
               echo "Files not found to archive. "
            fi
#Clean up zip files that are 60 days old
     find $ADMIN_DIR/SessLogs -name *.tgz -mtime +60 -mtime -60 -exec rm -f {} \;
            RC=$?
            echo "Return code for file removal process is " $RC
            if [ $RC -ne 0 ]
            then
               echo "Files not found to clean up. "
            fi

but when i execute the script, it skips at the first line even though there is a file existing in directory. It exits with the message "No Session Log found for the date range" and when I execute the below code separately, I am able to find the file.

Code:
find $ADMIN_DIR/SessLogs -type f -mtime -5

Thanks in advance,
Sam
# 2  
Old 08-04-2010
Quote:
if [ ! -e find $ADMIN_DIR/SessLogs/ -type f -mtime -5 ]
then
echo "No Session Log found for the date range"
exit
fi
In the above script the "find" does not execute and the "if" statement looks for a file with a strange name.

Try counting the number of hits.
Code:
counter=`find $ADMIN_DIR/SessLogs/ -type f -mtime -5 | wc -l`
if [ ${counter} -eq 0 ]
then
               echo "No Session Log found for the date range"
               exit
fi

# 3  
Old 08-04-2010
Methyl,

Thanks for the reply. I was able to run the script. But it gives a warning message while zipping it.

Code:
tar: Removing leading `/' from member names

# 4  
Old 08-04-2010
Quote:
Originally Posted by svajhala
Methyl,

Thanks for the reply. I was able to run the script. But it gives a warning message while zipping it.

Code:
tar: Removing leading `/' from member names

make sure your $ADMIN_DIR variable defined/derived prior to using it.
# 5  
Old 08-04-2010
Hi,

Thanks for your reply.

Yes it is defined prior to running the script and i was able to get the value for $ADMIN_DIR in the previous section of the script.

Regards,
Srikanth
# 6  
Old 08-05-2010
Quote:
tar: Removing leading `/' from member names
I suspect that the variable $ADMIN_DIR contains an absolute path. In most versions of "tar" this would mean that when you explode the archive it must be to the exact same directory structure which can be inconvenient.
The useful feature of your "tar" is that it is removing the leading solidus from filenames which would enable you to explode the archive within a different directory tree.
In general it is better to use relative path in a "tar" archive.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search files with today date and files come anytime in between 10 pm to 1 am

Hi, i want to make script. In a directory everyday( exclude sat and sun) in between 10 pm to 1 am there are 2 files comes and when file comes it will mail us. Format for files is mentioned below. please help me on making this, and also have year end consider and if files come after 12 am it... (6 Replies)
Discussion started by: pallvi_mahajan
6 Replies

2. Shell Programming and Scripting

search information in multiple files and save in new files

hi everyone, im stuck in here with shell :) can you help me?? i have a directory with alot files (genbank files ... all ended in .gbk ) more than 1000 for sure ... and i want to read each one of them and search for some information and if i found the right one i save in new file with new... (6 Replies)
Discussion started by: andreia
6 Replies

3. Shell Programming and Scripting

search for content in files. Name of files is in another file. Format as report.

Hi I have multiple files in a folder and one file which contains a list of files (one on each line). I was to search for a string only within these files and not the whole folder. I need the output to be in the form File1<tab>string instance 2<tab> string instance 2<tab>string instance 3... (6 Replies)
Discussion started by: pkabali
6 Replies

4. Shell Programming and Scripting

"Search for files in a file and write these files to another file which sould be in some other direc

Hi , Need to shell script to extracts files names and write those to another file in different directory. input file is inputfile.txt abc|1|bcd.dat 123 david123 123 rudy2345 124 tinku5634 abc|1|def.dat 123 jevid123 123 qwer2345 124 ghjlk5634 abc|1|pqr.txt 123 vbjnnjh435 123 jggdy876... (1 Reply)
Discussion started by: dssyadav
1 Replies

5. Shell Programming and Scripting

How to access files from different directories and to perform search action in those files?

Hi, I want to access files from different directories (for example: /home/dir1/file1 , /home/dir2/file2 ...) Like this i have to access these files(file1, file2...). (3 Replies)
Discussion started by: bangarukannan
3 Replies

6. SuSE

Search all files based on first and in all listed files search the second patterns

Hello Linux Masters, I am not a linux expert therefore i need help from linux gurus. Well i have a requirement where i need to search all files based on first patterns and after seraching all files then serach second pattern in all files which i have extracted based on first pattern.... (1 Reply)
Discussion started by: Black-Linux
1 Replies

7. Shell Programming and Scripting

search files

Hello folks I Hope everyone is fine. I have list of files below aa.tph p3iu.tph a984c.c3c p8xc4.pql I want to search all above files path from /mydata. How to do it with find command with script? (1 Reply)
Discussion started by: learnbash
1 Replies

8. UNIX for Advanced & Expert Users

Search files with specfic extention and later search content

Hi, I would appriciate if somebody can help me figure out how to search for all the *.xml file under a specific directory and subdirectroies (/home/username) and later search of content "<start>" inside the xml file returned by search. -Lovin.V (2 Replies)
Discussion started by: lovi_v
2 Replies

9. Shell Programming and Scripting

search through files

Dears, I have a directory that contains many files,name of the file has a speciefied format like: B<date>-time B20080203-1510,B20080203-1520,.....etc these files contains many counters in the following format: <conter name> <counter value> I need to filter these files based on thier... (5 Replies)
Discussion started by: mm00123
5 Replies

10. Shell Programming and Scripting

search for files excluding binary files

Hi All, I need a solution on my following find command find ./.. -name '*.file' -print BTW This gives me the output as belows ./rtlsim/test/ADCONV0/infile/ad0_dagctst.file ./rtlsim/test/ADCONV0/user_command.file ./rtlsim/test/ADCONV0/simv.daidir/scsim.db.dir/scsim.db.file... (2 Replies)
Discussion started by: user_prady
2 Replies
Login or Register to Ask a Question