- Find directories with all files inside older than X?
- 5 Answers 5
- Find all files older than one minute
- 5 Answers 5
- Example
- The second — in -mtime -60s is not an option delimiter.
- Further reading
- How to find and delete files older than specific days in unix?
- 5 Answers 5
- Find the files that have been changed in last 24 hours
- 7 Answers 7
Find directories with all files inside older than X?
Is it possible on linux to find directories where all contained files and directories (and sub-directories’ files etc.) are older than a given age? In other words, if a directory has one or more files within that have a modification date more recent than a given threshold, that directory should not be listed. If all of the files and folders below that directory are older than the given threshold, then that directory should be listed. The use case is that my home directory is full of hidden directories, and I’m sure that many of them are left overs from previous installations, and software that I haven’t used in years. I’d like to be able to find these directories, so I can easily decide whether to cull them.
What do you mean by «older»: modification or access time? Note that the latter will be meaningless if the file system is mounted with relatime or noatime . Also a very old modification time doesn’t necessarily mean that a file is useless.
@cyrus modification time. I know it doesn’t mean it’s useless. As I said, I just want to use this as a tool to help me to decide what is useless..
5 Answers 5
It’s probably possible to do this without creating files using process substitution or something, but here’s a quick-and-dirty solution:
find . -type f -mtime +30 -printf '%h\n' | sort | uniq > old.txt find . -type f -mtime -30 -printf '%h\n' | sort | uniq > new.txt grep -vf new.txt old.txt
The first command outputs the path of every file modified more than 30 days ago (in find’s -printf — at least with the GNU find on my system — %h prints the whole path except for the actual filename), then sorts those and gets rid of any duplicates, and puts the whole thing into a file called old.txt .
The second command does the same but with every file modified less than 30 days ago, and puts them into another file, new.txt .
The grep line prints every line from old.txt that doesn’t appear in new.txt — so it will give you a list of directories that contain only files that were last modified more than 30 days ago.
This is all using the GNU versions of the utilities. I don’t know if the syntax matches up on the BSD versions, etc.
Find all files older than one minute
How can I use find to select files that have been written and not modified in the last minute? I know I can do it the other way around, find files modified in the last 60 seconds with -mtime -60s , but I want the ones that haven’t been modified in the last 60 seconds. I use Linux and get this error if I use seconds:
find ??/ -mtime +60s -name blah.tsv find: invalid argument `+60s' to `-mtime'
5 Answers 5
Use find /path -type f -mtime +60s
The — just before the digits is not a regular «argument dash», but means «less than». + then is «more than».
All primaries which take a numeric argument allow the number to be preceded by a plus sign («+») or a minus sign («-»). A preceding plus sign means «more than n», a preceding minus sign means «less than n» and neither means «exactly n».
It should be noted that for exactly n, the time is rounded. So 1 (1 day) does not mean 86400 seconds.
Strictly speaking, +60 is not the opposite of -60 , for the same reason less than is not the opposite of greater than: Both exclude the exact value they compare to. But your question does not indicate which behavior you want, exactly.
This isn’t exactly the same indeed, since ! reverts the original «query». But agreed, the OP doesn’t specify what he wants.
The day after, the OP stated it is on Linux, not Mac OS X. If that would have changed the answer, can you update it?
Example
$ ls * four.txt one.txt three.txt two.txt $ touch foo && find . -mmin +1 . ./three.txt ./four.txt ./two.txt ./one.txt
The second — in -mtime -60s is not an option delimiter.
-mtime is an option, and it is followed by an option argument. The option argument is -60s , and the — in it is part of the option argument itself, not an option delimiter. It means «less than 60 seconds». Option arguments 60s and +60s mean «equal to 60 seconds» and «greater than 60 seconds», respectively.
The Apple MacOS manual and the FreeBSD manual mention the + and — prefixes in exactly one place, and forget to explain anywhere what they are. This is what they are.
(The GNU Info manual for GNU find has the same omission, interestingly enough. However, GNU find ‘s syntax for times is somewhat different to the BSD and MacOS find syntax.)
Further reading
- Apple incorporated (2008-02-24). find MacOS 10 manual page. MacOS 10 Developer Library.
- find (1). 2010-03-17. FreeBSD General Commands Manual. FreeBSD Project.
How to find and delete files older than specific days in unix?
I have got one folder for log with 7 folders in it. Those seven folders too have subfolders in them and those subfolders have subfolders too. I want to delete all the files older than 15 days in all folders including subfolders without touching folder structrure, that means only files.
mahesh@inl00720:/var/dtpdev/tmp/ > ls A1 A2 A3 A4 A5 A6 A7 mahesh@inl00720:/var/dtpdev/tmp/A1/ > ls B1 B2 B3 B4 file1.txt file2.csv
5 Answers 5
You could start by saying find /var/dtpdev/tmp/ -type f -mtime +15 . This will find all files older than 15 days and print their names. Optionally, you can specify -print at the end of the command, but that is the default action. It is advisable to run the above command first, to see what files are selected.
After you verify that the find command is listing the files that you want to delete (and no others), you can add an «action» to delete the files. The typical actions to do this are:
- -exec rm -f <> \; (or, equivalently, -exec rm -f <> ‘;’ )
This will run rm -f on each file; e.g.,
rm -f /var/dtpdev/tmp/A1/B1; rm -f /var/dtpdev/tmp/A1/B2; rm -f /var/dtpdev/tmp/A1/B3; …
rm -f /var/dtpdev/tmp/A1/B1 /var/dtpdev/tmp/A1/B2 /var/dtpdev/tmp/A1/B3 …
So, if you use option 2, the whole command would be:
find /var/dtpdev/tmp/ -type f -mtime +15 -exec rm -f <> +
Find the files that have been changed in last 24 hours
E.g., a MySQL server is running on my Ubuntu machine. Some data has been changed during the last 24 hours. What (Linux) scripts can find the files that have been changed during the last 24 hours? Please list the file names, file sizes, and modified time.
7 Answers 7
To find all files modified in the last 24 hours (last full day) in a particular specific directory and its sub-directories:
find /directory_path -mtime -1 -ls
The — before 1 is important — it means anything changed one day or less ago. A + before 1 would instead mean anything changed at least one day ago, while having nothing before the 1 would have meant it was changed exacted one day ago, no more, no less.
The argument to -mtime is interpreted as the number of whole days in the age of the file. -mtime +n means strictly greater than, -mtime -n means strictly less than.
Another, more humanist way, is to use -newermt option which understands human-readable time units (see man find and search for -newerXY ).
Unlike -mtime option which requires the user to read find documentation to figure our what time units -mtime expects and then having the user to convert its time units into those, which is error-prone and plain user-unfriendly. -mtime was barely acceptable in 1980s, but in the 21st century -mtime has the convenience and safety of stone age tools.
Example uses of -newermt option with the same duration expressed in different human-friendly units:
find / -newermt "-24 hours" -ls find / -newermt "1 day ago" -ls find / -newermt "yesterday" -ls