Find Files with a input in directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find Files with a input in directories
# 1  
Old 11-09-2012
Find Files with a input in directories

So i have directories that store logs, 1 directorie for each day, with the name like this :

2012_07_01/
2012_07_02/

and for each directorie we have the logs, inside them lives the logs for that day, and every log have this name pattern :

sensor1_log_from_2012_07_01_10_:_30_to_2012_07_01_10_:_50

and every file have 100 mb

now that the introduction about the system is done i can tell the problem, i do have a script that is on the bottom to show you how i do things right now but, question is if i have a leap in days or hours or anything between my $startdate and $enddate (the logs are created with timestamps for each 100 mb collected )value that i use to search for the files that i want to see in the directorie logs, then a problem comes because we can have logs that came like this :

sensor1_log_from_2012_07_01_10_:_30_to_2012_07_03_10_:_30

and think on this what if i want to see logs from 2012_07_02 ??? and this can happen because if a sensor dont collect enough traffic for the 100 mb then he goona leaps the end date until it reachs the 100mb but, for this i'm thinking or if i can search for the last know file before the date i search for $startdate ? that would do the trick but i dont know how to do it right now i have this :

Code:
Code:
cmd={ls | sort | sed -n "/$stardate/"}

if [cmd eq True]:
then 

 for dir in `ls | sort | sed -n "/$startdate/,/$enddate/p"` ; do
 
    if [ `ls $dir | wc -l` -gt 0 ] ; then
         ls $dir > list.txt
     else
         rm $dir
     fi
 done

else 


done

soo with that said can anyone give me a hand , i know some people use find like:
Code:
find ${DIR} \ (-name $startdate and $enddate) -ctime > /tmp/list.txt

i just dont know how to combine that in my script in the right way or even to say in a simple if to search for the last file recorded before the $startdate.

Last edited by jim mcnamara; 11-09-2012 at 08:56 PM.. Reason: the else is the key for the solution that is were im stuck
# 2  
Old 11-09-2012
What exactly are you looking for? Do you want to find files which are older than a particular file or you want to find it using name?
If you are looking for files older than your reference file (let's say ref_file) you can use this :

Code:
find . ! -newer ref_file print

(! for not newer than or rather older than your reference file)
Moderator's Comments:
Mod Comment Please use code tags

Last edited by jim mcnamara; 11-09-2012 at 08:55 PM..
# 3  
Old 11-09-2012
I understood the problem lays in the fact that a single file can span several days worth of data, such as

Quote:
Code:
sensor1_log_from_2012_07_01_10_:_30_to_2012_07_03_10_:_30

You need a method to find appropriate file for a given date, if I am not mistaken.

I propose to use gawk capacity to manipulate dates and use unix epoch date form, which can be compared arithmetically.

1) convert given date/time to epoch form
2) go through files and for each extract start_time and end_time from the file name and using a mktime function convert both into the unix epoch time
3) if given_time >= start_time and given_time <= end_time then you got the file needed.

Note: I'm not sure if awk/nawk has mktime function, that is why emphasis on gawk.

To demonstrate I put a simple script:

Code:
#!/bin/ksh
s="sensor1_log_from_2012_07_01_10_:_30_to_2012_07_03_10_:_30"
echo $s|
gawk '
BEGIN{
        qry1_time = mktime("2012 06 20 18 30 00");
        qry2_time = mktime("2012 07 02 10 00 00");
        qry3_time = mktime("2012 07 03 22 45 00");
}
{
n=split($0, arr, "_");
str_fr_time = arr[4]" "arr[5]" "arr[6]" "arr[7]" "arr[9]" 00"
str_to_time = arr[11]" "arr[12]" "arr[13]" "arr[14]" "arr[16]" 00"
fr_time = mktime(str_fr_time);
to_time = mktime(str_to_time);
print "FR:"str_fr_time
print "TO:"str_to_time
if(qry1_time >= fr_time && qry1_time <= to_time)  
        print "OK for " strftime("%x %X", qry1_time);
else 
        print "NO for " strftime("%x %X", qry1_time);
 
if(qry2_time >= fr_time && qry2_time <= to_time)  
        print "OK for " strftime("%x %X", qry2_time);
else 
        print "NO for " strftime("%x %X", qry2_time);
if(qry3_time >= fr_time && qry3_time <= to_time)  
        print "OK for " strftime("%x %X", qry3_time);
else 
        print "NO for " strftime("%x %X", qry3_time);
 
}'

And here is the output
Code:
 
FR:2012 07 01 10 30 00
TO:2012 07 03 10 30 00
NO for 06/20/12 18:30:00
OK for 07/02/12 10:00:00
NO for 07/03/12 22:45:00

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find common files between two directories

I have two directories Dir 1 /home/sid/release1 Dir 2 /home/sid/release2 I want to find the common files between the two directories Dir 1 files /home/sid/release1>ls -lrt total 16 -rw-r--r-- 1 sid cool 0 Jun 19 12:53 File123 -rw-r--r-- 1 sid cool 0 Jun 19 12:53... (5 Replies)
Discussion started by: sidnow
5 Replies

2. UNIX for Dummies Questions & Answers

Loop over certain user directories and find files

Hello I have user directories that contain /temp directory. Example folders: /user1/temp/ /user2/temp/ /user3/temp/ How can i loop over all user directories and find all files only in their /temp folder? Thanks a lot for help! (3 Replies)
Discussion started by: flavius42
3 Replies

3. UNIX for Dummies Questions & Answers

Find Files In A List with known Partial Directories

First I'm new to Linux and have used the find command pretty often but this is where I've hit a snag. I have a file that contains 3500 files that I want to find and then eventually copy to my own directory (these files are all on a shared directory at work atm). Our work computer are huge and... (2 Replies)
Discussion started by: Myrona
2 Replies

4. UNIX for Advanced & Expert Users

Find all files in the current directory excluding hidden files and directories

Find all files in the current directory only excluding hidden directories and files. For the below command, though it's not deleting hidden files.. it is traversing through the hidden directories and listing normal which should be avoided. `find . \( ! -name ".*" -prune \) -mtime +${n_days}... (7 Replies)
Discussion started by: ksailesh1
7 Replies

5. Shell Programming and Scripting

Find only files/directories with different permissions/owners

My git post-update has the following lines in it to make sure the permissions are set right: find /usr/local/apache/htdocs -type d -print0 | xargs -0 chmod 755 find /usr/local/apache/htdocs -type f -print0 | xargs -0 chmod 644 chown -R apache:apache /usr/local/apache/htdocsThe only problem is... (5 Replies)
Discussion started by: dheian
5 Replies

6. Shell Programming and Scripting

Script to go Into Directories and Find/Delete files

I have a task, I usually do manually, but with growing responsibilities I tend to forget to do this weekly, I want to write a script that automates this, but I cant seem to work it out in my head, I have the shell of it out, but need help, and you guys have helped me with EVERY problem I have... (5 Replies)
Discussion started by: gkelly1117
5 Replies

7. Shell Programming and Scripting

Script to delete files with an input for directories and an input for path/file

Hello, I'm trying to figure out how best to approach this script, and I have very little experience, so I could use all the help I can get. :wall: I regularly need to delete files from many directories. A file with the same name may exist any number of times in different subdirectories.... (3 Replies)
Discussion started by: *ShadowCat*
3 Replies

8. Homework & Coursework Questions

Find and delete empty files and directories

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Need to make a script, to remove all empty files and folders from current category. It also should show the name... (2 Replies)
Discussion started by: Itixop
2 Replies

9. UNIX for Advanced & Expert Users

find -type d returning files as well as directories

Can anyone see why the following command returns all files and not just the directories as specified? find . -type d -exec ls -F {} \; Also tried find . -type d -name "*" -exec ls -F {} \; find . -type d -name "*" -exec ls -F '{}' \; -print Always returns all files :-\ OS is... (2 Replies)
Discussion started by: tuns99
2 Replies

10. Shell Programming and Scripting

Find files in Directories

Hello, We have in a file a list of the path of files from one server. We want to search if these files exist on another server , but the path on this new server isn't the same. We want to use the command "awk" but there isn't the god way. Example: on server 1 in a file : listServer1.txt... (4 Replies)
Discussion started by: steiner
4 Replies
Login or Register to Ask a Question