search
HomeBackend DevelopmentPHP TutorialLinux scheduled file cleaning script shell

Everything that is often used in work is collected from the Internet. Record it for easy search

Statement writing:

find corresponding directory -mtime + number of days -name "file name" -exec rm -rf {} ;

Example 1:

find /usr/local/backups -mtime +10 -name “*.*” -exec rm -rf {} ;

will find all files with “.” in the /usr/local/backups directory 10 days ago. File deletion

find: Linux search command, users search for files with specified conditions

/usr/local/backups: Any directory you want to clean up

-mtime: Standard statement writing

+10: Find files 10 days ago Files, the number here represents the number of days, +30 means searching for files 30 days ago

"*.*": the data type you want to find, "*.jpg" means searching for all files with the extension jpg, "*" means searching All files, this can be used flexibly, draw inferences from one example

-exec: Fixed writing method

rm -rf: Forced deletion of files, including directories

{};: Fixed writing method, a pair of braces + spaces++;

Example 2 :

1.#touch /usr/local/bin/clear

#chmod 777 clear

Create a new executable file clear

2.vi clear

Edit the clear file as follows:

#!/bin/sh

find /usr/local/backups -mtime +10 -name “*.*” -exec rm -rf {} ;

ok, save and exit
3.#crontab -e

Add the clear file to the system plan The task will be executed automatically when the time comes.

Input:

* 2 * * */usr/local/bin/clear

The setting here is to execute the clear file at 2 am every day for data cleaning. You can study cron and formulate your own needs Scheduled task

Example:

#!/bin/sh

find /usr/local/jboss-4.2.3.GA/server/default/log -mtime +6 -name “server.log.*” - exec rm -rf {} ;

exit

[root@web3 ~]# crontab -l

Edit the user's Crontab file

crontabl -e

The Crontab file created by the user is stored in /var/spool/cron , the file name is consistent with the user name.
The format is divided into six sections. The first five sections are the time setting sections, and the sixth section is the command section to be executed.
The format is as follows: * * * * *
The meaning of the time sections is as shown in Table 2:

Paragraph

meaning

value range

The first paragraph

represents minutes

0—59

The second paragraph

represents hours

0—23

The third paragraph

represents date

1— 31

The fourth paragraph

represents the month

1—12

The fifth paragraph

represents the day of the week, 0 represents Sunday

0—6

Name: crontab
Permissions: All users
Usage:
crontab [ -u user ] file
crontab [ -u user ] { -l | -r | -e }
Explanation:
crontab is used to allow users to execute programs at a fixed time or fixed interval. In other words , which is similar to the user's schedule. -u user refers to setting the schedule of the specified user. The premise is that you must have its permissions (for example, root) to specify other people's schedules. If -u user is not used, it means setting your own schedule.
Number of meals:
-e: Execute a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable to specify which text to use. Editor (such as setenv VISUAL joe)
-r: Delete the current schedule
-l: List the current schedule
The format of the schedule is as follows:
f1 f2 f3 f4 f5 program

where f1 is represents minutes, f2 represents hours, f3 represents the day of a month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed.
When f1 is *, it means the program will be executed every minute, when f2 is *, it means the program will be executed every hour, and so on
When f1 is a-b, it means the program will be executed from minute a to minute b , when f2 is a-b, it means that it will be executed from the a-th hour to the b-th hour, and so on. When f1 is */n, it means that it will be executed every n-minute time interval. When f2 is */n, it means every n-hour time interval. Execute once, and so on. When f1 is a, b, c,..., it means that the a, b, c,... minute will be executed. When f2 is a, b, c,..., it means the a, b, c... minute. Hourly execution, and so on. Users can also store all settings in the file first, and use crontab file to set the schedule.
Example:
Execute /bin/ls at the 0th minute of every hour every day of the month:
0 7 * * * /bin/ls

In 12 months, every day from 6 am to 12 am, every 20 Execute /usr/bin/backup once every minute:
0 6-12/3 * 12 * /usr/bin/backup

Send a letter to alex@domain.name at 5:00 pm every day from Monday to Friday:
0 17 * * 1-5 mail -s “hi” alex@domain.name /dev/null 2>&1

Example: If the content of the user’s Crontab file is: 29 19 * * * echo its dinner time, the system Display 'its dinner time' at 19:29 every day

Example (create the entire process of cron, and enter the current time in test.txt every minute):

1. Log in to the Linux system as an ordinary user (I am using CentOS4.1)

2. $crontab –e
Note: The default editor of the system is VIM. If not, please add the following shell:
$EDITOR=vi
$export EDITOR

3.                     Enter “*/1 * * * * date >> $HOME/test.txt”, save and exit VIM

4.               $su root

5.           $cd /etc/ init.d

6. ./crond restart

Let’s take a look at a few specific examples:

● 0 */2 * * * /sbin/service httpd restart means restarting apache every two hours

● 50 7 * * * /sbin/service sshd start means to start the ssh service at 7:50 every day

● 50 22 * ​​* * /sbin/service sshd stop means to close the ssh service at 22:50 every day

● 0 0 1 ,15 * * fsck /home Check the /home disk on the 1st and 15th of every month

● 1 * * * * /home/bruce/backup Execute the /home/bruce/backup file at the first minute of every hour

● 00 03 * * 1-5 find /home “*.xxx” -mtime +4 -exec rm {} ; Every Monday to Friday at 3 o’clock, search for the file named *.xxx in the directory /home. And delete files older than 4 days.
● 30 6 */10 * * ls means that the ls command is executed once every month on the 1st, 11th, 21st, and 31st at 6:30


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use