How to use find command to get only files from the current directory (excluding subdirectories and its files) older than a day on AIX?
1 Answer
Assuming that the implementation of find
on your Unix has the -maxdepth
predicate (it's non-standard, but often available), the following find
command would do that:
find . -maxdepth 1 -type f -mtime +0
This would print the names of the regular files (i.e. not directories, sockets, named pipes etc.) whose modification timestamp was more than 24 hours ago to the terminal.
If -maxdepth
can't be used, then consider
find . ! -name . -prune -type f -mtime +0 -print
This means:
- If the current thing
find
is looking at is not.
(! -name .
), the current directory, then - Prune that path from the search path of
find
(-prune
). This stopsfind
from even considering entering any subdirectories. - If the thing is a regular file (
-type f
), and - If its modification timestamp is older than 24 hours (
-mtime +0
), then - Print the name of the thing we've found (
-print
).
Regarding -mtime +0
: On your AIX system, see the note regarding the semantics of -mtime
to determine whether to use -mtime +0
or -mtime +1
. If you want the UNIX03 behaviour, you should be using -mtime +0
to find files with a modification timestamp of more than 24 hours ago (and set the environment variables XPG_SUS_ENV
to ON
and XPG_UNIX98
to OFF
, in accordance with that note in the manual).
By default, find
outputs the pathnames of the found files. This means that you'll get filenames prepended by ./
from the above command. If you don't want that, then you will have to call the basename
utility for each found filename.
You can do that like this:
find . ! -name . -prune -type f -mtime +0 -exec basename {} \;
In comments you mention that you'd like to compress these files. For this, there is no need to call the basename
utility. Just call gzip
(or whatever compression utility you'd like to use) instead:
find . ! -name . -prune -type f -mtime +0 -exec gzip {} +
Depending on whether you'd like to do something further with these files, you may look for similar question on this site, e.g. Why is looping over find's output bad practice?
Also related:
-
2Comments are not for extended discussion; this conversation has been moved to chat.– terdon ♦Mar 18, 2019 at 13:14
-
-
@Anony Not in any way that would help I think. Also, this comment section was archived in a chat. Do consider using it. Also note that I've asked multiple times for the actual command that you are using. I can't debug anything without seeing it. Do not post further comments in this thread, but do feel welcome to use the chat.– Kusalananda ♦Mar 18, 2019 at 13:41
-
1Shouldn’t it be
-mtime +0
? See Why does find -mtime +1 only return files older than 2 days?– MelebiusMar 19, 2019 at 7:29 -
@StéphaneChazelas Sorted. Though, the
find
on OpenBSD seems to have different semantics for-mtime
and you would have to use+1
there rather than+0
.– Kusalananda ♦Mar 19, 2019 at 10:35
find
vary considerably between the different implementations, so you always need to tell us what OS you are using. The shell you are using, ksh in your case, is irrelevant.