A python solution to do the job completely. It creates a tar.gz
file from the latest files of all sub directories of a given directory.
The script
#!/usr/bin/env python3
import os
import time
import tarfile
files_dir = "/path/to/directory/with/subdirectories"
targeted_file = "/path/to/latest_files.tar.gz"
latest_files = []
# get the latest files of all sub directories
for root, dirs, files in os.walk(files_dir):
for dr in dirs:
dr = root+"/"+dr
filelist = []
for item in os.listdir(dr):
file = dr+"/"+item
if os.path.isfile(file):
filelist.append((file, os.stat(file).st_mtime))
filelist.sort(key=lambda x: x[1])
if len(filelist) != 0:
latest_files.append(filelist[-1][0])
# write to latest_files.tar.gz
tar = tarfile.open(targeted_file, "w:gz")
for file in latest_files:
tar.add(file, arcname = file.split("/")[-1])
tar.close()
Copy the script into an empty file, set in the head section of the script the directory, containing the sub directories (files_dir =
), and the path to the tar.gz
file (targeted_file =
), and save it as get_latest.py
.
Run it by the command:
python3 /path/to/get_latest.py
What it does
The script first lists all (sub-) directories. Within the sub directories, it lists the files, sorts them by modification date and adds the latest ones to the "master" list, to be included in the compressed file.
To set the number of versions to include
To give the answer a more universal character, below a version of the script in which you can set the numbers of (latest) versions to include in the compressed file. If the number of set versions (versions =
) exceeds the actual number of files in a folder, all files are included.
The script
#!/usr/bin/env python3
import os
import time
import tarfile
files_dir = "/path/to/directory/with/subdirectories"
targeted_file = "/path/to/latest_files.tar.gz"
versions = 1
latest_files = []
# get the latest files of subdirectories
for root, dirs, files in os.walk(files_dir):
for dr in dirs:
dr = root+"/"+dr
filelist = []
for item in os.listdir(dr):
file = dr+"/"+item
if os.path.isfile(file):
filelist.append((file, os.stat(file).st_mtime))
filelist.sort(key=lambda x: x[1])
if len(filelist) != 0:
for item in filelist[-versions:]:
latest_files.append(item[0])
# write to latest_files.tar.gz
tar = tarfile.open(targeted_file, "w:gz")
for file in latest_files:
tar.add(file, arcname = file.split("/")[-1])
tar.close()