How to create a Cronjob that keeps only X recent files 

How to create a Cronjob that keeps only X recent files 

This cronjob command has made a lot of the things easier to do. If you are a DevOps Engineer or a computer programmer who uses Ubuntu OS regularly might be aware of a tool called cronjob. So in this ...

If you are a DevOps Engineer or a computer programmer who uses Ubuntu OS regularly might be aware of a tool called cronjob. So in this article, I’ll be giving you a one handy command which I recently managed to learn during my DevOps training.

First of all, for everyone who is a bit new to this tool let me guide you through some basics first.

Cronjob: it creates jobs that a user can run on a scheduled time on automation which makes their tasks easier to do. There is a cron “daemon” that runs on these systems.

A daemon is a program that runs in the background all the time, usually initiated by the system. This cron daemon is responsible for launching these cron jobs on schedule. Here is the basic syntax ;

10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1

You can refer to crontab guru which can guide you on how to specify time and days for your job easily through their wonderful and smart gui.

What is the use of Cronjob?

  • You can schedule long-running tasks to run from a command-line script, rather than running it from a web script. Like encoding videos, or sending out mass e-mails.
  • You can execute daily tasks to delete logs and files that creates on a regular basis in the system.
  • It can be used to remove caches files and can also be used to perform simple tasks like fetching data from social media or auto-check for websites

Possibilities are infinite.

Now coming to the main topic, what we’re gonna do is to generate a job which will keep x amount of recent files in your specified directory and will delete the rest of them

0 2 * * 0-6 cd /home/username/dir && ls -tr | head -n -2 | xargs --no-run-if-empty rm

Okay. So the above-mentioned command will find the recent 2 files and delete the rest of the files *at 02:00 on every day-of-week from Sunday through Saturday. *(0–6 is a defined range for the days of the week )

So here is the command, if you wanna try it then create a demo directory and demo files before putting this command onto the live project.

programming ubuntu cronjob tutorial coding

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.

How to Structure Your Programming Code

I remember my first fumble with basic on my ZX Spectrum computer back in the 1980s, ploughing through pages of basic commands and example code without any real idea of how I could write programs myself

What Is Code Golfing And Biggest Such Tournaments

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that implements a certain algorithm. Playing code golf is known as "golf scripting". Code golf challenges and tournaments may also be named with the ... Some code golf questions, such as those posed on general programming ...

Coding 101: Programming Language Building Blocks

This article will introduce the concepts and topics common to all programming languages, that beginners and experts must know!

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.