Umesh Patil
Umesh Patil

Reputation: 10705

List all files older than x days only in current directory

I am new to unix and couldn't get appropriate outcome in other questions.

I want to list only files in current directory which are older than x days. I have below restriction

I used find . -mtime +30. but this gives files and files in sub-directories as well. I would like to restrict doing search recursively and not to search inside directories.

Thanks a lot in advance !

Upvotes: 24

Views: 80261

Answers (4)

Lefty G Balogh
Lefty G Balogh

Reputation: 1898

A slightly different spin on this: find is incredibly versatile, you can specify size and time as follows:

This finds you all the logs that are 4 months or older and bigger than 1 meg. If you remove the + sign, it finds files that are roughly that size.

find /var/log -type f -mtime +120 -size +1M
/var/log/anaconda/journal.log
/var/log/ambari-agent/ambari-alerts.log.23
/var/log/ambari-agent/ambari-alerts.log.22
/var/log/ambari-agent/ambari-alerts.log.24
/var/log/ambari-agent/ambari-alerts.log.25
/var/log/ambari-agent/ambari-alerts.log.21
/var/log/ambari-agent/ambari-alerts.log.20
/var/log/ambari-agent/ambari-alerts.log.19

What's even better, you can feed this into an ls:

find /var/log -type f -mtime +120 -size +1M -print0 | xargs -0 ls -lh
-rw-r--r--. 1 root root 9.6M Oct  1 13:24 /var/log/ambari-agent/ambari-alerts.log.19
-rw-r--r--. 1 root root 9.6M Sep 27 07:44 /var/log/ambari-agent/ambari-alerts.log.20
-rw-r--r--. 1 root root 9.6M Sep 22 03:32 /var/log/ambari-agent/ambari-alerts.log.21
-rw-r--r--. 1 root root 9.6M Sep 16 23:23 /var/log/ambari-agent/ambari-alerts.log.22
-rw-r--r--. 1 root root 9.6M Sep 11 19:12 /var/log/ambari-agent/ambari-alerts.log.23
-rw-r--r--. 1 root root 9.6M Sep  6 15:02 /var/log/ambari-agent/ambari-alerts.log.24
-rw-r--r--. 1 root root 9.6M Sep  1 10:51 /var/log/ambari-agent/ambari-alerts.log.25
-rw-------. 1 root root 1.8M Mar 11  2019 /var/log/anaconda/journal.log

Upvotes: 2

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40061

You can use find . -maxdepth 1 to exclude subdirectories.

Upvotes: 8

A1rPun
A1rPun

Reputation: 16857

To add on @Richasantos's answer:

This works perfectly fine

$ find . -maxdepth 1 -type f -mtime +30

Prints:

./file1
./file2
./file3

You can now pipe this to anything you want. Let's say you want to remove all those old files:

$ find . -maxdepth 1 -type f -mtime +30 -print | xargs /bin/rm -f

From man find: ``

If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the -print0 option instead of -print.

So using -print0

$ find . -maxdepth 1 -type f -mtime +30 -print0

Prints (with null characters in between):

./file1./file2./file3

And is used like this to remove those old files:

$ find . -maxdepth 1 -type f -mtime +30 -print0 | xargs -0 /bin/rm -f

Upvotes: 17

Richasantos
Richasantos

Reputation: 626

You can do this:

find ./ -maxdepth 1 -type f -mtime +30 -print

If having problems, do:

find ./ -depth 1 -type f -mtime +30 -print

Upvotes: 28

Related Questions