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
    3 hours ago










  • 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
    2 hours ago














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
    3 hours ago










  • 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
    2 hours ago












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 3 hours ago









visyoual

477




477











  • 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
    3 hours ago










  • 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
    2 hours ago
















  • 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
    3 hours ago










  • 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
    2 hours ago















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
3 hours ago




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
3 hours ago












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
2 hours ago




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
2 hours ago










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
    1 hour ago











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    1 hour ago










  • 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
    1 hour ago











  • 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
    1 hour ago







  • 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
    1 hour ago

















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
    2 hours ago










  • @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
    2 hours ago










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    2 hours ago










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
    1 hour ago











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    1 hour ago










  • 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
    1 hour ago











  • 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
    1 hour ago







  • 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
    1 hour ago














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
    1 hour ago











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    1 hour ago










  • 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
    1 hour ago











  • 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
    1 hour ago







  • 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
    1 hour ago












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 1 hour ago

























answered 3 hours ago









fd0

5,59011327




5,59011327











  • 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
    1 hour ago











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    1 hour ago










  • 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
    1 hour ago











  • 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
    1 hour ago







  • 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
    1 hour ago
















  • 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
    1 hour ago











  • @visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
    – fd0
    1 hour ago










  • 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
    1 hour ago











  • 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
    1 hour ago







  • 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
    1 hour ago















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
1 hour ago





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
1 hour ago













@visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
– fd0
1 hour ago




@visyoual - From the manual: ` head [-n count | -c bytes] [file ...]`
– fd0
1 hour ago












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
1 hour ago





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
1 hour ago













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
1 hour ago





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
1 hour ago





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
1 hour ago




@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
1 hour ago












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
    2 hours ago










  • @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
    2 hours ago










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    2 hours ago














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
    2 hours ago










  • @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
    2 hours ago










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    2 hours ago












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 1 hour ago

























answered 2 hours ago









user3439894

25.1k63655




25.1k63655







  • 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
    2 hours ago










  • @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
    2 hours ago










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    2 hours ago












  • 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
    2 hours ago










  • @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
    2 hours ago










  • Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
    – user3439894
    2 hours ago







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
2 hours ago




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
2 hours ago












@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
2 hours ago




@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
2 hours ago












Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
– user3439894
2 hours ago




Thanks @fd0, I missed the extra < in ... done < <( ... in my testing. :)
– user3439894
2 hours ago

















 

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













































































Comments

Popular posts from this blog

What does second last employer means? [closed]

Installing NextGIS Connect into QGIS 3?

One-line joke