I have a problem, like I need to find the directories that got updated yesterday. I tried using find command but its listing all the files that got updated in the directories. But I need only the directory names.
Asked
Active
Viewed 1.7k times
7
Rui F Ribeiro
- 55,929
- 26
- 146
- 227
vamsi krishna
- 117
- 1
- 2
- 7
2 Answers
10
To find dirs containing files modified in the last 24 hours:
find [dir-to-search] -type f -mtime -1 -exec dirname {} \; | sort --unique
Change the mtime -1 to mtime -2 to search the last 48 hours, or change it to mmin -120 to search the last 2 hours
Edit: explanation:
Searches dir-to-search (or current dir if not provided) recursively for entries of type f (file) which were modified less than (1*24) hours ago. Execute the dirname command for each of these. This will give one dirname listing for each file, which may result in many duplicates, so pipe the output to sort and ask it to extract unique dirnames.
thomas_d_j
- 1,481
- 1
- 9
- 8
-
Sounds like this one should be the accepted answer, because of the shortcomings mentioned in the comment of https://unix.stackexchange.com/a/287117/249784 – Kutzi Oct 10 '19 at 12:11
-
what would be best way to invert this command (e.g. find directories which have not been modified in the last 24 hours)? – Arsen Zahray Oct 09 '20 at 11:53
7
You can use -type d in the find string:
find /path/to/target -type d -mtime 1
terdon
- 234,489
- 66
- 447
- 667
Kristian Kirilov
- 106
- 3
-
4That command will only find dirs with added, deleted or renamed contents, not dirs which had an existing file modified. Also be wary of `find`'s handling of things like mtime and size - it is not particularly intuitive. `find -mtime 1` actually finds files modified between 24.0 and 47.999 hours ago. Contrarily, `find -size 2G` finds files between 1.0 and 1.999 GiB. My favourite is `find -size -1G` which only finds zero-byte files. But I digress... – thomas_d_j Jun 02 '16 at 08:26