Copy files with match prefix AND suffix with shell script
Clash Royale CLAN TAG#URR8PPP
I have a directory with files such as
aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png
Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.
shell cp
add a comment |
I have a directory with files such as
aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png
Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.
shell cp
add a comment |
I have a directory with files such as
aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png
Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.
shell cp
I have a directory with files such as
aaaXXXbbb.png
aaaYYYccc.png
xxxAAAyyy.png
yyyAAAxxx.png
Now I want to copy all files with prefix 'aaa' and suffix '.png' to a new directory with shell script, let say 2 files 'aaaXXXbbb.png' and 'aaaYYYccc.png'.
shell cp
shell cp
edited Jan 22 at 2:50
Rui F Ribeiro
40k1479135
40k1479135
asked Jan 22 at 0:11
user2842390user2842390
33
33
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can do this with a one liner:
grep '^aaa.*.png$' list.txt | xargs -I '' cp '' destination_dir/
grep
is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp
which moves them to 'destination_dir'
If you are in the directory of the files you can just cp
them with:
cp aaa*.png destination_dir
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar withcp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
@user1794469 And it still states that he is looking to copy files. The only way forgrep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available.
– Nasir Riley
Jan 22 at 0:30
|
show 1 more comment
cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
With find
:
find . -maxdepth 1 -type f -name aaa*.png -exec cp destination ;
.
indicates the current directory.
-maxdepth 1
tells it to only look in the current directory
type -f
tells it to look for files
-name aaa*.png
indicates files beginning with aaa
and ending in .png
-exec cp destination ;
copies the files into the directory called destination
.
My environment requires me to escape the *
with a . Yours may not require this so you may be able to just use
aaa*.png
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495878%2fcopy-files-with-match-prefix-and-suffix-with-shell-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do this with a one liner:
grep '^aaa.*.png$' list.txt | xargs -I '' cp '' destination_dir/
grep
is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp
which moves them to 'destination_dir'
If you are in the directory of the files you can just cp
them with:
cp aaa*.png destination_dir
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar withcp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
@user1794469 And it still states that he is looking to copy files. The only way forgrep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available.
– Nasir Riley
Jan 22 at 0:30
|
show 1 more comment
You can do this with a one liner:
grep '^aaa.*.png$' list.txt | xargs -I '' cp '' destination_dir/
grep
is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp
which moves them to 'destination_dir'
If you are in the directory of the files you can just cp
them with:
cp aaa*.png destination_dir
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar withcp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
@user1794469 And it still states that he is looking to copy files. The only way forgrep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available.
– Nasir Riley
Jan 22 at 0:30
|
show 1 more comment
You can do this with a one liner:
grep '^aaa.*.png$' list.txt | xargs -I '' cp '' destination_dir/
grep
is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp
which moves them to 'destination_dir'
If you are in the directory of the files you can just cp
them with:
cp aaa*.png destination_dir
You can do this with a one liner:
grep '^aaa.*.png$' list.txt | xargs -I '' cp '' destination_dir/
grep
is looking for 'aaa' at the start of the line followed by zero or more characters and ending with '.png'. It then pipes that as a list of arguments to cp
which moves them to 'destination_dir'
If you are in the directory of the files you can just cp
them with:
cp aaa*.png destination_dir
edited Jan 22 at 0:19
answered Jan 22 at 0:16
user1794469user1794469
1,5801822
1,5801822
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar withcp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
@user1794469 And it still states that he is looking to copy files. The only way forgrep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available.
– Nasir Riley
Jan 22 at 0:30
|
show 1 more comment
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar withcp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
@user1794469 And it still states that he is looking to copy files. The only way forgrep
to apply would be the put the names into a text file before usinggrep
which isn't necessary whenfind
is already available.
– Nasir Riley
Jan 22 at 0:30
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
Thank you very much, if I'm in a directory instead of getting a list.txt and all files which is in the list.txt is in the current dir, what would be the right command to do the job?
– user2842390
Jan 22 at 0:18
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with
cp
– user1794469
Jan 22 at 0:21
That's not what you asked for ;) If you need to copy the files in the current directory you would do something similar with
cp
– user1794469
Jan 22 at 0:21
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@user1794469 That is what he asked for. The question states that he is trying to find and copy files. There's no need to put the filenames into a file and then grep for them.
– Nasir Riley
Jan 22 at 0:25
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
@NasirRiley he edited the question.
– user1794469
Jan 22 at 0:27
1
1
@user1794469 And it still states that he is looking to copy files. The only way for
grep
to apply would be the put the names into a text file before using grep
which isn't necessary when find
is already available.– Nasir Riley
Jan 22 at 0:30
@user1794469 And it still states that he is looking to copy files. The only way for
grep
to apply would be the put the names into a text file before using grep
which isn't necessary when find
is already available.– Nasir Riley
Jan 22 at 0:30
|
show 1 more comment
cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
cp aaa*.png /some/destdir
This would match all filenames starting with the string aaa
and ending in the string .png
and copy them all to the directory /some/destdir
. The *
would match any number of any characters in the middle of the name.
This would fail if you had many thousands of files matching the pattern, since the generated list would be too long.
In that case, use something like the following loop:
for name in aaa*.png; do
cp "$name" /some/destdir
done
This would copy the files one by one.
A more efficient method for many thousands of files would be (using GNU cp
with its -t
option):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec cp -t /some/destdir +
Or (without GNU cp
):
find . -maxdepth 1 -type f -name 'aaa*.png' -exec sh -c 'cp "$@" /some/destdir' sh +
This last find
command would find all regular files (-type f
) under the current directory (only, due to -maxdepth 1
) whose names matches the pattern aaa*.png
, and for batches of these it would call a short in-line shell script. The short in-line shell script would simply copy the files in the current batch (which would be a reasonable and managable number of files) to the destination directory.
More on find
using -exec
: Understanding the -exec option of `find`
edited Jan 22 at 0:31
answered Jan 22 at 0:21
KusalanandaKusalananda
129k16242399
129k16242399
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
Thank you very much!
– user2842390
Jan 22 at 1:36
Thank you very much!
– user2842390
Jan 22 at 1:36
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
With find
:
find . -maxdepth 1 -type f -name aaa*.png -exec cp destination ;
.
indicates the current directory.
-maxdepth 1
tells it to only look in the current directory
type -f
tells it to look for files
-name aaa*.png
indicates files beginning with aaa
and ending in .png
-exec cp destination ;
copies the files into the directory called destination
.
My environment requires me to escape the *
with a . Yours may not require this so you may be able to just use
aaa*.png
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
With find
:
find . -maxdepth 1 -type f -name aaa*.png -exec cp destination ;
.
indicates the current directory.
-maxdepth 1
tells it to only look in the current directory
type -f
tells it to look for files
-name aaa*.png
indicates files beginning with aaa
and ending in .png
-exec cp destination ;
copies the files into the directory called destination
.
My environment requires me to escape the *
with a . Yours may not require this so you may be able to just use
aaa*.png
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
With find
:
find . -maxdepth 1 -type f -name aaa*.png -exec cp destination ;
.
indicates the current directory.
-maxdepth 1
tells it to only look in the current directory
type -f
tells it to look for files
-name aaa*.png
indicates files beginning with aaa
and ending in .png
-exec cp destination ;
copies the files into the directory called destination
.
My environment requires me to escape the *
with a . Yours may not require this so you may be able to just use
aaa*.png
With find
:
find . -maxdepth 1 -type f -name aaa*.png -exec cp destination ;
.
indicates the current directory.
-maxdepth 1
tells it to only look in the current directory
type -f
tells it to look for files
-name aaa*.png
indicates files beginning with aaa
and ending in .png
-exec cp destination ;
copies the files into the directory called destination
.
My environment requires me to escape the *
with a . Yours may not require this so you may be able to just use
aaa*.png
edited Jan 22 at 0:27
answered Jan 22 at 0:22
Nasir RileyNasir Riley
2,654249
2,654249
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
Thank you very much!
– user2842390
Jan 22 at 1:36
Thank you very much!
– user2842390
Jan 22 at 1:36
Thank you very much!
– user2842390
Jan 22 at 1:36
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f495878%2fcopy-files-with-match-prefix-and-suffix-with-shell-script%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown