LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-23-2011, 07:42 PM   #1
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431
Blog Entries: 32

Rep: Reputation: 3
find to get the number of directories modified within last week


Hi, I looked up and found "find" is a function that "looks for files" but I am not really sure about the syntax even after I looked up the manual, how do I use it to count the number of directories last modified within a week?
Also seperately, a list that was last accessed within a week?
Thanks,
Ted
 
Old 11-23-2011, 08:38 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Use '-type d' to find directories, '-mtime' to search for modification times, '-atime' to search for access times, and use '-n' to specify less than a set number of days.

For example, to find directories modified in the last 7 days:
Code:
find . -type d -mtime -7

Last edited by neonsignal; 11-23-2011 at 11:56 PM. Reason: typo in parameter, see post below
 
1 members found this post helpful.
Old 11-23-2011, 09:49 PM   #3
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
Quote:
Originally Posted by neonsignal View Post
Use '-type d' to find directories, '-mtime' to search for modification times, '-atime' to search for access times, and use '-n' to specify less than a set number of days.

For example, to find directories modified in the last 7 days:
Code:
find . -type d -mtype -7
Hi, thanks!, did u mean
Code:
find . -type d -mtime -7
Thanks,
Ted
 
Old 11-23-2011, 09:54 PM   #4
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
Sorry, for asking a bit more, how would I match the folders in the root directory only?
eg.
/sda1/ok/aaa
/sda1/dd/aaw
/sda1/bb/ffe
/sda1/ad/efe

I only want the top level, /sda1, so the universal set of folder that it will draw from is only ok, dd, bb, ad. it would not consider subdirectories.
Code:
find "/sda1/ -type d -mtime -7 -name "/sda1/*/"
Is this correct? or wrong? It took a long time to make each search, I wonder if it could be quicker as well.
Thanks,
Ted
 
Old 11-23-2011, 10:15 PM   #5
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
Use '-maxdepth 1' if you want to restrict the recursion to one subdirectory level, rather than trying to do name matching (the path is not included in the name match anyway).
 
1 members found this post helpful.
Old 11-23-2011, 11:43 PM   #6
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ted_chou12 View Post
Code:
find "/sda1/ -type d -mtime -7 -name "/sda1/*/"
The correct form (including the depth limiting options, which much precede other options) is
Code:
find /sda1 -mindepth 1 -maxdepth 1 -type d -mtime -7
The depth of command line arguments is zero. The -mindepth 1 option means the /sda1 directory itself is never counted, even if it has been modified during the last week.

If you only need the number of matches (using Bash or a POSIX shell), use
Code:
changed=$(find /sda1 -mindepth 1 -maxdepth 1 -type d -mtime -7 -printf . | wc -c)
If you need the results in an array in a Bash script, use
Code:
changed=()
while read -rd "" file ; do
    changed=("${changed[@]}" "$file")
done < <( find /sda1 -mindepth 1 -maxdepth 1 -type d -mtime -7 -print0 )
and the full path to each changed directory (as separate parameters) is "${changed[@]}", the first changed directory is "${changed[0]}" and the number of changed directories is ${#changed[@]} . This while read -construct is one of the very rare ones that can support all possible file and directory names in Linux, including those that contain newlines and other exotic characters.

If you want to additionally limit the search to specific name patterns, use -name 'pattern' where pattern is a glob pattern (your normal filename patterns, like *.png) that matches only the directory name, not the path. In your case, you could use
Code:
find /sda1 -mindepth 1 -maxdepth 1 -type d -mtime -7 -name '[a-z][a-z]'
to find the immediate subdirectories of /sda1 changed in the last week and named using two lowercase letters.
 
1 members found this post helpful.
Old 11-24-2011, 12:44 AM   #7
ted_chou12
Member
 
Registered: Aug 2010
Location: Zhongli, Taoyuan
Distribution: slackware, windows, debian (armv4l GNU/Linux)
Posts: 431

Original Poster
Blog Entries: 32

Rep: Reputation: 3
Thanks,
Ted
 
  


Reply



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
Copy Directories By Modified Date lbargers Linux - General 3 09-03-2010 10:30 AM
Show files not directories than have been modified within the current day (NEWTHR) jianelisj Linux - Newbie 2 03-18-2008 04:58 PM
Show all files not directories than have been modified within the current day jianelisj Linux - Newbie 2 03-08-2008 04:54 PM
Are Active Directories modified by ISA Server ? VicRic General 0 03-20-2007 08:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:07 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
Open Source Consulting | Domain Registration