Linux 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 (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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

Gate.io is a highly acclaimed cryptocurrency trading platform known for its extensive token selection, low transaction fees and a user-friendly interface. With its advanced security features and excellent customer service, Gate.io provides traders with a reliable and convenient cryptocurrency trading environment. If you want to join Gate.io, please click the link provided to download the official registration installation package to start your cryptocurrency trading journey.

This tutorial guides you through installing and configuring Nginx and phpMyAdmin on an Ubuntu system, potentially alongside an existing Apache server. We'll cover setting up Nginx, resolving potential port conflicts with Apache, installing MariaDB (
