1

I'm trying to upload videos to YouTube via a Bash shell script. I want to run this script for a specific folder where, when the script is run it, should select only the latest file inside this folder.

I have written the code to upload from shell, but need help to select the latest file automatically.

#!/bin/sh

# youtube-upload


read -p 'Title (default filename): ' TITLE # defaults to filename
read -p 'Privacy (public | unlisted | private): ' PRIVACY # defaults to private
read -p 'Video link:'  LINK


PRIVACY="--privacy ${PRIVACY:-private}"
CREDENTIALS='--client-secrets=/home/thanos/Desktop/client_secrets.json'




VIDEO_ID="$(youtube-upload --title="$TITLE" $PRIVACY $CREDENTIALS $LINK)"

VIDEO_LINK="https://www.youtube.com/watch?v=$VIDEO_ID"
echo "$VIDEO_LINK" > ~/Desktop/Upload-Links/"$(date +%d%m%Y-%H%M%S).txt"


read -p "Upload Complete.  Press any key to continue."`
laurent
  • 88,262
  • 77
  • 290
  • 428
TH4N0S
  • 11
  • 1
  • 1
    Possible duplicate of [Linux: Most recent file in a directory](https://stackoverflow.com/questions/1015678/linux-most-recent-file-in-a-directory) – bishop Jun 15 '17 at 14:24
  • See also [BashFAQ/003](http://mywiki.wooledge.org/BashFAQ/003) and [BashFAQ/099](http://mywiki.wooledge.org/BashFAQ/099): "How can I sort or compare files based on some metadata attribute (newest / oldest modification time, size, etc)?" and "How can I get the newest (or oldest) file from a directory?" – Benjamin W. Jun 15 '17 at 14:26
  • use `stat -c "%Y %n" *` – P.... Jun 15 '17 at 14:34

1 Answers1

1

This would give you the latest modified file:

LAST_MOD_FILE="$(ls -1t | head -1)"
echo $LAST_MOD_FILE
laurent
  • 88,262
  • 77
  • 290
  • 428
  • Please dont mind me asking as im just a beginner, in the code that you have given me, where is it that i would be mentioning the folder path – TH4N0S Jun 15 '17 at 14:38
  • It would be in the `ls` command, as in `ls -1t /path/to/your-folder/ | head -1` – laurent Jun 15 '17 at 15:09
  • Thanks a lot for your help... I'm very close in making it possible. Will update wen i do it – TH4N0S Jun 15 '17 at 15:22
  • Hi this.lau, i have tried the code you had provided and it worked. the question i have is, im trying to implement the same functionality in node.js. what are my options? – TH4N0S Jun 22 '17 at 12:12
  • Hi @TH4N0s, you might want to create a new question about this to get more answers. I suspect you'll simply have to call [fs.readdir](https://nodejs.org/api/fs.html#fs_fs_readdir_path_options_callback) and search for the most recent file in there. – laurent Jun 22 '17 at 12:38