Lets say I want to find the youngest/newest *.txt file inside a folder, for example /tmp/? I wrote this little python script for this, but are there better ways to do it?

By youngest I mean the file with the least recent change or creation date? All files have these timestamps. But could I also watch the filesystems folder for file creation events?

import os
import glob

def find_newest_file_type(glob_path):
    # youngest file has biggest timestamp
    youngest = -10
    ultpath = "file_does_not_exist0xfadfadsfadfads.asdfajsdklfj"
    for file in  glob.iglob(glob_path):
        mtime = os.path.getmtime(file)
        if mtime > youngest:
            youngest = mtime
            ultpath = file
    return ultpath

newest = find_newest_file_type("/tmp/*.txt")
  • scratsearcher 🔍🔮📊🎲@sopuli.xyzOP
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    3 months ago

    Are you limited to doing this in python?

    I am really unexperienced in bash, so until now I did even these things in python … but maybe this should change


    When I run this in my /tmp folder like this: find . -type f -name "*.tmp" -printf '%T@ %p\n' | sort -n | tail -n 1 | cut -f2- -d" " It says these things …

    find: ‘./systemd-private…<imagine service name here>’: Permission denied
    

    While the other commenters command ls *.tmp -Art | tail -n 1 does not complain as much, is this because it searches all sub-directories as well?

    • dandelion (she/her)@lemmy.blahaj.zone
      link
      fedilink
      English
      arrow-up
      2
      ·
      edit-2
      3 months ago

      yes, find does look at sub-directories - sorry, I didn’t realize you only wanted it to search in the current dir and not in sub-directories. You would add -maxdepth 1 to the find command to tell it to only look in the current dir, not further.

      EDIT: and yeah, I would recommend doing tasks like this in bash - you can just type them directly into the terminal. The convenience of writing one-liners like this from memory is hard to beat.

      Once you have enough experience you can just do it from memory, it’s all accessible. Writing a python program and running it every time you want to do a small task like finding the newest file is overkill IMO. But it can be nice to have a little cheatsheet file of bash oneliners you have written in the past to refer to later, esp. as a beginner.

      That said, it’s not wrong to take what I would see as the less convenient route by writing a python script - it can be fun to learn both, and it’s more useful to use python if you need to automate a more complicated task or need that newest file as a part of a larger program you’re writing in python.

      • scratsearcher 🔍🔮📊🎲@sopuli.xyzOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        3 months ago

        Thanks, with maxdepth 1 it works without errors!


        Since the step of finding the youngest file in the folder is part of a larger more complicated workflow/pipeline for me (transcribe the youngest wav file in the tmp folder, name the resulting transcript after the youngest file in the …/recordings_folder/ and copy it there) I will need to integrate it into a python script I think.