[SOLVED] find to get the number of directories modified within last week
Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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
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:
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).
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.