find and delete file or folder older than x days
You can make use of this piece of code
find /tmp/* -mtime +7 -exec rm {} \;
Explanation
The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +7, it will find files older than 7 days.
The third argument, -exec, allows you to pass in a command such as rm. The {} ; at the end is required to end the command.
Source : http://www.howtogeek.com/howto/ubuntu/delete-files-older-than-x-days-on-linux/
For deleting folders, after emptying inside of them you can rmdir
instad of rm
in the piece of code, also if you only want to see directories you can add
-type d
to piece of code such as below:
find /tmp/*/* -mtime +7 -type d -exec rmdir {} \;
Pythonic way to delete files/folders older than X days
Not a python expert here either, but here's something simple:
Find the date oldest date that you want to keep. Anything older than this will be deleted. Let's say it is the 28/04/2020
From that date, you can build a string "/root_folder/2020/04/28"
List all the files, if their path (as string) is less than the string from the previous step, you can delete them all
Example:
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.txt' in file:
files.append(os.path.join(r, file))
Source of that snippet: https://mkyong.com/python/python-how-to-list-all-files-in-a-directory/
Now, you can do:
for f in files:
if f < date_limit:
os.remove(f)
Note: This is not optimal
It deletes file by file, but the moment you enter the if you could just delete the whole folder where this file is (but then the list of files points to files that have been deleted).
You actually don't care about the files. You could apply the logic to folders alone and remove them recursively.
Update: doing both steps as we browse the folders:
for r, d, f in os.walk(path):
if( r < date_limit ):
print(f"Deleting {r}")
shutil.rmtree(r)
python delete folders older than x days
#! /usr/bin/python
import time
import ftputil
host = ftputil.FTPHost('ftphost.com', 'username', 'password')
mypath = 'your_path'
now = time.time()
host.chdir(path)
names = host.listdir(host.curdir)
for name in names:
if host.path.getmtime(name) < (now - (5 * 86400)):
if host.path.isfile(name):
host.remove(name)
print 'Closing FTP connection'
host.close()
Filter and delete files and folders(and files inside of folders) older than x days in powershell
param(
[IO.DirectoryInfo]$targetTolder = "d:\tmp",
[DateTime]$dateTimeX = "2020-11-15 00:00:00"
)
Get-ChildItem $targetTolder -Directory -Recurse | Sort-Object {$_.FullName} -Descending | ForEach-Object {
Get-ChildItem $_ -File | Where-Object {$_.LastWriteTime -lt $dateTimeX} | Remove-Item -Force
if ((Get-ChildItem $_).Count -eq 0){Remove-Item $_ -Force}
}
remove -WhatIf after test
Delete folders older than X days in network location
but I get an error that I can't use UNC paths.
True! But there is a workaround:
With the command pushd
you can push a network location to a stack and automatically map a network drive. With popd
you pop the directory, and unmap the drive again:
pushd "\\Server\Your Folder\"
REM do your thing with forfiles
popd
REM done
If there are questions left, feel free to ask :)
Batch file to delete files older than N days
Enjoy:
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"
See forfiles
documentation for more details.
For more goodies, refer to An A-Z Index of the Windows XP command line.
If you don't have forfiles
installed on your machine, copy it from any Windows Server 2003 to your Windows XP machine at %WinDir%\system32\
. This is possible since the EXE is fully compatible between Windows Server 2003 and Windows XP.
Later versions of Windows and Windows Server have it installed by default.
For Windows 7 and newer (including Windows 10):
The syntax has changed a little. Therefore the updated command is:
forfiles /p "C:\what\ever" /s /m *.* /D -<number of days> /C "cmd /c del @path"
Related Topics
Converting Serial Port Data to Tcp/Ip in a Linux Environment
Removing Part of a Filename for Multiple Files on Linux
Start Script After Another One (Already Running) Finishes
How to Get the Resolution (Width and Height) for a Video File from a Linux Command Line
How to Convert \Uxxxx Unicode to Utf-8 Using Console Tools in *Nix
Colour Highlighting Output Based on Regex in Shell
Docker in Docker Cannot Mount Volume
Check the Open Fd Limit for a Given Process in Linux
How to Get a List of Available Wireless Networks on Linux
How to Change the Filename of a Shared Library After Building a Program That Depends on It
Signed Executables Under Linux
Crt1.O: in Function '_Start': - Undefined Reference to 'Main' in Linux
A General Linux File Permissions Question: Apache and Wordpress
What Is Global _Start in Assembly Language
How to Increase Neo4J's Maximum File Open Limit (Ulimit) in Ubuntu