liunx delete files based on time

1. Some sample commands using find and rm -rf commands:

  1. Find and delete files of a specified type.
    If you want to delete all files with a .log extension in the current directory, you can use the following command:
find . -type f -name "*.log" -exec rm {
    
    } \;

This command will find all files with a .log extension in the current directory and delete them using the rm command.

  1. Find and delete files before a specific date.
    Suppose you want to delete all files in the current directory whose modification time is earlier than January 1, 2022. You can use the following command:
find . -type f -mtime +456 -exec rm {
    
    } \;

This command will find all files in the current directory with modification times earlier than January 1, 2022, and delete them using the rm command. Note that +456 means 456 days ago because the find command counts days.

  1. Delete an entire directory
    Suppose you want to delete an entire directory named mydirectory, you can use the following command:
rm -rf mydirectory

This command will recursively delete the mydirectory directory and all its subdirectories and files. Use caution as this action cannot be undone.

Please note that the find and rm -rf commands may delete important files, so before using them, make sure you understand how they work and proceed with caution.

2. You can use the "find" command and "delete" command in Linux to delete files that were created or modified before or after a specific time. Here are some example commands:

  1. Delete files modified 7 days ago:
find /path/to/directory -type f -mtime +7 -delete

This command will find all files in the specified directory that were modified 7 days ago and delete them.

  1. Delete files created 30 days ago:
find /path/to/directory -type f -ctime +30 -delete

This command will find all files created 30 days ago in the specified directory and delete them.

  1. Delete all files that have not been accessed for more than a year:
find /path/to/directory -type f -atime +365 -delete

This command will find all files in the specified directory that have not been accessed for more than one year and delete them.

Note that these commands will not ask you if you want to delete each file, so double-check the path and options before executing them.

Guess you like

Origin blog.csdn.net/nj0128/article/details/129856506