- Removing files older than 7 days
- Explanation:
- Alternatively, use:
- Delete files older than X days +
- 4 Answers 4
- Delete files older than specific file
- 2 Answers 2
- You must log in to answer this question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- How to Delete Files Older than 30 days in Linux
- 1. Delete Files older Than 30 Days
- 2. Delete Files with Specific Extension
- 3. Delete Old Directory Recursively
- Conclusion
- find files older than X days in bash and delete
- 3 Answers 3
Removing files older than 7 days
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:
find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '<>' \;
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:
find /path/to/ -type f -mtime +7 -name '*.gz' -print0 | xargs -r0 rm --
-print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that -print uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs.
Which is a bit more efficient, because it amounts to:
rm file1; rm file2; rm file3; .
An alternative and also faster command is using exec’s + terminator instead of \; :
find /path/to/ -type f -mtime +7 -name '*.gz' -execdir rm -- '<>' +
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 :
find /path/to/ -type f -mtime +7 -name '*.gz' -delete
I think when using find to do a batch deletion like all *.gz files, it would be important to make use of -maxdepth . The Find command seems to be recursive by default, and it will search all child directories for a matching pattern. This may be unwanted behavior, so it would be a good idea to use -maxdepth 1
for the completeness: you may also use -delete
find /some/where/ -name '*.log' -type f -mtime +7 -delete
this way you don’t run a command per file.
This is the best answer. Well done for placing the «-delete» portion at the end of the line. I strongly suggest anyone considering this method reads the man page prior to implementation.
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 <> \;
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.
Delete files older than specific file
I need to delete from a folder all files older than a specific file.
Running bash on CentOS 7. I have a solution for this, but I think there should be a more elegant way do it:
reference_file=/my/reference/file get_modify_time() < stat $1 | grep -Po "Modify: \K[0-9- :]*" >pit=$(get_modify_time $reference_file) for f in /folder/0000* ; do [[ "$pit" > "$(get_modify_time $f)" ]] && rm $f ; done
2 Answers 2
I haven’t tried it, but find should be able to handle the whole operation just fine:
$ find dir/ -type f ! -newer reference -delete
$ find dir/ -type f ! -newer reference ! -name reference -delete
- ! -newer reference matches files which have been modified less recently than reference .
- -delete deletes them.
- ! -name reference excludes reference , in case it is also located under dir/ and you want to keep it.
This should delete all files older than reference , and located under dir/ .
! -newer means «not newer», so «older or the same age«; the file itself will be matched if it’s in the path find looks at, just something to keep in mind.
@ferada It will not if you exclude it, which is why my answer also includes ! -name reference (see the third bullet).
compare file modification times with test , using -nt (newer than) and -ot (older than) operators:
if [ "$file1" -ot "$file2" ]; then #do whatever you want; fi
Thanks, that’s better than my solution but you still need to iterate all files in the folder and compare. The other answer is even more elegant
You must log in to answer this question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.17.43537
Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.
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 30 days in Linux
Regularly cleaning out old unused files from your server is the best practice. For example, if we are running a daily/hourly backup of files or databases 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 describes 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
Using the find command, you can search for and delete all files that have been modified more than X days. Also, if required you can delete them with a single command.
First of all, list all files older than 30 days under /opt/backup directory.
find /opt/backup -type f -mtime +30
Verify the file list and make sure no useful file is listed in the above command. Once confirmed, you are good to go to delete those files with the following command.
find /opt/backup -type f -mtime +30 -delete
2. Delete Files with Specific Extension
You can also specify more filters to locate commands rather than deleting all files. For example, you can only delete files with the “.log” extension and modified before 30 days.
For the safe side, first, do a dry run and list files matching the criteria.
find /var/log -name "*.log" -type f -mtime +30
Once the list is verified, delete those files by running the following command:
find /var/log -name "*.log" -type f -mtime +30 -delete
The above command will delete only files with a .log extension and with the last modification date older than 30 days.
3. Delete Old Directory Recursively
The -delete option may fail if the directory is not empty. In that case, we will use the Linux rm command with find to accomplish the deletion.
Searching all the directories under /var/log modified before 90 days using the command below.
find /var/log -type d -mtime +90
Here we can execute the rm command using -exec command line option. Find command output will be sent to rm command as input.
find /var/log -type d -mtime +30 -exec rm -rf <> \;
WARNING: Before removing the directory, Make sure no user directory is being deleted. Sometimes parent directory modification dates can be older than child directories. In that case, recursive delete can remove the child directory as well.
Conclusion
You have learned how to find and delete files in the Linux command line that have been modified more than a specified number of days ago. That will help you clean up your system from unwanted files.
find files older than X days in bash and delete
I have a directory with a few TB of files. I’d like to delete every file in it that is older than 14 days. I thought I would use find . -mtime +13 -delete . To make sure the command works as expected I ran find . -mtime +13 -exec /bin/ls -lh ‘<>‘ \; | grep ‘
See -daystart option for find. Your find counts exactly 24*13 hours backwards, leaving files which might be 24*13 — 1 minute and later your another find will find those.
Just figured it out! The reason is ls . find finds a directory with mtime +13 and ls simply list all it’s content no matter what mtime the files have (facepalm!).
Always test your find command first by replacing «-delete» with «-print». It may also include the current directory (.) in the result list, which may or may not be what you want.
3 Answers 3
find your/folder -type f -mtime +13 -exec rm <> \;
Doesn’t work for filenames containing spaces. Either (GNU specific) find -delete or find -print0 | xargs -0 rm
@grebneke: can you back up your statement with examples or facts? find ‘s <> is well-known to be safe regarding spaces and funny symbols in file names.
$ find ./folder_name/* -type f -mtime +13 -print | xargs rm -rf
The -r switch is useless. Moreover, you’ll run into problems if you have filenames containing spaces or other funny symbols. Use -print0 and xargs -0 . if your utilities support them, otherwise, use @Mindx’s answer. Or, if your find supports it, use the -delete test of find as so: find ./folder_name -type f -mtime +13 -delete .
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
using asterisk find ./folder_name/* is mistake, because shell will expand ‘‘ info all items existent in this folder. When folder keeps extremally huge items (files or directories), it will exceed maximum arguments or max characters for execute command. Better remove ‘‘ signt, this will do the same, without any automated shel expand. but if you need to find specified names, use option -name ‘some*thing’ and give expansion directly to find internals.