Linux operating systems are widely used for web servers and other data-heavy tasks such as penetration testing etc. In these applications, there can be an accumulation of files that are not useful after a certain period. Now Linux, being an operating system used mostly for its automation functionalities, provides many ways to remove files older than a particular number of days automatically. In this article, we shall see ways to remove files older than x days automatically.
Pre-requisites
Before proceeding with the tutorial, make sure that you fulfill the following requirements:
- Linux Machine.
- Knowledge of Linux Terminals.
- Understanding of cronjobs.
Use the Find command with xargs to delete files older than x days
Step 1: Using the find command
Now, to get files files based on certain criteria, we have the find command in Linux. The find command searches for a file(s) in a directory hierarchy. This means that if you are searching for a file say file1.sh in a folder say my_folder then, the find command will search for this file in the directory my_folder and all the sub-folders present in that directory.
Syntax:
find <search directory> <search criteria>
Here, the first argument is for the search directory. If left empty then, find searches in the current working directory. The second argument decides the search criteria and has many different options, which we will use in the later steps.
Step 2: Getting files that are older than x days
Now, to find files that are older than x days, meaning they were last modified x days ago, we will use the find command. The syntax to do the same is:
Syntax:
find <search directory> -type f -mtime +x
- find: The first option for the find command is the path to the search directory, if no path is passed it works on the present working directory.
- -type: The next component is the -type option. This option specifies what kind of object we are searching for. For example, f is used for all general file types such as text files, scripts, etc. The d type searches for directories only. Here we are looking for files only so, we use the f option with -type.
- mtime: The last component is the -mtime component which takes input the number of days, the searched object was last modified. In this case, we looking for files older than x days so, we passed +x, where the + suffix indicates that the time could be x or more but, not less.
Let us see the same in action. For illustration purposes, we will take files modified five days ago or older. We are using the following directory structure for this example:
-/
-home
-my_folder
-file1
-file2
-file3
Here, we are in the /home folder, inside which we create a dummy folder my_folder containing 3 files last modified 5 days ago. We shall now try to use the find command to search for these files.
The command is the same as explained above. We have just passed the search directory and number of days. The output of this command is:
find /home/my_folder -type f -mtime +5
Output:
Output of find commandNow, as we can see the files are found.
Step 3: Changing the output to argument form
Before we can move further and delete these files, we have a slight syntactical problem. The rm command that removes files takes input in argument form i.e., separated by a blank space but, in the output of the find command, we got file names separated by a newline character, which is not supported by rm. Therefore, we need to change the list of file names into argument form.
Fortunately, there is a simple command, the xargs command, that converts lines of text from stdin(standard input) into an argument list.
Syntax:
xargs <command>
The command option takes the command that needs to be executed with the positional arguments given by xargs. First, let us see how xargs changes the output of our previous step.
find /home/my_folder/ -type f -mtime +5 | xargs
Output:
Output of find command in argument form
Here, we passed the output of the find command to the xargs command using the pipe command (|).
Step 4: Removing the files with rm
Now that we have got the files that we need to remove, that too in arguments form, all that remains is to remove these files. This can be achieved by simply passing the rm command to the xargs command in the previous step.
Note: You must be very cautious when removing files using the rm and with sudo privileges. One wrong execution can permanently delete some system files which will ruin your OS. Due to this very reason, all the tasks in this article are performed in a common non / folder.
This will delete all the files in the my_folder directory, which are older than 5 days.
find /home/my_folder -type f -mtime +5 | xargs rm
Removing files older than 5 days and verifying the same with ls
You can also verify whether the files have been deleted or not using the ls command.
Using the 'find' command with the -exec option
In the previous method, we had to use an external command, the xargs command to change the output of the find command in a pipable form to the rm command. However, there exists another way to run the rm command with the output of the find command without using the xargs command; using the -exec option of the find command.
The -exec option allows the user to run a command with the output of the find command. The syntax is as follows:
Syntax:
find <other options> -exec <command> {} \;
Here,
- <command>: The <command> placeholder takes the command to be executed.
- :{}: The {} placeholder specifies the path files found by the find command.
- \: The \ indicates the end of the -exec option.
- ;: The; is required to indicate the end of the command as the terminal by default takes the \ as a newline character.
To use this to find and delete files older than x days (5 days in this example) we can change the command from method 1 to the following:
find /home/my_folder -type f -mtime +5 -exec rm {} \;
Output:
Removing files with the -exec option in the find command.As we can see the files that were older than 5 days were removed from the specified folder.
Using the 'find' command with -delete option
There is another way to use the find command for deleting files older than x days, by using the inbuilt -delete option. This option deletes the files found by the find command. This is the most effective way to use the find command for deleting files as it uses a single command rather than multiple commands. The syntax of the same is given below.
Syntax:
find <other options> -delete
You only have to add the -delete option at the end of your find command and it will delete the files found based on the criteria given in the command. In our example, we can modify the find command as follows to delete files older than 5 days:
find /home/my_folder -type f -mtime +5 -delete
Output:
Deleting files using the -delete option in the find commandScheduling the auto-deletion task with cronjob
Now that we have learned how to remove files older than x days. We will now set up a cronjob to execute the command daily to make the task automatic, which is the objective of this article. In case you are not familiar with cronjobs, refer to this article. We shall not explain them in detail here as it is not in this article's scope.
The cronjobs required to automate the above methods to run daily are as follows:
Example 1:
0 0 * * * find /home/my_folder -type f -mtime +5 | xargs rm
Example 2:
0 0 * * * find /home/my_folder -type f -mtime +5 -exec rm {} \;
Example 3:
0 0 * * * find /home/my_folder -type f -mtime +5 -delete
Here, the first two arguments '0 0' refers to the minute and hour of the day. We have set it to midnight but, you can choose any time of your choosing. The next three arguments ' * * * ' represent day-of-month, the month of the year, and day-of-week respectively. We have set them to * which means on every occurrence of these times. Then, we finally give the command we want to execute.
Now, you only need any one of the methods to complete the deletion task. You can choose any of your liking and then, to make its cronjob work, you need to append this to our Linux machine's crontable which can be accessed by following the command:
crontab -e
If you are using crontab for the first time, you will be asked to choose your preferred editor from your installed editors. Once the selection is done, you will have your crontable file as follows:
Output:
Crontable
Now, all that is left is to add the cronjob created above at the end of this file without a # suffix. After that, you just save the changes and exit the editor.
Now, you have successfully set up automatic removal of files older than x days. A thing to note here is that we have specified the folder here, from which we need to remove x days older files.
Note: One must never set up a cronjob for a current folder of crontable or any other system file folder unless they are aware of what they are doing otherwise, they may end up corrupting their system.
Conclusion
In this article, we learned how to use the find command to search for files older than x days in a specified folder and then, we learned 4 three different methods to use the find command to remove those files. We used the following methods:
- find command with xargs and rm command.
- find command with -exec option to remove files with the rm command.
- find command with -delete option to delete files without any other command.
- Scheduling the auto-deletion task with cronjob
Similar Reads
How to Automatically Delete Old Files in Windows 11?
Managing storage space efficiently is crucial for maintaining a smooth and responsive system. Windows 11 offers built-in features to help you automatically delete old files, freeing up valuable disk space without manual intervention. Whether you're looking to manage temporary files, downloads, or ot
6 min read
How to Recover a Deleted File in Linux?
We all have often faced a problem where we have accidentally deleted some files in Linux, that we regretted deleting later on after we did not even find it in the trash. But what if we can recover them? Here, we will discuss How we can recover a Deleted File in Linux.Whenever we delete something fro
4 min read
Delete files older than N days in Python
In this article, we will learn to delete files that are older than a given number of days using Python. This work can be done manually but it is difficult to do when files are more in number, so we will use Python to make this task simple. We will learn three ways to do this task using Python: Delet
5 min read
How to Find and Remove Files Modified or accessed N days ago in Linux
Searching for the files which were modified (or accessed) some days ago is a common operation that is performed by Archival or Backup applications. This process is a trivial one for an Operating System that stores all the Metadata about the files in a File Table. Therefore, there exist commands offe
3 min read
How to Find and Remove Duplicate Files on Linux?
Most of us have a habit of downloading many types of stuff (songs, files, etc) from the internet and that is why we may often find we have downloaded the same mp3 files, PDF files, and other extensions. Your disk spaces are unnecessarily wasted by Duplicate files and if you want the same files on a
4 min read
How to Delete All Files Before a Certain Date in Linux
Linux, an advanced and open-source operating system, has several file-management commands and utilities. Deleting files in Linux is an essential skill for all users, whether beginner or experienced. In this article, we will learn how to delete all files before a certain date in Linux. In Linux, you
5 min read
How to Delete Files in Linux?
Linux comes with several tools that can assist us in removing Directories and files. We always need to delete many files and folders based on a set of requirements. To complete our mission quickly, knowing a few basic commands and their variations is beneficial. Use caution when using the commands b
6 min read
How to Find and Remove Duplicate Files in Linux Using âFSlintâ Tool
Fslint is a general-purpose Linux utility widely used with the purpose to remove duplicate, unwanted, and lint files from the system. The collection of unwanted and unnecessary files is called lint files. Fslint is an open-source tool written in python and shell. Let's install Fslint on our system :
2 min read
How to Create a File in the Linux Using the Terminal?
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
How to Compress Files in Linux | Tar Command
File compression is a fundamental task in managing and transferring data efficiently on a Linux system. The Tar command, short for Tape Archive, is a powerful tool that allows users to create compressed and archived files. In this comprehensive guide, we will explore the various options and examples
11 min read