Linux delete all older files

Linux: Delete Files Older Than X Days

Posted on June 14, 2013 By Nikola Stojanoski

This is a very simple tutorial on how to find, move and delete files older than X days. I needed this for a project where I collected some images and needed to archive and delete them after a while.

With this, you will be able with the Linux find command to find your JPG files older than 30 days and then execute rm or mv or whatever command you want on them.

Find files older then

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +7 is for files older than 7 days.

This is the simple find file command that will list all the .jpg files older than 7 days.

Delete files older then

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +15 is for files older than 15 days.
  • Fifth part -exec executes a command. In this case rm is the command, <> gets the filelist and \; closes the command

This will delete all the .jpg files older than 15 days.

Move files older then

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +30 is for files older than 30 days.
  • Fifth part -exec executes a command. In this case mv is the command, <> gets the filelist, path where to move the files and \; closes the command
Читайте также:  Windows anytime upgrade как обновиться

This will move all the .jpg files older than 30 days into a new directory.

bash delete file script

Now we can combine these two commands to archive the images older than 15 days and delete them from the archive folder if they are older then 30 days.

We are going to create a shell script that will do that and we can run it with a crontab.

This command should work on most of the Linux distributions.

Источник

How to Delete Files Older than 30 days in Linux

This is the best practice to remove old unused files from your server. For example, if we are running daily/hourly backup of files or database on the server then there will be much junk created on the server. So clean it regularly. To do it you can find older files from the backup directory and clean them.

This article describe you to how to find and delete files older than 30 days. Here 30 days older means the last modification date is before 30 days.

1. Delete Files older Than 30 Days

You can use the find command to search all files modified older than X days. And also delete them if required in single command.

First of all, list all files older than 30 days under /opt/backup directory.

Verify the file list and make sure no useful file is listed in above command. Once confirmed, you are good to go to delete those files with following command.

2. Delete Files with Specific Extension

Instead of deleting all files, you can also add more filters to find command. For example, you only need to delete files with “.log” extension and modified before 30 days.

For the safe side, first do a dry run and list files matching the criteria.

Once the list is verified, delete those file by running the following command:

Above command will delete only files having .log extension and last modification date is older than 30 days.

3. Delete Old Directory Recursively

Using the -delete option may fail, if the directory is not empty. In that case we will use Linux rm command with find command.

The below command will search all directories modified before 90 days under the /var/log directory.

Here we can execute the rm command using -exec command line option. Find command output will be send to rm command as input.

Читайте также:  Screenshot one screen windows

Conclusion

In this tutorial, you have learned to search and delete files modified older than specific days in Linux command line.

Источник

Delete files older than 10 days using shell script in Unix [duplicate]

I’m new to shell scripts, can anyone help? I want to delete scripts in a folder from the current date back to 10 days. The scripts looks like:

The script will run in every 10 day with Crontab, that’s why I need the current date.

3 Answers 3

find is the common tool for this kind of task :

EXPLANATIONS

  • ./my_dir your directory (replace with your own)
  • -mtime +10 older than 10 days
  • -type f only files
  • -delete no surprise. Remove it to test your find filter before executing the whole command

And take care that ./my_dir exists to avoid bad surprises !

Just spicing up the shell script above to delete older files but with logging and calculation of elapsed time

The code adds a few things.

  • log files named with a timestamp
  • log folder specified
  • find looks for *.txt files only in the log folder
  • type f ensures you only deletes files
  • maxdepth 1 ensures you dont enter subfolders
  • log files older than 7 days are deleted ( assuming this is for a backup log)
  • notes the start / end time
  • calculates the elapsed time for the backup operation.

Note: to test the code, just use -print instead of -print -delete. But do check your path carefully though.

Note: Do ensure your server time is set correctly via date — setup timezone/ntp correctly . Additionally check file times with ‘stat filename’

Note: mtime can be replaced with mmin for better control as mtime discards all fractions (older than 2 days (+2 days) actually means 3 days ) when it deals with getting the timestamps of files in the context of days

Источник

How do you delete files older than specific date in Linux?

I used the below command to delete files older than a year.

But now I want to delete all files whose modified time is older than 01 Jan 2014. How do I do this in Linux?

4 Answers 4

This works for me:

You can touch your timestamp as a file and use that as a reference point:

e.g. for 01-Jan-2014:

this works because find has a -newer switch that we’re using.

This other answer pollutes the file system and find itself offers a «delete» option. So, we don’t have to pipe the results to xargs and then issue an rm.

This answer is more efficient:

This will list files accessed older than 4 days, searching from home directory.

Not the answer you’re looking for? Browse other questions tagged linux find delete-file or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Драйвер для сканера imagerunner canon 1133 series windows 10 64 bit

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to delete files older than X hours

I’m writing a bash script that needs to delete old files.

It’s currently implemented using :

This will delete of the files older than 1 day.

However, what if I need a finer resolution that 1 day, say like 6 hours old? Is there a nice clean way to do it, like there is using find and -mtime?

9 Answers 9

Does your find have the -mmin option? That can let you test the number of mins since last modification:

Or maybe look at using tmpwatch to do the same job. phjr also recommended tmpreaper in the comments.

Here is the approach that worked for me (and I don’t see it being used above)

deleting all the files older than 59 minutes while leaving the folders intact.

You could to this trick: create a file 1 hour ago, and use the -newer file argument.

(Or use touch -t to create such a file).

-mmin is for minutes.

Try looking at the man page.

If one’s find does not have -mmin and if one also is stuck with a find that accepts only integer values for -mtime , then all is not necessarily lost if one considers that «older than» is similar to «not newer than».

If we were able to create a file that that has an mtime of our cut-off time, we can ask find to locate the files that are «not newer than» our reference file.

To create a file that has the correct time stamp is a bit involved because a system that doesn’t have an adequate find probably also has a less-than-capable date command that could do things like: date +%Y%m%d%H%M%S -d «6 hours ago» .

Fortunately, other old tools can manage this, albeit in a more unwieldy way.

To begin finding a way to delete files that are over six hours old, we first have to find the time that is six hours ago. Consider that six hours is 21600 seconds:

Since the perl statement produces the date/time information we need, use it to create a reference file that is exactly six hours old:

Now that we have a reference file exactly six hours old, the «old UNIX» solution for «delete all files older than six hours» becomes something along the lines of:

It might also be a good idea to clean up our reference file.

Источник

Оцените статью