1

How to use find command to get only files from the current directory (excluding subdirectories and its files) older than a day on AIX?

terdon
  • 242,166
Anony
  • 149
  • 4
    Please always mention your operating system! You already wasted your time on Ask Ubuntu because you didn't tell us you were using AIX and now you have reposted here (which is fine, AIX is welcome here) and you still didn't tell us you're using AIX! I had to edit it in. The basic tools like 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. – terdon Mar 18 '19 at 13:13

1 Answers1

10

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:

  1. If the current thing find is looking at is not . (! -name .), the current directory, then
  2. Prune that path from the search path of find (-prune). This stops find from even considering entering any subdirectories.
  3. If the thing is a regular file (-type f), and
  4. If its modification timestamp is older than 24 hours (-mtime +0), then
  5. 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:

Kusalananda
  • 333,661