x32x01
  • by x32x01 ||
It is a best practice to find and remove old or unused files from the Linux system after a certain period of time, as this will free up some space on the system, which can be used for some other purpose.

Please make sure these files are no longer needed, as it will not ask for your confirmation before deleting the files.
How to Delete files older than 30 Days in Linux
Are you wondering how to delete older files in your Linux system? This quick guide show you how to find and remove files older than 30 days in Linux.

To delete files older than 30 days on your Linux server, you need to use multiple commands together such as find command, ‘mtime’ command, ‘exec’ command and rm command.

What’s mtime command
mtime (File Last Modify Time) – mtime shows when you modify, append or update a file content. The time argument consider an input value as ’24 hours’. For example, time 2 = 2*24 hours (2 days).

1) Search and Delete files older than 30 days
First, we will find out all files older than 30 days under the ‘/home/linuxgeek/Downloads’ directory.
The below output will allow you to check whether these files are needed before they are deleted. If not, delete them using the below command.
Bash:
You Can, Log in or Register To View Codes Content !
Once you have decided that these files are no more requires then go ahead and delete it, using one of the following command.
Bash:
You Can, Log in or Register To View Codes Content !

Details:
  • find: find is a command
  • /home/linuxgeek/Downloads: Path to files (It should be replaced by yours)
  • -type f: What Type of files
  • -mtime +30: It filters 30 days old files.
  • -exec rm -f Perform a file remove action
  • {}: Represents the file found by Find command.
  • \; It will end the command.
2) Delete files older than ‘X’ days with Wildcard option
In many cases, you may have to delete files based on the name when there are many log files with different names in the log directory, I usually use this command to delete files older than 30 days on the system.

It is always recommended to do a dry run instead of removing directly. This will give you the time to check before deleting the files.
Code:
You Can, Log in or Register To View Codes Content !
Once you verified the list, select one of the following commands to remove it.
Bash:
You Can, Log in or Register To View Codes Content !

3) Delete files older than 10 days with Specific Extension
Sometimes you may need to delete files based on file extension instead of deleting them. For instance, to delete files with “.log” extension and modified within 10 days, run:

First, let’s find and print a list of log files older than 10 days under the ‘/var/log’ directory:
Bash:
You Can, Log in or Register To View Codes Content !
Check the list and delete it using one of the following command if it is no longer needed.
Bash:
You Can, Log in or Register To View Codes Content !
Final Thoughts
In this guide, we’ve shown you how to delete files older than 30 days on Linux with three different examples. It is not limited to 30 days and days can be changed as needed like 10 days or 90 days or even 120 days.

If you have any questions or feedback, feel free to comment below.
 

Similar Threads

x32x01
Replies
0
Views
49
x32x01
x32x01
x32x01
Replies
0
Views
173
x32x01
x32x01
x32x01
Replies
0
Views
120
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
100
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
87
x32x01
x32x01
TAGs: Tags
delete files linux

Register & Login Faster

Forgot your password?

Latest Resources

Forum Statistics

Threads
507
Messages
508
Members
42
Latest Member
Mustafa123
Back
Top