Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

What would be the easiest way to go about rsyncing the n newest files in a directory to a remote server?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

The easiest way is to run zsh instead of bash.

rsync -a -- /path/to/directory/*(om[1,42]) remote-server:

In the parentheses, om orders files by reverse modification time (i.e. by increasing age), and [1,42] selects the first 42 matches.

If you want only regular files and not directories, add a . after the opening parenthesis. For more possibilities, look under “glob qualifiers” in the zshexpn man page.

If you want to match files in the directory tree rooted at /path/to/directory, rather than just inside that directory, use /path/to/directory/**/*(.om[1,42]). This will send all the files to the same directory on the target, though.

share|improve this answer

Assuming you want to send files from the current working directory:

rsync `ls -tp | grep -v / | head -n <n>` <destination> <options>

will do the trick. For example:

rsync `ls -tp | grep -v / | head -n 10` user@host:/dest/dir/ --progress --compress

This will give an error if there are no files to be found in the current working directory, or if any of the top files contain spaces or other special characters.

The ` characters around ls -tp | grep -v / | head -n <n> tell bash to run the commands and replaced them with the resulting file list as a space separated list. The -t option tells ls to sort by timestamp, the -p tells it to add a / after directory names and the grep part screens out lines ending / so you don't end up sending directories over. Add -c to the ls options if you want the newest files to be judged by creation time instead of modification time (though note that some programs will remove and replace files instead of updating them so ctime and mtime can be the same even though a file seems to have been around longer).

I'll not claim it is without doubt the easiest way but it would be the way I'd first think of.

share|improve this answer
    
Do not use this command unless you know that the file names cannot contain any “special” character (whitespace, \[?*, control characters or non-ASCII characters). –  Gilles Nov 9 '10 at 0:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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