Linux delete files older than 7 days

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

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.

Читайте также:  Windows forms assembly version

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.

Источник

Removing files older than 7 days

I write below command to delete all files that are older than 7 days, but it doesn’t work:

How can I remove these files?

3 Answers 3

As @Jos pointed out you missed a space between name and ‘*.gz’ ; also for speeding up the command use -type f option to running the command on files only.

So the fixed command would be:

Explanation:

  • find : the unix command for finding files/directories/links and etc.
  • /path/to/ : the directory to start your search in.
  • -type f : only find files.
  • -name ‘*.gz’ : list files that ends with .gz .
  • -mtime +7 : only consider the ones with modification time older than 7 days.
  • -execdir . \; : for each such result found, do the following command in . .
  • rm — ‘<>‘ : remove the file; the <> part is where the find result gets substituted into from the previous part. — means end of command parameters avoid prompting error for those files starting with hyphen.

Alternatively, use:

Which is a bit more efficient, because it amounts to:

as in the -exec method.

An alternative and also faster command is using exec’s + terminator instead of \; :

This command will run rm only once at the end instead of each time a file is found and this command is almost as fast as using -delete option as following in modern find :

Be careful removing files with find. Run the command with -ls to check what you are removing

find /media/bkfolder/ -mtime +7 -name ‘*.gz’ -ls . Then pull up the command from history and append -exec rm <> \;

Limit the damage a find command can do. If you want to remove files from just one directory, -maxdepth 1 prevents find from walking through subdirectories or from searching the full system if you typo /media/bkfolder / .

Other limits I add are more specific name arguments like -name ‘wncw*.gz’ , adding a newer-than time -mtime -31 , and quoting the directories searched. These are particularly important if you are automating cleanups.

find «/media/bkfolder/» -maxdepth 1 -type f -mtime +7 -mtime -31 -name ‘wncw*.gz’ -ls -exec rm <> \;

Источник

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.

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.

Читайте также:  Windows cannot repair this computer automatically что делать

Источник

How to Remove files older than ‘N’ days using Tmpwatch/Tmpreaper on Linux

You may have missed to delete some files that are no longer needed on your computer in some directory.

It could be either in “Download” or any other directory that has been used frequently.

It might have grown up, over a period of time.

Even though if you have enough storage, you should remove the list of unused files or files which are no longer in use from the Directory, otherwise it will slow down your system performance.

It is very difficult to search a file in such a clumsy directory which is having thousands of files, when you are not sure on the specific file names or extensions.

This can be performed by using the ‘find‘ command with some combination, Please refer the below articles which were written in the past.

Today, we will show you how to achieve this, by using the Tmpwatch utility on Linux.

What is Tmpwatch

Tmpwatch recursively removes files that has not been accessed for a specific period of time in the specified directories.

Typically, it is used to clean directories automatically in the temporary file systems, such as / tmp or /var/tmp.

Ideally, it will remove the empty directories, regular files, and symbolic links.

This command will not execute on other file systems, and excludes the “lost+found” directory of the root user.

By default, tmpwatch deletes files based on their atime (access time), not their mtime (conversion time).

You can change this behavior by adding other parameters in the tmpwatch command.

WARNING: Please do not run “tmpwatch” or “tmpreaper” in “/” (root directory) because there is no mechanism in the program to protect against this.

How to install Tmpwatch on Linux

Tmpwatch can be installed as follows, from the distribution official repository.

For RHEL/CentOS 6 systems, use the yum command to install Tmpwatch.

For Debian and Ubuntu systems, use the apt command or apt-get command to install Tmpreaper.

For openSUSE systems, use the zypper command to install Tmpwatch.

For Fedora systems, use the dnf command to install Tmpwatch.

Make a note: If you are using Debian-based systems, use “tmpreaper” instead of tmpwatch. All examples will work as expected.

Understanding key options and arguments

  • atime (File Last Access Time) – Access time shows the last time the data was read from a file by any of the process such as command or script, etc, leaving the data unmodified.
  • mtime (File Last Modify Time) – mtime shows when you modify, append or update a file content. Most of the times ctime and mtime will be the same, unless the file attributes are updated.
  • ctime (File Last Change Time) – ctime shows when your file metadata got changed. It means when the file attributes are changed like ownership or group, etc.,
  • dirmtime (Directory Last modification time) – dirmtime shows the last modified time of the directory.

The time parameter defines the threshold for removing files.

  • d – for days
  • h – for hours
  • m – for minutes
  • s – for seconds

Remove files that haven’t been accessed for a Period of Time

As mentioned in the beginning of the article, Tmpwatch delete files, by default, referring the access time of the files. No need to add the hours parameter as suffix to time, since the action is performed by default using the hours unit.

Читайте также:  Отличие windows 10 корпоративная ltsc

For example, run the command below to recursively remove files, that have not been accessed for the past 5 hours.

Run the command below to delete files that have not been modified for the last 10 hours. If you want to delete files based on their modification time (mtime), you need to add the “-m” option with the tmpwatch command.

Delete files that haven’t been accessed more than “X” days

If you want to delete files using days, you need to add the suffix “d”. The example below deletes files older than 30 days.

Delete all files that haven’t been accessed for a Period of Time

The below command removes all file types, not just regular files, symbolic links and directories based on the file modification time.

Exclude a directory

The below command will delete all files and excludes directories that haven’t been modified for the past 10 hours.

Exclude a specific path

The below command will delete all files except the directory below which has not been modified for the past 10 hours.

Exclude specific pattern

The below command will delete all files except the Pattern below which has not been modified for the past 10 hours.

Perform a dry run

If you want to perform a test run without actually delete anything on the system, use the below command.

Setup a cronjob to delete files periodically

By default, it leaves a cronjob file under the “/etc/cron.daily/tmpreaper” directory. This cronjob works according to the configuration file located in “/etc/timereaper.conf”. You can customize the file according to your requirement.

It runs once a day and deletes files older than 7 days.

Alternatively, if you would like to perform an action periodically, you can manually add a cronjob based on your needs.

The above cronjob will delete files that are older than 15 days daily at 10AM.

Источник

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

Источник

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