Copy the first n files from one directory to another

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












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 configurations




    This probably failed for syntax problems on my end : /

    I couldn't seem to get a head type selection working




Any help or suggestions would be appreciated.



Thanks in advance!










share|improve this question





















  • Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
    – jmh
    Sep 13 at 15:53










  • No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
    – jmh
    Sep 13 at 16:19














up vote
3
down vote

favorite
1












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 configurations




    This probably failed for syntax problems on my end : /

    I couldn't seem to get a head type selection working




Any help or suggestions would be appreciated.



Thanks in advance!










share|improve this question





















  • Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
    – jmh
    Sep 13 at 15:53










  • No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
    – jmh
    Sep 13 at 16:19












up vote
3
down vote

favorite
1









up vote
3
down vote

favorite
1






1





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 configurations




    This probably failed for syntax problems on my end : /

    I couldn't seem to get a head type selection working




Any help or suggestions would be appreciated.



Thanks in advance!










share|improve this question













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 configurations




    This probably failed for syntax problems on my end : /

    I couldn't seem to get a head type selection working




Any help or suggestions would be appreciated.



Thanks in advance!







terminal command-line bash copy-paste






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 13 at 15:26









visyoual

528




528











  • Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
    – jmh
    Sep 13 at 15:53










  • No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
    – jmh
    Sep 13 at 16:19
















  • Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
    – jmh
    Sep 13 at 15:53










  • No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
    – jmh
    Sep 13 at 16:19















Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
– jmh
Sep 13 at 15:53




Can you give a few examples of the file names you wish to copy? Is there anything distinguishing about the names other than the extension.
– jmh
Sep 13 at 15:53












No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
– jmh
Sep 13 at 16:19




No but it may have an effect on what commands work. It could be a very simple answer. Is n all the files in the folder or is it n out of m you want to copy.
– jmh
Sep 13 at 16:19










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










You need the -J option with xargs.



find . -maxdepth 1 -type f | head -n5 | xargs -J X cp X /target/directory


The J option places all the filenames into the placeholder X, which can be any character(s) and cp accepts multiple files to a target directory. It can be visualized as-



cp file1 file2 file3 file4 file5 DESTINATION


EDIT:



To handle filenames with spaces, we have find print the null character after each filename and then have xargs handle the null bit.



 find . -maxdepth 1 -type f -print0 | head -n5 | xargs -0 -J X cp X /target/directory





share|improve this answer






















  • Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
    – visyoual
    Sep 13 at 17:21











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    Sep 13 at 17:30










  • Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
    – visyoual
    Sep 13 at 17:33











  • As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
    – user3439894
    Sep 13 at 17:37







  • 1




    @visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
    – fd0
    Sep 13 at 17:45

















up vote
0
down vote













I found a different solution without xargs or -exec but I think fd0's answer is a better way to go:



while IFS= read -r f; do cp "$f" "/target/directory/"; done < <(find . -maxdepth 1 -type f | head -n5)





share|improve this answer


















  • 2




    I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
    – fd0
    Sep 13 at 16:38










  • @fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
    – user3439894
    Sep 13 at 16:57










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    Sep 13 at 17:06










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "118"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f336427%2fcopy-the-first-n-files-from-one-directory-to-another%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
4
down vote



accepted










You need the -J option with xargs.



find . -maxdepth 1 -type f | head -n5 | xargs -J X cp X /target/directory


The J option places all the filenames into the placeholder X, which can be any character(s) and cp accepts multiple files to a target directory. It can be visualized as-



cp file1 file2 file3 file4 file5 DESTINATION


EDIT:



To handle filenames with spaces, we have find print the null character after each filename and then have xargs handle the null bit.



 find . -maxdepth 1 -type f -print0 | head -n5 | xargs -0 -J X cp X /target/directory





share|improve this answer






















  • Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
    – visyoual
    Sep 13 at 17:21











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    Sep 13 at 17:30










  • Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
    – visyoual
    Sep 13 at 17:33











  • As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
    – user3439894
    Sep 13 at 17:37







  • 1




    @visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
    – fd0
    Sep 13 at 17:45














up vote
4
down vote



accepted










You need the -J option with xargs.



find . -maxdepth 1 -type f | head -n5 | xargs -J X cp X /target/directory


The J option places all the filenames into the placeholder X, which can be any character(s) and cp accepts multiple files to a target directory. It can be visualized as-



cp file1 file2 file3 file4 file5 DESTINATION


EDIT:



To handle filenames with spaces, we have find print the null character after each filename and then have xargs handle the null bit.



 find . -maxdepth 1 -type f -print0 | head -n5 | xargs -0 -J X cp X /target/directory





share|improve this answer






















  • Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
    – visyoual
    Sep 13 at 17:21











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    Sep 13 at 17:30










  • Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
    – visyoual
    Sep 13 at 17:33











  • As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
    – user3439894
    Sep 13 at 17:37







  • 1




    @visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
    – fd0
    Sep 13 at 17:45












up vote
4
down vote



accepted







up vote
4
down vote



accepted






You need the -J option with xargs.



find . -maxdepth 1 -type f | head -n5 | xargs -J X cp X /target/directory


The J option places all the filenames into the placeholder X, which can be any character(s) and cp accepts multiple files to a target directory. It can be visualized as-



cp file1 file2 file3 file4 file5 DESTINATION


EDIT:



To handle filenames with spaces, we have find print the null character after each filename and then have xargs handle the null bit.



 find . -maxdepth 1 -type f -print0 | head -n5 | xargs -0 -J X cp X /target/directory





share|improve this answer














You need the -J option with xargs.



find . -maxdepth 1 -type f | head -n5 | xargs -J X cp X /target/directory


The J option places all the filenames into the placeholder X, which can be any character(s) and cp accepts multiple files to a target directory. It can be visualized as-



cp file1 file2 file3 file4 file5 DESTINATION


EDIT:



To handle filenames with spaces, we have find print the null character after each filename and then have xargs handle the null bit.



 find . -maxdepth 1 -type f -print0 | head -n5 | xargs -0 -J X cp X /target/directory






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 13 at 17:55

























answered Sep 13 at 16:01









fd0

5,64011327




5,64011327











  • Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
    – visyoual
    Sep 13 at 17:21











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    Sep 13 at 17:30










  • Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
    – visyoual
    Sep 13 at 17:33











  • As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
    – user3439894
    Sep 13 at 17:37







  • 1




    @visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
    – fd0
    Sep 13 at 17:45
















  • Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
    – visyoual
    Sep 13 at 17:21











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    Sep 13 at 17:30










  • Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
    – visyoual
    Sep 13 at 17:33











  • As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
    – user3439894
    Sep 13 at 17:37







  • 1




    @visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
    – fd0
    Sep 13 at 17:45















Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
– visyoual
Sep 13 at 17:21





Excellent solution, just what I was after! Could you explain the 'n' in head -n5? At any rate, big thanks, and thanks @user3439894 for your input as well!
– visyoual
Sep 13 at 17:21













@visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
– fd0
Sep 13 at 17:30




@visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
– fd0
Sep 13 at 17:30












Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
– visyoual
Sep 13 at 17:33





Ok. In my search for other solutions prior to posting this questions, several had the head command with -5 and not -n5. Wasn't sure if that was acceptable shorthand, or syntax error. I guess i'm still not sure.
– visyoual
Sep 13 at 17:33













As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
– user3439894
Sep 13 at 17:37





As you mentioned to me in a comment to my answer, your solution does not handle filenames with spaces, it there a way to achieve it modifying your command slightly?
– user3439894
Sep 13 at 17:37





1




1




@visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
– fd0
Sep 13 at 17:45




@visyoual - depends on the version of head that you are using, With GNU head -n5 would mean the first five lines and -5 would mean all but the last five lines. With BSD head both are the same.
– fd0
Sep 13 at 17:45












up vote
0
down vote













I found a different solution without xargs or -exec but I think fd0's answer is a better way to go:



while IFS= read -r f; do cp "$f" "/target/directory/"; done < <(find . -maxdepth 1 -type f | head -n5)





share|improve this answer


















  • 2




    I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
    – fd0
    Sep 13 at 16:38










  • @fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
    – user3439894
    Sep 13 at 16:57










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    Sep 13 at 17:06














up vote
0
down vote













I found a different solution without xargs or -exec but I think fd0's answer is a better way to go:



while IFS= read -r f; do cp "$f" "/target/directory/"; done < <(find . -maxdepth 1 -type f | head -n5)





share|improve this answer


















  • 2




    I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
    – fd0
    Sep 13 at 16:38










  • @fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
    – user3439894
    Sep 13 at 16:57










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    Sep 13 at 17:06












up vote
0
down vote










up vote
0
down vote









I found a different solution without xargs or -exec but I think fd0's answer is a better way to go:



while IFS= read -r f; do cp "$f" "/target/directory/"; done < <(find . -maxdepth 1 -type f | head -n5)





share|improve this answer














I found a different solution without xargs or -exec but I think fd0's answer is a better way to go:



while IFS= read -r f; do cp "$f" "/target/directory/"; done < <(find . -maxdepth 1 -type f | head -n5)






share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 13 at 17:32

























answered Sep 13 at 16:18









user3439894

25.4k63655




25.4k63655







  • 2




    I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
    – fd0
    Sep 13 at 16:38










  • @fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
    – user3439894
    Sep 13 at 16:57










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    Sep 13 at 17:06












  • 2




    I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
    – fd0
    Sep 13 at 16:38










  • @fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
    – user3439894
    Sep 13 at 16:57










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    Sep 13 at 17:06







2




2




I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
– fd0
Sep 13 at 16:38




I'll suggest the you set IFS to nothing-IFS= to handle any leading or trailing spaces in a filename and use process substitution instead of command substitution and a here string. Your solution will definitely be slower but it will handle filenames with spaces in them. My solution will not in its current form.
– fd0
Sep 13 at 16:38












@fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
– user3439894
Sep 13 at 16:57




@fd0, Good suggestion. I know with IFS= it would be IFS=; while read ... but not sure how to swap out the command substitution and here string for process substitution instead, without doing some research. Would not mind you showing me or even editing my answer, thanks.
– user3439894
Sep 13 at 16:57












Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
– user3439894
Sep 13 at 17:06




Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
– user3439894
Sep 13 at 17:06

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fapple.stackexchange.com%2fquestions%2f336427%2fcopy-the-first-n-files-from-one-directory-to-another%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay