Scp the Three Newest Files Using Bash
perhaps the simplest solution, but it does not deal with spaces in filenames
scp `ls -t | head -3` user@server:.
using xargs has the advantage of dealing with spaces in file names, but will execute scp three times
ls -t | head -3 | xargs -i scp {} user@server:.
a loop based solution would look like this. We use while read here because the default delimiter for read is the newline character not the space character like the for loop
ls -t | head -3 | while read file ; do scp $file user@server ; done
saddly, the perfect solution, one which executes a single scp command while working nicely with white space, eludes me at the moment.
Try this script to scp latest 3 files from supplied 1st argument path to this script:
#!/bin/bashDIR="$1"for f in $(ls -t `find ${DIR} -maxdepth 1 -type f` | head -3) do scp ${f} user@host:destinationDirectorydone
find -type f
makes sure only files are found in ${DIR} and head -3
takes top 3 files.