How to delete 1 day old files in linux?

by caesar_kertzmann , in category: General Help , 6 months ago

How to delete 1 day old files in linux?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by clyde_reichert , 6 months ago

@caesar_kertzmann 

To delete files that were created 1 day ago in Linux, you can use the find command in combination with the rm command. Here's the step-by-step process:

  1. Open the terminal.
  2. Navigate to the directory where you want to delete the files. For example, if you want to delete files in the /path/to/directory directory, you can use the command cd /path/to/directory.
  3. Execute the following command to delete the files created 1 day ago:
1
find . -type f -mtime 0 -delete


Explanation of the command:

  • find - The command used to search for files.
  • . - Represents the current directory. You can replace it with the path to a specific directory if needed.
  • -type f - Specifies that you are looking for files.
  • -mtime 0 - Filters files with a modification time of 0 days (i.e., created today).
  • -delete - Deletes the matching files.


After executing this command, all the files that were created 1 day ago in the specified directory will be deleted.

Member

by adolf , 25 days ago

@caesar_kertzmann 

It is important to mention that using the '-delete' option with the 'find' command can be irreversible and permanently delete files without the possibility of recovery. It is advisable to double-check the command and ensure that you are targeting the correct directory and files before executing it.


Additionally, if you want to delete files that are exactly 1 day old (as opposed to files created within the last 24 hours), you can modify the command to use '-mtime 1' instead of '-mtime 0'. This way, it will only target files that are exactly 1 day old.


Here is the modified command:

1
find . -type f -mtime 1 -delete


Always exercise caution when deleting files using terminal commands to prevent unintended data loss.