Shell, copy files with similar names












1















I would like to copy a series of similar files from the current directory to the target directory, the files under the current directory are:



prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_uz.hst


Where sim is from sim0001 to sim0500 and f is from f0001 to f0009. I only need f0002, f0005 and f0008. I write the following code:



target_dir="projects/data"

for i in {0001..0500}; do
for s in f000{2,5,8}; do
files="[*]$i[*]$s[*]"
cp $files target_dir
done
done


I am very new to Shell, and wondering how to write the $files="[*]$i[*]$s[*]"$, so that it could match only the f0002, f0005 and f0008. The reason why I also use for i in {0001..0500}; do is that the files are too large and I would like to make sure I could access some completed ones (for example, including all sim0001) in the beginning.



Edit: changed for s in f0002 f0005 f0008; do to f000{2,5,8}.










share|improve this question

























  • What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

    – ghoti
    Nov 21 '18 at 2:31


















1















I would like to copy a series of similar files from the current directory to the target directory, the files under the current directory are:



prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_uz.hst


Where sim is from sim0001 to sim0500 and f is from f0001 to f0009. I only need f0002, f0005 and f0008. I write the following code:



target_dir="projects/data"

for i in {0001..0500}; do
for s in f000{2,5,8}; do
files="[*]$i[*]$s[*]"
cp $files target_dir
done
done


I am very new to Shell, and wondering how to write the $files="[*]$i[*]$s[*]"$, so that it could match only the f0002, f0005 and f0008. The reason why I also use for i in {0001..0500}; do is that the files are too large and I would like to make sure I could access some completed ones (for example, including all sim0001) in the beginning.



Edit: changed for s in f0002 f0005 f0008; do to f000{2,5,8}.










share|improve this question

























  • What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

    – ghoti
    Nov 21 '18 at 2:31
















1












1








1








I would like to copy a series of similar files from the current directory to the target directory, the files under the current directory are:



prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_uz.hst


Where sim is from sim0001 to sim0500 and f is from f0001 to f0009. I only need f0002, f0005 and f0008. I write the following code:



target_dir="projects/data"

for i in {0001..0500}; do
for s in f000{2,5,8}; do
files="[*]$i[*]$s[*]"
cp $files target_dir
done
done


I am very new to Shell, and wondering how to write the $files="[*]$i[*]$s[*]"$, so that it could match only the f0002, f0005 and f0008. The reason why I also use for i in {0001..0500}; do is that the files are too large and I would like to make sure I could access some completed ones (for example, including all sim0001) in the beginning.



Edit: changed for s in f0002 f0005 f0008; do to f000{2,5,8}.










share|improve this question
















I would like to copy a series of similar files from the current directory to the target directory, the files under the current directory are:



prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0001_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0002_uz.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_ux.hst
prod07_sim0500-W31-0.2_velocity-models-2D_t80_f0003_uz.hst


Where sim is from sim0001 to sim0500 and f is from f0001 to f0009. I only need f0002, f0005 and f0008. I write the following code:



target_dir="projects/data"

for i in {0001..0500}; do
for s in f000{2,5,8}; do
files="[*]$i[*]$s[*]"
cp $files target_dir
done
done


I am very new to Shell, and wondering how to write the $files="[*]$i[*]$s[*]"$, so that it could match only the f0002, f0005 and f0008. The reason why I also use for i in {0001..0500}; do is that the files are too large and I would like to make sure I could access some completed ones (for example, including all sim0001) in the beginning.



Edit: changed for s in f0002 f0005 f0008; do to f000{2,5,8}.







bash shell glob






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 2:05









l0b0

34.5k1586149




34.5k1586149










asked Nov 20 '18 at 23:16









Panfeng LiPanfeng Li

1,1301025




1,1301025













  • What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

    – ghoti
    Nov 21 '18 at 2:31





















  • What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

    – ghoti
    Nov 21 '18 at 2:31



















What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

– ghoti
Nov 21 '18 at 2:31







What about cp *sim0[0-5][0-9][0-9]-*_f000[258]_* /target/ ? Fill in globs as required, of course... Might barf if you have more files than ARG_MAX, but I don't see the need for loops. (Yes, I know this also matches sim0000.. If that's a concern, split it into two commands. Still less typing and headaches than nested loops.)

– ghoti
Nov 21 '18 at 2:31














1 Answer
1






active

oldest

votes


















3














What you need is globbing and a bit different quoting:



cp *"$i"*"$s"* "$target_dir"


Not storing this in a variable is intentional - it's faster and it's safe. If you end up with such a large list of files that you start running into system limits you'll have to look into xargs.






share|improve this answer
























  • Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

    – tripleee
    Nov 21 '18 at 5:26











  • Both of those (and more) should be very well explained by the linked articles.

    – l0b0
    Nov 21 '18 at 10:02











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403054%2fshell-copy-files-with-similar-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














What you need is globbing and a bit different quoting:



cp *"$i"*"$s"* "$target_dir"


Not storing this in a variable is intentional - it's faster and it's safe. If you end up with such a large list of files that you start running into system limits you'll have to look into xargs.






share|improve this answer
























  • Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

    – tripleee
    Nov 21 '18 at 5:26











  • Both of those (and more) should be very well explained by the linked articles.

    – l0b0
    Nov 21 '18 at 10:02
















3














What you need is globbing and a bit different quoting:



cp *"$i"*"$s"* "$target_dir"


Not storing this in a variable is intentional - it's faster and it's safe. If you end up with such a large list of files that you start running into system limits you'll have to look into xargs.






share|improve this answer
























  • Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

    – tripleee
    Nov 21 '18 at 5:26











  • Both of those (and more) should be very well explained by the linked articles.

    – l0b0
    Nov 21 '18 at 10:02














3












3








3







What you need is globbing and a bit different quoting:



cp *"$i"*"$s"* "$target_dir"


Not storing this in a variable is intentional - it's faster and it's safe. If you end up with such a large list of files that you start running into system limits you'll have to look into xargs.






share|improve this answer













What you need is globbing and a bit different quoting:



cp *"$i"*"$s"* "$target_dir"


Not storing this in a variable is intentional - it's faster and it's safe. If you end up with such a large list of files that you start running into system limits you'll have to look into xargs.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 2:06









l0b0l0b0

34.5k1586149




34.5k1586149













  • Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

    – tripleee
    Nov 21 '18 at 5:26











  • Both of those (and more) should be very well explained by the linked articles.

    – l0b0
    Nov 21 '18 at 10:02



















  • Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

    – tripleee
    Nov 21 '18 at 5:26











  • Both of those (and more) should be very well explained by the linked articles.

    – l0b0
    Nov 21 '18 at 10:02

















Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

– tripleee
Nov 21 '18 at 5:26





Maybe emphasize that both quoting the wildcard and embedding it in square brackets individually turns the wildcard into a literal *. You have to remove both the quotes and the square brackets before you get an actual wildcard.

– tripleee
Nov 21 '18 at 5:26













Both of those (and more) should be very well explained by the linked articles.

– l0b0
Nov 21 '18 at 10:02





Both of those (and more) should be very well explained by the linked articles.

– l0b0
Nov 21 '18 at 10:02




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403054%2fshell-copy-files-with-similar-names%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings