How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (2024)

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:

  1. Linux Machine.
  2. Knowledge of Linux Terminals.
  3. 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:

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (1)

Output of find command

Now, 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:

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

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (3)

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:

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (4)

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:

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (5)

Deleting files using the -delete option in the find command

Scheduling 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:

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (6)

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:

  1. find command with xargs and rm command.
  2. find command with -exec option to remove files with the rm command.
  3. find command with -delete option to delete files without any other command.
  4. Scheduling the auto-deletion task with cronjob


O

owl0223

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (7)

Improve

Next Article

How to Automatically Delete Old Files in Windows 11?

Please Login to comment...

How to automatically delete/remove files older than x days in Linux - GeeksforGeeks (2024)
Top Articles
How To Get Shiny Lucario & Master Ball For Free in Pokémon Scarlet & Violet
Squarespace Help
Encore Atlanta Cheer Competition
Best Pizza Novato
Algebra Calculator Mathway
FFXIV Immortal Flames Hunting Log Guide
Robinhood Turbotax Discount 2023
Phenix Food Locker Weekly Ad
Snarky Tea Net Worth 2022
Revitalising marine ecosystems: D-Shape’s innovative 3D-printed reef restoration solution - StartmeupHK
4156303136
Sand Castle Parents Guide
Michael Shaara Books In Order - Books In Order
Mikayla Campinos Laek: The Rising Star Of Social Media
Music Go Round Music Store
Kirksey's Mortuary - Birmingham - Alabama - Funeral Homes | Tribute Archive
The Blind Showtimes Near Amc Merchants Crossing 16
Euro Style Scrub Caps
Clare Briggs Guzman
Canvasdiscount Black Friday Deals
Gran Turismo Showtimes Near Marcus Renaissance Cinema
Brbl Barber Shop
Hellraiser 3 Parents Guide
Gilchrist Verband - Lumedis - Ihre Schulterspezialisten
The 15 Best Sites to Watch Movies for Free (Legally!)
2023 Ford Bronco Raptor for sale - Dallas, TX - craigslist
Downtown Dispensary Promo Code
Ups Drop Off Newton Ks
Chicago Pd Rotten Tomatoes
Lil Durk's Brother DThang Killed in Harvey, Illinois, ME Confirms
Murphy Funeral Home & Florist Inc. Obituaries
Foolproof Module 6 Test Answers
American Bully Xxl Black Panther
Nearest Ups Office To Me
20 bank M&A deals with the largest target asset volume in 2023
Weather Underground Cedar Rapids
Homeloanserv Account Login
Pa Legion Baseball
Foxxequeen
Elven Steel Ore Sun Haven
Tacos Diego Hugoton Ks
Dyi Urban Dictionary
Petfinder Quiz
Booknet.com Contract Marriage 2
Unblocked Games 6X Snow Rider
Automatic Vehicle Accident Detection and Messageing System – IJERT
Smoke From Street Outlaws Net Worth
Fresno Craglist
2487872771
Jigidi Jigsaw Puzzles Free
Phumikhmer 2022
Famous Dave's BBQ Catering, BBQ Catering Packages, Handcrafted Catering, Famous Dave's | Famous Dave's BBQ Restaurant
Latest Posts
Article information

Author: Twana Towne Ret

Last Updated:

Views: 6179

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Twana Towne Ret

Birthday: 1994-03-19

Address: Apt. 990 97439 Corwin Motorway, Port Eliseoburgh, NM 99144-2618

Phone: +5958753152963

Job: National Specialist

Hobby: Kayaking, Photography, Skydiving, Embroidery, Leather crafting, Orienteering, Cooking

Introduction: My name is Twana Towne Ret, I am a famous, talented, joyous, perfect, powerful, inquisitive, lovely person who loves writing and wants to share my knowledge and understanding with you.