Find and Delete File or Folder Older Than X Days

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 rmdirinstad 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:

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

  2. From that date, you can build a string "/root_folder/2020/04/28"

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

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

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



Leave a reply



Submit