1

I'd like to copy my 3 most recently downloaded files to another directory using Git Bash. I can list them using

ls -t ~/Downloads|head -n 3

, but how do I pass them as input to the cp function so that I can copy them to some other directory?

I'm pretty new to programming and Bash, so my apologies if this is obvious.

Roland Weisleder
  • 9,668
  • 7
  • 37
  • 59
Raw Noob
  • 81
  • 1
  • 3

1 Answers1

1

Note: you don't need git bash to copy those files: if you have added <git>/usr/bin to your %PATH% (as I do here, with this simplified PATH), you can do that Linux command sequence in any regular CMD session:

vonc@VONC D:\git\git
> where xargs
D:\prgs\git\latest\usr\bin\xargs.exe

vonc@VONC D:\git\git
> ls -t|head -3|xargs | xargs -I\{\} echo  "{}" a
wt-status.h wt-status.c worktree.h a

See also "pass output as an argument for cp in bash", which mentions xargs.
The other option (cp $(ls -t | head -3) Directory) would have to be in a bash only.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250