Recursively Remove Files

How to Recursively Remove Files of a Certain Type

Adding backslashes before spaces protects the spaces against expansion in shell source code. But the output of a command in a command substitution does not undergo shell parsing, it only undergoes wildcard expansion and field splitting. Adding backslashes before spaces doesn't protect them against field splitting.

Adding backslashes before dashes is completely useless since it's rm that interprets dashes as special, and it doesn't interpret backslashes as special.

The output of find is ambiguous in general — file names can contain newlines, so you can't use a newline as a file name separator. Parsing the output of find is usually broken unless you're dealing with file names in a known, restricted character set, and it's often not the simplest method anyway.

find has a built-in way to execute external programs: the -exec action. There's no parsing going on, so this isn't subject to any problem with special characters in file names. (A path beginning with - could still be interpreted as an option, but all paths begin with . since that's the directory being traversed.)

find . -type f -name '*.gz' -exec rm {} +

Many find implementations (Linux, Cygwin, BSD) can delete files without invoking an external utility:

find . -type f -name '*.gz' -delete

See Why does my shell script choke on whitespace or other special characters? for more information on writing robust shell scripts.

Deleting files with Powershell recursively

This should do what you want:

$source = 'D:\Test'
$days = 7

# Remove Files
Get-ChildItem $Source -Recurse | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (get-date).AddDays(-$($days)) } | % { Remove-Item -Path $_.FullName -Force }

# Remove empty directories
Get-ChildItem $source -Recurse | Where-Object { $_.PSIsContainer -and $_.GetFiles("*.*").Count -le 0 } | % { Remove-Item -Path $_.FullName -Force }

Python recursively remove files/folders in a directory, but not the parent directory or a specific folder

I would not check equality, but whether your path contains the folder you want to keep. This example is adapted from the docs (scroll up a little to see it)

import os

# change this, obviously
root_dir = r'C:\Users\ben\Documents\Python Scripts\tmp'

keep = 'keep'

for root, dirs, files in os.walk(root_dir):
for name in files:
# make sure what you want to keep isn't in the full filename
if (keep not in root and keep not in name):
print(os.path.join(root, name)) #change to os.remove once sure
for name in dirs:
if (keep not in root and keep not in name):
print(os.path.join(root, name)) # change to os.rmdir once sure

Note: depending on how detailed your keep is, this might not erase some files you want to erase: rm_me\keep.txt will be kept even though it's not in the right directory. Detailed folder names can help with this: if keep is something like r'C:\very_specific', that won't be the name of a file, and you'll erase everything you need.

Deleting folders in python recursively

Try shutil.rmtree:

import shutil
shutil.rmtree('/path/to/your/dir/')

PathLib recursively remove directory?

As you already know, the only two Path methods for removing files/directories are .unlink() and .rmdir() and neither does what you want.

Pathlib is a module that provides object oriented paths across different OS's, it isn't meant to have lots of diverse methods.

The aim of this library is to provide a simple hierarchy of classes to
handle filesystem paths and the common operations users do over them.

The "uncommon" file system alterations, such as recursively removing a directory, is stored in different modules. If you want to recursively remove a directory, you should use the shutil module. (It works with Path instances too!)

import shutil
import pathlib
import os # for checking results

print(os.listdir())
# ["a_directory", "foo.py", ...]

path = pathlib.Path("a_directory")

shutil.rmtree(path)
print(os.listdir())
# ["foo.py", ...]


Related Topics



Leave a reply



Submit