Find only newest file in a directory, then move it

I read the UNIX Find a File Command article, but am looking for something even more specific — using the find command.

I can get the filename via ls
ls -1tT ~/desktop/. | head -1

that I can turn into
mv ls -1tT ~/desktop/*.* | head -1 /users/username/dropbox/subfolder/
using back-quotes around ls -1tT ~/desktop/ . | head -1

NOTE: back-quotes don’t seem to work in the editor

but I’ve seen a number of scattered pages that suggest not to use the ls — recommending find instead, but

  • not showing me how to do this same thing with the find command
  • and not saying why the ls method isn’t suggested

Insight? Ideas?

THANKS in advance.

The ls may break with special file names. Hence, you must use the find command. Here is your ls command example:

## get file name and store in $file ##
file="$(ls -t ~/Desktop/ | head -1)"
echo "$file"
mv "$file"  /users/username/dropbox/subfolder/
1 Like

Thanks nixcraft… now I see!


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki