find /path/to/files -type f -mtime 0 -delete
时间: 2024-05-17 12:12:53 浏览: 13
这个命令会删除 `path/to/files` 目录下今天修改或创建的所有文件。如果您想要删除所有类型的文件,这个命令是可以用的。但是请注意,这个命令会不可逆地删除文件,请谨慎操作,并且建议在执行前先使用 `find /path/to/files -type f -mtime 0` 命令查看一下要删除的文件列表。如果您只想要删除某种类型的文件,您可以使用 `find /path/to/files -type f -name "*.txt" -mtime 0 -delete` 命令,这将只删除 `path/to/files` 目录下今天修改或创建的所有扩展名为 `.txt` 的文件。
相关问题
I want to delete the files which are created earlier than a particular time in a directory. How can I do this in shell scripts?
In shell scripts, you can use the `find` command along with the `-mtime` option to identify files created earlier than a specific time. Here's an example script that deletes files older than a certain number of days:
```shell
#!/bin/bash
directory="/path/to/directory"
days=7
# Find files older than the specified number of days and delete them
find "$directory" -type f -mtime +"$days" -exec rm {} \;
```
In this script, replace `/path/to/directory` with the actual directory path you want to search in. The `days` variable represents the number of days ago from which you want to delete files. Adjust it according to your needs.
The `find` command searches for files (`-type f`) in the specified directory and its subdirectories, which were modified more than `$days` days ago (`-mtime +"$days"`). The `-exec` option is used to execute the `rm` command on each found file (`{}` represents the file path) to delete it.
Please exercise caution while running scripts that involve file deletion, as it can permanently remove data. It's recommended to test the script on a small set of files or take appropriate backup measures before using it in production.
linux删除今天生成的文件
您可以使用以下命令来删除今天生成的文件:
```
find /path/to/files -type f -mtime 0 -delete
```
这将删除 `/path/to/files` 目录下今天生成的所有文件。如果您想要删除指定文件类型的文件,您可以使用以下命令:
```
find /path/to/files -type f -name "*.txt" -mtime 0 -delete
```
这将删除 `/path/to/files` 目录下今天生成的所有 `.txt` 文件。请注意,这些命令将不可逆地删除文件,请谨慎操作。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)