Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

How To Find and Delete Files Older Than X Days in Linux

October 23, 2022
in Blog, Linux
0
ADVERTISEMENT

Table of Contents

It’s always recommended to find and cleanup your old files which are no longer necessary after a certain period of time. 

This will save you some disk space. This brief tutorial walks you through how to find and delete files older than X days in Linux and Unix-like operating systems.

Note You should be extremely careful while running the following commands. These commands will not ask you any confirmation before deleting the files. It will simply delete the files once you hit the key. So be very careful and double check the files you're about to delete.

Find Files Older Than X Days In Linux

First, let’s find out the files older than X days, for example 30 days.

The below command will find and display the older files which are older than 30 days in the current working directory.

find . -mtime +30 -print
  • dot (.) – Represents the current directory.
  • -mtime – Represents the file modification time and is used to find files older than 30 days.
  • -print – Displays the older files

The files older than 30 days in the current directory.

[root@kms log]# find . -mtime +30 -print
./tallylog
./grubby_prune_debug
./samba
./samba/old
./secure-20220828
./tuned
./tuned/tuned.log
./audit
./audit/audit.log.2
./audit/audit.log.1
./audit/audit.log

If you want to search files in a specific directory, just replace the dot with the folder path. For example, to find out the files which are older than 30 days in nginx log directory /var/log/nginx.

find /var/log/nginx -mtime +30 -print
[root@kms log]# find /var/log/nginx -mtime +30 -print
/var/log/nginx/error.log-20220908.gz
/var/log/nginx/error.log-20220909.gz
/var/log/nginx/error.log-20220911.gz
/var/log/nginx/access.log-20220912.gz
/var/log/nginx/error.log-20220916.gz
/var/log/nginx/access.log-20220909.gz
/var/log/nginx/access.log-20220911.gz
/var/log/nginx/error.log-20220912.gz

Delete Files Older Than X Days In Linux

Now, run the following command to delete the files which are not required anymore. Again, I warn you that these commands will delete the files immediately once you hit button. Please be cautious and double check before running these commands.
find /var/log/nginx -type f -mtime +30 | xargs rm -f

Once done, when you run the command to find the files older than 30 days in nginx log directory. There’s nothing returned.

Bg2184

Alternatively, you can use one of below commands to find then delete files older than 30 days in a directory in Linux.

find /var/log/nginx -mtime +30 -exec rm -f {} \;
find /var/log/nginx -mtime +30 -delete;

Conclusion

Delete old files periodically if they are not necessary at regular intervals, or backup them to any external drives and free up disk space. You can use the free space for any other useful purposes.

ADVERTISEMENT
5/5 - (1 vote)
Previous Post

How to Delete Files Older Than X Days Automatically in Linux

Next Post

How to Get the Size of a Directory in Linux

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory