1

I am trying to write a one line shell command (bash) to save a list of movies to a CSV file. I am okay with using a script if needed.

My folders are laid out as follows:

    -Movies/
    --A/
    ----After Earth (2013).mkv
    --B/
    ----Batman (1989).mkv

Using this command:

     ls Movies/* | grep '.mkv' | cut -d. -f1

This provides me with a list of movies that I know to be of the type MKV.

    12 Monkeys (1995)
    2001 - A Space Odyssey (1968)
    A Million Ways To Die In The West (2014)
    A Series of Unfortionate Events (2004)
    Bad Company (2002)

I would eventually however like to end up with a CSV file that looks as follows:

    MOVIE_NAME, MOVIE_YEAR, FILE_TYPE, CREATED_DATE (YYYY-MM-DD)

Perhaps something involving sed or awk may be necessary?

  • Is the year of the movie always in quotes? – cutrightjm May 14 '17 at 5:35
  • With ffprobe (part of avconv) you can extract a lot of data from the metadata / tags of the media file. For example ffprobe -v error -show_entries format_tags=title,date:format=duration file.mp4. – Tigger May 14 '17 at 6:39
  • @cutrightjm, yes the year of the movie is always in quotes. Always in the format [Movie Title (YYYY).mkv]. – Matthew Kelley May 14 '17 at 22:27
1

This script assumes all files are in the format in the question.

#!/bin/sh
#
# Important! This assumes all files are in the following format:
#
# Movies/A/After Earth (2013).mkv
# Movies/B/Batman (1989).mkv
# Movies/C/Carry On Sergeant (1958).mkv"

raw=`find ./Movies -type f -name \*.mkv`
# Field split on line return
IFS="
"
for m in $raw
do
    fDate=`stat -f %Sm -t %Y-%m-%d "$m"`
    fName=${m##*/}
    fExt=${m##*\.}
    # Assume every movie is in "Name of Film (YYYY).mkv" format
    movie=${fName%% (*}
    mYear=${fName%%)*}
    mYear=${mYear##*(}
    echo "\"${movie}\", \"${mYear}\", \"${fExt}\", \"${fDate}\""
done

exit

And the result should be like:

"After Earth", "2013", "mkv", "2017-05-14"
"Batman", "1989", "mkv", "2017-05-14"
"Carry On Sergeant", "1958", "mkv", "2017-05-14"
1

Assuming by created date, you mean the last modification time of the file (as reported by ls -l), then with GNU find, you could do (here using RFC 4180 CSV format):

find . -regextype posix-extended \
       -regex '.*\([0-9]{4}\)\.[[:alnum:]]+' \
       -type f \
       -printf '%TF/%f\0' |
  perl -l -0pe '
    s/"/""/g;
    s{(.*)/(.*?)\s*\((\d{4})\)\.([^.]*)$}{"$2",$3,$4,$1\r}s'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.