• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Find command in Unix/Linux

Hi,

How to use the Find command  for pulling out Directories exactly the  day before yesterday ones.

I tried something like this:

find * -maxdepth 1 -mindepth 1  -type d   -mtime 2

But  additional to OCT 19 it is also getting OCT 18 dir.
0
new_perl_user
Asked:
new_perl_user
1 Solution
 
varontronCommented:
Hi,

The mtime numeric logic is n*24 hours, and is not specific to dates.

You can use mmin, however, but first you have to calculate the minutes since midnight the day before yesterday and since midnight yesterday.  You'll end up with something like this, assuming it is now 1800 hours:

find * -type d -mmin -2520 -and -mmin +1080

that directories modified in less than 2520 minutes and more than 1080 minutes

hth,
Dave
0
 
TintinCommented:

Use the -daystart flag to ensure you are only matching from midnight onwards.

find . -maxdepth 1 -type d -daystart -mtime 2

Open in new window

0
 
new_perl_userAuthor Commented:
can we go by dates instead of time as time being sensitive. If so can you please suggest me using date
and the bad thing is I can't hard code the date because  this find command will be running from an automated script.

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TintinCommented:
You can use a reference file to achieve this:

#!/bin/bash
START=/tmp/start$$
END=/tmp/end$$

touch -d $(date -d "2 days ago" +%Y%m%d) $START
touch -d $(date -d "1 day ago" +%Y%m%d) $END

find /some/dir -maxdepth 1 -type d -newer $START -a -not -newer $END
rm -f $START $END

Open in new window

0
 
new_perl_userAuthor Commented:
Hi Tintin,

Thank you for the above logic. I tried it the above one but it is erroring out. Could you pls help me..

#!/bin/bash
START=/tmp/start$$
END=/tmp/end$$

touch -d $(date -d "2 days ago" +%Y%m%d) $START
touch -d $(date -d "1 day ago" +%Y%m%d) $END

  ssh servery@xxx.xx.xxx.xx "cd /cygdrive/d//FILES; /usr/bin/find * -maxdepth 1 -type d -newer $START -a -not -newer $END
rm -f $START $END";

Open in new window


Error:

/usr/bin/find: `/tmp/start19713': No such file or directory
0
 
TintinCommented:
Ah, you didn't mention you were running this on a remote server.

Easiest way is to copy the script over and just invoke it, ie:

ssh servery@xxx.xx.xxx.xx "/path/to/script"

Open in new window

0
 
new_perl_userAuthor Commented:
I tired to see if it working on local server. but for some reason it is not doing anything neither creating start and end files nor finding the files.
#!/bin/bash
START=/tmp/start$$
END=/tmp/end$$

touch -d $(date -d "2 days ago" +%Y%m%d) $START
touch -d $(date -d "1 day ago" +%Y%m%d) $END

find /usr/home/Data/Files  -maxdepth 1 -type d -newer $START -a -not -newer $END
rm -f $START $END

Open in new window

0
 
TintinCommented:
Didn't see that you were using FreeBSD.  In which case, it uses a different version of the touch command.

Change to

#!/bin/bash
START=/tmp/start$$
END=/tmp/end$$

touch -t $(date -d "2 days ago" +%Y%m%d0000) $START
touch -t $(date -d "1 day ago" +%Y%m%d0000) $END

find /usr/home/Data/Files  -maxdepth 1 -type d -newer $START -a -not -newer $END
rm -f $START $END

Open in new window

0
 
new_perl_userAuthor Commented:
Hi,

It is working now thanks. But a small help, right now it is capturing the output as below:

/usr/home/Data/FILES
/usr/home/DATA/FILES/BATCH0098
/usr/home/DATA/FILES/BATCH0098/2578022W
/usr/home/DATA/FILES/BATCH0112
/usr/home/DATA/FILES/BATCH0112/101176137

Open in new window


Can we get something like below:

BATCH0098/2578022W
BATCH0112/101176137
0
 
new_perl_userAuthor Commented:
No Problem, able to get the above one by adding min depth and tweaking max depth.
0
 
gheistCommented:
traversing toplevel directory reads it once, so mindepth seems impractical....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now