LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2016, 03:33 PM   #1
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,301
Blog Entries: 4

Rep: Reputation: 148Reputation: 148
Trying to figure out a script to look for files modified


Instead of looking in hundreds of directories manually I'm either needing a for do loop to run thru something like this

Code:
find /data/svn -mtime -30
However I will need to point it to the correct directory along with changing the time from 30, 60, 90, 180 and 360 days. Basically looking for files that have been modified and not doing it manually.

I'm not sure how to start this. I've looked online couldn't find anything solid.

Code:
for OUTPUT in $(find /data/svn -mtime -30)
do
 command1 on $OUTPUT 
 command2 on $OUTPUT 
 command3 on $OUTPUT 
done
Maybe I'm approaching this incorrectly.

thanks
 
Old 01-05-2016, 07:03 PM   #2
CSandman
Member
 
Registered: Mar 2007
Distribution: Fedora/SUSE/Debian/Knoppix
Posts: 34

Rep: Reputation: 16
Quote:
However I will need to point it to the correct directory along with changing the time from 30, 60, 90, 180 and 360 days. Basically looking for files that have been modified and not doing it manually.
I'm not sure I completely follow what you are trying to do. If you just want to find a list of files grouped into different age ranges then I think you are headed down the correct track.

Note, if you just need to run the same command once for each of the files you could use the "-exec" option on the find command.

Code:
# the trailing \; is required to tell find where the end of the command to exec is
find /data/svn -mtime -30 -exec some_command '{}' \;
I haven't tested this code out but something like the following might help you along a bit further:

Code:
for range in "30 60 90 180 360"; do 
    echo "doing \"${cmd}\" to files up to ${range} days old..."
    find /data/svn -mtime -${range} -exec ${cmd} '{}' \;
done
You also mentioned "pointing it to the correct directory". Would an additional nested loop iterating over an array of the directories do the trick? You could even populate the array using something like
Code:
$(ls -d /path/to/top/dir/*)
And finally, if you run the find command with the mtime range of 30 it will also include the files listed by find when using 60 days for the value. To create mutually exclusive lists of files in the different age groups you could maybe do something with the "-v" option in grep. Again I haven't tested this...
Code:
sixty=$(find /data/svn -mtime -60)
thirty_to_sixty=$(find /data/svn -mtime -30 | grep -v "${sixty}")

# I'm not 100 sure if the one-liner above will work so here is another option
thirty_to_sixty=$(find /data/svn -mtime -30)
for file in ${sixty}; do
    thirty_to_sixty=$(grep -v ${file} ${thirty_to_sixty})
done

Last edited by CSandman; 01-05-2016 at 07:10 PM. Reason: I originally had the symantics of "-mtime -30" vs "-mtime -60" backwards.
 
Old 01-05-2016, 10:17 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
If this is a large directory tree (more directories and inodes than the kernel will hold in its cache), it will be a lot more efficient to make find search the directory tree just once and generate all the output.
Code:
find /data/svn -mtime -30 -printf '30 %p\n' -o -mtime -60 -printf '60 %p\n' \
               -o -mtime -90 -printf '90 %p\n' -o -mtime -180 -printf '180 %p\n' \
               -o -mtime -360 -printf '360 %p\n'
You'll get one batch of lines that can be sorted/filtered/etc. by the first field.
 
1 members found this post helpful.
Old 01-12-2016, 01:33 PM   #4
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,301

Original Poster
Blog Entries: 4

Rep: Reputation: 148Reputation: 148
I'm still trying this out.

I don't understand the

Code:
-o
in the following:

Code:
find /data/svn -o -mtime -60 -printf '30 %p\n'
It comes back with

Code:
find: invalid expression; you have used a binary operator with nothing before it.
I looked in the find man page and I can't find an entry for -o. If I script up the statement above, this is what I'm getting:

Code:
./find_svn.sh: line 4: -o: command not found 
./find_svn.sh: line 6: -o: command not found
./find_svn.sh: line 7: -o: command not found
 
Old 01-12-2016, 01:59 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
Quote:
Originally Posted by JockVSJock View Post
Code:
find /data/svn -o -mtime -60 -printf '30 p\n'
It comes back with

Code:
find: invalid expression; you have used a binary operator with nothing before it.
You've got an extraneous "-o" in there. That "-o" is the logical OR operator, and belongs between two terms, which would otherwise be joined by the implied AND operator ("-a").
.

Quote:
Code:
./find_svn.sh: line 4: -o: command not found 
./find_svn.sh: line 6: -o: command not found
./find_svn.sh: line 7: -o: command not found
The reason you are getting the "-o: command not found" messages is because you are also omitting the backslash on the preceding lines. Those three lines I gave are all one command, broken into separate lines for readability
 
Old 01-13-2016, 10:24 AM   #6
JockVSJock
Senior Member
 
Registered: Jan 2004
Location: SATX
Distribution: RHEL/CentOS
Posts: 1,301

Original Poster
Blog Entries: 4

Rep: Reputation: 148Reputation: 148
Tried it again and got it to work.

The '\' threw me as its telling the find statement to start a new line.
 
Old 01-13-2016, 11:49 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,360

Rep: Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001Reputation: 2001
It's shell syntax for continuing a command onto multiple lines.
 
  


Reply

Tags
bash, find command, for loop, mtime


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
need help - script to display modified date&time of files Pawang Linux - General 3 06-01-2010 03:40 AM
files modified ? sachin1361 Linux - Enterprise 1 03-13-2007 04:11 AM
gnu make recompiles the source files fully even though the files are not modified yasothamani Linux - Software 4 02-07-2007 07:36 AM
Tar files in a dir modified before 7 days in a shell script jayachristina Linux - Newbie 4 05-14-2004 03:49 AM
How would you check which files had been modified? entm Linux - General 1 05-14-2002 10:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration