How to rename (move) most recent files in directory?

I'm using cygwin32 on Windows.
DN is an environment variable pointed at my download directory.

This command works to move the single most recent file in my download directory to my current directory:

mv "`perl -e '$p = $ARGV[0]; opendir $h, $p or die "cannot opendir $p: $!"; @f = sort {  -M $a <=> -M $b } map  { "$p/$_" } readdir $h; closedir $h;@f=grep(!/\\.$/,@f); foreach $a (@f){$a=~s/\\\\/\\//g;$a=lcfirst $a;$a=~s@^(.):@/cygdrive/$1@;}  $" = "\\n";print "@f[0..0]\\n"; ' $DN`" .

Can someone help me modify this so it moves the top "n" (eg: 6 or 20 or...) most recent files from the download directory to the current directory?

thanks
Siegfried

Unless the filenames in your download directory contain whitespace characters, try:

#!/bin/bash
n=5	# Or whatever number of files you want to move...
dest=${PWD}
cd "$DN"
mv $(ls -t|head -n $n) "$dest"

In theory, all that you need to change

print "@f[0..0]\\n";

to

print "@f[0..$n]\\n";

where $n is the number of files to move -1 (since it starts at 0) within the array boundary.

However, that's a messy flatten Perl command and I would recommend that you would create a proper Perl script and use File::Copy to move the files without the need of the external shell mv command.