Copy the first n files from one directory to another
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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
switchexec
in a few different configurations
This 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!
terminal command-line bash copy-paste
add a comment |Â
up vote
3
down vote
favorite
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
switchexec
in a few different configurations
This 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!
terminal command-line bash copy-paste
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
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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
switchexec
in a few different configurations
This 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!
terminal command-line bash copy-paste
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
switchexec
in a few different configurations
This 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!
terminal command-line bash copy-paste
terminal command-line bash copy-paste
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
add a comment |Â
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
add a comment |Â
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
Excellent solution, just what I was after! Could you explain the 'n' inhead -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
 |Â
show 4 more comments
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)
2
I'll suggest the you setIFS
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 withIFS=
it would beIFS=; 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
add a comment |Â
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
Excellent solution, just what I was after! Could you explain the 'n' inhead -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
 |Â
show 4 more comments
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
Excellent solution, just what I was after! Could you explain the 'n' inhead -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
 |Â
show 4 more comments
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
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
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' inhead -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
 |Â
show 4 more comments
Excellent solution, just what I was after! Could you explain the 'n' inhead -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
 |Â
show 4 more comments
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)
2
I'll suggest the you setIFS
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 withIFS=
it would beIFS=; 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
add a comment |Â
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)
2
I'll suggest the you setIFS
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 withIFS=
it would beIFS=; 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
add a comment |Â
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)
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)
edited Sep 13 at 17:32
answered Sep 13 at 16:18
user3439894
25.4k63655
25.4k63655
2
I'll suggest the you setIFS
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 withIFS=
it would beIFS=; 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
add a comment |Â
2
I'll suggest the you setIFS
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 withIFS=
it would beIFS=; 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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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