Reusable python on the command line - lmmx/devnotes GitHub Wiki

  • you can use python -c as a short version of awk, e.g. to parse the output of ls -lt for the last column (but it's better to just use it as a throwaway scripting method rather than using awk)

  • this is dumb:

    from subprocess import check_output
    file_list = check_output(["ls", "-lt"]).decode().split('\n')[1:-1]
    filenames = [x.split(' ')[-1] for x in file_list]
    
  • handy one liner to copy over directories [e.g. month directories at the start of the year], the below line copied february-december month subfolders initialised for a 'chemistry' category directory:

    dirlist=$(python -c 'import os; dirl=os.listdir("../chem"); dirl.sort(); [print(x) for x in dirl[1:]]')
    for l in $dirlist; do mkdir "$l"; done