Linux удалить файлы старше года

How to delete files older than X hours

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:

find $LOCATION -name $REQUIRED_FILES -type f -mmin +360 -delete 

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

Using —mmin +X returns all files with my find. My fault for not checking this first, but this command just deleted most of my home directory. For me, —mmin -X is the correct argument.

tmpreaper is a fork of tmpwatch. It is safer, and exists as a debian package in the distro. Benefits over find -delete : tmpreaper will not remove symlinks, sockets, fifos, or special files

Point out that $REQUIRED_FILES need to be in quotes (double quotes) and you can use a pattern like: «.txt» for all .txt files or files beginning with a pattern like: «temp-» to delete all files named with temp-

@PaulDixon how to modify this so that $LOCATION and $REQUIRED_FILES can both have multiple values such as dir1 dir2 and *.txt *.tmp ?

@Enissay $LOCATION is a single directory. For multiple extensions you’d probably want to use a pattern with -regex — see stackoverflow.com/questions/5249779/…

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

$ find /path/to/the/folder -name '*.*' -mmin +59 -delete > /dev/null 

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

Better to single-quote ‘*.*’ or the shell will expand it to actual filenames instead of keeping it as a wildcard for find to resolve. This breaks find ‘s recursive operation on subdirs.

Also keep in mind that -name ‘*.*’ will not delete files that have no extension, such as README , Makefile , etc.

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).

there is no -older switch (at least in my find command), and that’s what would be needed. -newer doesn’t help.

can you give a touch command that would generate a file 1 hour old that will work on machines that can’t use -mmin? (If you’re on Linux, -mmin is available, if not then date and other commands are also feeble in comparison.)

@iconoclast touch -t $(date -d ‘-1 hour’ +%Y%m%d%H%M.00) test Creates file test that’s always 1 hour old.

To rm files and directories older than file.ext run rm -r `find -maxdepth 1 -not -newer file.ext` . To rm files and directories newer than file.ext do rm -r `find -maxdepth 1 -newer file.ext` . To place file.ext where you want it in time run touch -t $(date -d ‘-1 hour’ +%Y%m%d%H%M.00) file.ext where ‘-1 hour’ specifies «1 hour ago». Credit: xoftl, rovr138, nickf.

 Example 6 Selecting a File Using 24-hour Mode The descriptions of -atime, -ctime, and -mtime use the ter- minology n ``24-hour periods''. For example, a file accessed at 23:59 is selected by: example% find . -atime -1 -print at 00:01 the next day (less than 24 hours later, not more than one day ago). The midnight boundary between days has no effect on the 24-hour calculation. 

If you do not have «-mmin» in your version of «find», then «-mtime -0.041667» gets pretty close to «within the last hour», so in your case, use:

Читайте также:  Linux command do done

so, if X means 6 hours, then:

works because 24 hours * 0.25 = 6 hours

Was hopeful because this old UNIX doesn’t have -mmin, but, sadly this is of no help as this old UNIX also does not like fractional values for mtime: find: argument to -mtime must be an integer in the range -2147483647 to 2147483647

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:

$ date && perl -e '@d=localtime time()-21600; \ printf "%4d%02d%02d%02d%02d.%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' > Thu Apr 16 04:50:57 CDT 2020 202004152250.57 

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

$ date && touch -t `perl -e '@d=localtime time()-21600; \ printf "%4d%02d%02d%02d%02d.%02d\n", \ $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]'` ref_file && ls -l ref_file Thu Apr 16 04:53:54 CDT 2020 -rw-rw-rw- 1 root sys 0 Apr 15 22:53 ref_file 

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:

$ find . -type f ! -newer ref_file -a ! -name ref_file -exec rm -f "<>" \; 

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

Источник

How To Delete Files Older Or Newer Than N Days Using find (With Extra Examples)

This article explains how to delete files older or newer than N days, with extra examples like matching only files using a particular extension, and more. For this we’ll use find , a command line tool to search for files in a directory.

Читайте также:  Linux автоматическое обновление пакетов

You can delete all files and folders older than (with the file’s data modification time older than) N days from a directory by using:

find /directory/path/ -mindepth 1 -mtime +N -delete

An explanation of the whole command and what you need to replace:

  • find is the Unix command line tool for finding files (and more)
  • /directory/path/ is the directory path where to find (and delete) files. Replace it with the path of the directory from which you want to delete files and folders older than N days
  • -mindepth 1 is used to not apply any tests or actions at levels less than the specified level. -mindepth 1 means the command should process all files except the command line arguments. Without using this, the command would attempt to also remove /directory/path/ if it matches the find criteria. You can skip it in all the commands mentioned in this article if you want to also match the specified find path. Also, there’s no need to specify this if you only look for files ( -type f for regular files for example), and not folders
  • -mtime +N is used match files that had their data (content) last modified N days ago. Replace N with a number (integer). In this command, files with a modification time older than N days will be deleted. It’s important to note that for example, if a file was last modified 1 day and 23 hours ago, -mtime +1 won’t match it, treating it as if the file was last modified 1 day, 0 hours, 0 minutes, and 0 seconds ago; see this explanation on why that’s the case, since the find man page doesn’t do this very well
  • -delete deletes matched files and folders
find /directory/path/ -mindepth 1 -mtime +N

Let’s take a look at an example. To delete all files and folders older than 10 days from the ~/Downloads folder you can use:

find ~/Downloads -mindepth 1 -mtime +10 -delete

To delete all files and folders newer than (with a file modification time newer than) N days, use -N instead of +N :

find /directory/path/ -mindepth 1 -mtime -N -delete

Example in which we’ll remove all files and folders from ~/Downloads that had their contents modified between now and 10 days ago:

find ~/Downloads -mindepth 1 -mtime -10 -delete

A few important observations that I recommend reading:

  1. The arguments order matters! You should add -delete after matching the files. If it’s the first argument, every file (and folder) will be deleted from /directory/path/ , regardless of them matching your query or not. So always add -delete at the end of the command.
  2. Not all versions of find support -delete , which is a GNU extension. In case the find version you’re using doesn’t support -delete , you can use -exec rm -rf <> + , though there are find versions that don’t support this either from what I’ve read. Using -exec rm -rf <> + to remove files older than N days, and only matching files (keeping empty subdirectories):
find /directory/path/ -mindepth 1 -type f -mtime +N -exec rm -rf <> +
find /directory/path/ -mindepth 1 -mtime +N -exec rm -rf <> \;

Other examples of using find to delete files or folders based on their modification time

Only delete regular files older than N days, keeping empty subdirectories:

find /directory/path/ -mindepth 1 -type f -mtime +N -delete

Here we’ve used -type f to match only regular files. You can also use -type d to match folders, or -type l to match symbolic links.

Only delete files matching .extension older than N days from a directory and all its subdirectories:

find /directory/path/ -type f -mtime +N -name '*.extension' -delete

You can add -maxdepth 1 to prevent the command from going through subdirectories, and only delete files and 1st level depth only directories:

find /directory/path/ -mindepth 1 -maxdepth 1 -mtime +N -delete

You may also use -ctime +N , used to match (and delete in this example) files that had their status last changed N days ago (the file attributes/metadata AND/OR file content was modified), as opposed to -mtime , which only matches files based on when their content was last modified:

find /directory/path/ -mindepth 1 -ctime +N -delete

Источник

Delete files older than X days +

Possibly fun when I have files with spaces. E.g a file called «test one» and rm gets fed rm test one . (Which will delete a file called «test» and a file called «one», but not a file called «test one»). Hint: -delete or -print0

As a side note, always quote the argument provided by find to avoid issues with special characters, as mentioned in the answer’s first line. E.g.: find /path/to/files/ -exec somecommand ‘<>‘ \;

4 Answers 4

Be careful with special file names (spaces, quotes) when piping to rm.

There is a safe alternative — the -delete option:

find /path/to/directory/ -mindepth 1 -mtime +5 -delete 

That’s it, no separate rm call and you don’t need to worry about file names.

Replace -delete with -depth -print to test this command before you run it ( -delete implies -depth ).

  • -mindepth 1 : without this, . (the directory itself) might also match and therefore get deleted.
  • -mtime +5 : process files whose data was last modified 5*24 hours ago.

Alternatively, if you want to do the same for all files NEWER than five days: find /path/to/directory/ -mindepth 1 -mtime -5 -delete

Note that every find argument is a filter that uses the result of the previous filter as input. So make sure you add the -delete as the last argument. IE: find . -delete -mtime +5 will delete EVERYTHING in the current path.

Note that this command will not work when it finds too many files. It will yield an error like:

bash: /usr/bin/find: Argument list too long 

Meaning the exec system call’s limit on the length of a command line was exceeded. Instead of executing rm that way it’s a lot more efficient to use xargs. Here’s an example that works:

find /root/Maildir/ -mindepth 1 -type f -mtime +14 | xargs rm 

This will remove all files (type f) modified longer than 14 days ago under /root/Maildir/ recursively from there and deeper (mindepth 1). See the find manual for more options.

Источник

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