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?

2
  • 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, 2019 at 13:13
  • Similar: delete folders older than 1 day Mar 19, 2019 at 7:53

1 Answer 1

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:

5
  • 2
    Comments are not for extended discussion; this conversation has been moved to chat.
    – terdon
    Mar 18, 2019 at 13:14
  • Is there any way using xargs to implement the same?
    – Anony
    Mar 18, 2019 at 13:37
  • @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
  • 1
    Shouldn’t it be -mtime +0? See Why does find -mtime +1 only return files older than 2 days?
    – Melebius
    Mar 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .