In a single (flat) directory
If all files are in one and the same directory, the script below should do the job.
#!/usr/bin/env python3
import os
import sys
dr = sys.argv[1]; ids = ("_3800.mp4", "_8000.mp4")
checklist = [f[:-9] for f in os.listdir(dr) if f[-9:] in (ids)]
for f in [f for f in set(checklist) if checklist.count(f) != 1]:
os.remove(dr+"/"+f+"_8000.mp4")
How it works:
- The script lists all files, ending with either
_3800.mp4
or _8000.mp4
, stripped from their endings
- If the (stripped-) name occurs twice, both versions exist (since duplicate names are impossible in one and the same directory)
- Subsequently, the script removes the
_8000.mp4
- version from the duplicates
In a layered directory (search recursively)
A slightly different approach. It compares files recursively and (only) deletes the _8000.mp4
- version if the _3800.mp4
- version exists.
I wasn't sure there was a need for a recursive search, but added it anyway.
How it works:
- First list the files to keep anyway
- Then remove possible "throw aways", if the version-to-keep exists.
#!/usr/bin/env python3
import os
import sys
files_dir = sys.argv[1]; file_list = []
#---
keep = "_3800.mp4"; rm = "_8000.mp4"
#---
for root, dirs, files in os.walk(files_dir):
for name in files:
if name.endswith(keep):
file_list.append(name)
for name in files:
if name.endswith(rm) and name.replace(rm, keep) in file_list:
os.remove(root+"/"+name)
How to use
To use either one of these scripts:
- Copy the script into an empty file, save it as
clean_directory.py
Run it, with the directory to clean up as an argument, by the command:
python3 /path/to/clean_directory.py </path/to/directory/to/clean>
XXXXX
or every other pair has different pattern?linux command or output thereof
... ;-)