Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • VirtualBox
  • VMware
  • Windows
  • 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
  • VirtualBox
  • VMware
  • Windows
  • 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

Ftr38

[WinForms] Creating GUIs in Windows PowerShell with WinForms

November 15, 2023
Ftr21

Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format

October 21, 2023
Ftr21

Translate Microsoft 365 License GUIDs to Product Names in PowerShell Microsoft Graph

October 19, 2023
Ftr21

How to Get an Access Token for Microsoft Graph PowerShell / API

November 27, 2023
Ftr21

Getting Access Token for Microsoft Graph

November 27, 2023
Ftr5

How to Copy Files without Changing Date Creation Time on Windows

November 27, 2023

Leave a Reply Cancel reply

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

Recent Posts

  • [WinForms] Creating GUIs in Windows PowerShell with WinForms
  • Converting DateTime Obtained from Microsoft Graph Call to PowerShell Date and Time Format
  • Translate Microsoft 365 License GUIDs to Product Names in PowerShell Microsoft Graph

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 2023 © 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