Skip to content

Instantly share code, notes, and snippets.

@avosa
Last active March 18, 2021 10:51
Show Gist options
  • Save avosa/f5d028e4fa3b60327e9e60529d90ee82 to your computer and use it in GitHub Desktop.
Save avosa/f5d028e4fa3b60327e9e60529d90ee82 to your computer and use it in GitHub Desktop.
Some simple way to get copy newest file from dir using Unix script
#!/bin/bash
echo "Enter directory you want to copy latest file from"
read DEST
function checkForNewestFile(){
NEWEST=`ls -tr1d "${MY_DIR}/"*.${FILEEXT} 2>/dev/null | tail -1`
if [ -z "${NEWEST}" ] ; then
echo "No newest file to copy"
exit 1
elif [ -d "${NEWEST}" ] ; then
echo "The most recent entry is a directory"
exit 1
else
echo "FOUND!!!!Copying ${NEWEST}"
cp -p "${NEWEST}" "${DEST}"
fi
echo " "
{
checkForNewestFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment