Im looking to copy the first 'n' files from one directory to another directory preferably with only cli tools (no scripts).
I've tried the following:
-
find . -maxdepth 1 -type f | head -5 | xargs cp -t /target/directory
This looked promising, but failed because osx
cp
command doesn't appear to have the
-t
switch -
exec
in a few different configurationsThis probably failed for syntax problems on my end : /
I couldn't seem to get ahead
type selection working
Any help or suggestions would be appreciated.
Thanks in advance!
Best Answer
You need the
-J
option withxargs
.The
J
option places all the filenames into the placeholder X, which can be any character(s) andcp
accepts multiple files to a target directory. It can be visualized as-EDIT:
To handle filenames with spaces, we have
tr
translate the newline character to the null character after each filename and then have xargs handle the null bit as a separator for the filenames.