Remove and list files Older than X no of days

1. How to remove old file older than x no of days and which is not .gz file

create a old dated file using below commands.

touch -d 20150101 syslog.1

[root@unixplanetary tmp]# ls -ltr

-rw-r–r–. 1 root root 0 Jan  1  2015 syslog.1.gz

-rw-r–r–. 1 root root 0 Jan  1  2015 syslog.1

[root@unixplanetary tmp]= find /tmp -type f -mtime +20 |grep -v *.gz | xargs rm -rf;

[root@unixplanetary tmp]# ls -ltr

-rw-r–r–. 1 root root 0 Jan  1  2012 syslog.1.gz

[root@unixplanetary tmp]#

2. Remove more than 30 Days file.

find /tmp -mtime +30 -exec rm -rf {} \;

find /tmp -xdev -type f -mtime +30 -exec rm -rf {} \;

3. How to List the files older than 100 days

Unixplanetary:# find /var -mtime +100 |wc -l
56587

4. How to display the files older than 100 days .

#find /tmp -mtime +100 -print

5. List all file ending with .bz2 .

find / -type f -name “*.bz2”

6.  limit the find to the first level you can do:

# find / -maxdepth 1 -name ‘*.bz2’
/file1.bz2

7. List file Larger than 1 GB

find /tmp -xdev -size +1000000000c -exec ls -ltrh {} \;

8.Remove file larger than 1 GB from temp file system
find /tmp -xdev -size +1000000000c -exec rm -rf {} \;

Leave a Reply

Your email address will not be published. Required fields are marked *