To delete files older than 30 days in Linux, you can use the following command:
find /path/to/directory -type f -mtime +30 -exec rm -f {} +
This command will find all regular files (-type f) in the directory specified by /path/to/directory
that have been modified more than 30 days ago (-mtime +30). It will then execute the rm
command to delete the files. The -f
option to rm
tells it to force the deletion of the files, even if they are read-only.
For example, to delete all regular files in the current directory that have been modified more than 30 days ago, you would use the following command:
find . -type f -mtime +30 -exec rm -f {} +
This command will prompt you for confirmation before deleting any files. If you do not want to be prompted, you can use the -f
option to find
instead of the -exec
option:
find . -type f -mtime +30 -delete
This command will delete all regular files in the current directory that have been modified more than 30 days ago without prompting you for confirmation.
This command will find all regular files (-type f) in the current directory that have been modified more than 30 days ago (-mtime +30). It will print out the names of the files, but it will not actually delete them.
find . -type f -mtime +30
For example, if you have a file called file.txt
in the current directory that was modified 31 days ago, the command would print out the following:
file.txt
You can use this information to decide whether or not you want to delete the files.
Warning: Be careful when using this command, as it can easily delete important files. It is a good idea to test the command on a small subset of files before using it on a large directory. You’ve been warned!!!