Wildcard not working in cp command when using exec
I want to copy all the files in all the directories to the parent directory, but I get the error
./img/* is not a directory
For example. Here is the command that I used. Is there a better way to do this?
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
command-line unix find cp
add a comment |
I want to copy all the files in all the directories to the parent directory, but I get the error
./img/* is not a directory
For example. Here is the command that I used. Is there a better way to do this?
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
command-line unix find cp
add a comment |
I want to copy all the files in all the directories to the parent directory, but I get the error
./img/* is not a directory
For example. Here is the command that I used. Is there a better way to do this?
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
command-line unix find cp
I want to copy all the files in all the directories to the parent directory, but I get the error
./img/* is not a directory
For example. Here is the command that I used. Is there a better way to do this?
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
command-line unix find cp
command-line unix find cp
asked Feb 19 '15 at 2:56
m0menim0meni
1255
1255
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Try:
find . -type d -regex './[a-z]*' -exec bash -c 'cp -v "$1"/* ..' Cp {} ;
Discussion
Consider:
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
When bash sees this line, it performs pathname expansion on {}/*
. Since there (typically) is no directory named {}
, the *
is left as a literal *
. This is not what you want. You need pathname expansion to occur after find
has substituted in for {}
. By putting the cp
command in quotes and passing it as an argument to bash -c
, we achieve that goal.
A sample form for bash -c
looks like:
bash -c 'code...' a b c
This tells bash to assign a
to $0
, b
to $1
, c
to $2
and then execute code...
. If an error occurs, the shell uses $0
as the name of the program being executed (in the error message). Above, we chose Cp
as a descriptive name. Also, as above, {}
is substituted for $1
and we use use $1
in the code...
. When used this way, {}
doesn't need to be quoted (but it doesn't hurt if you do); find
handles any escaping that is needed. Inside code...
, however, $1
should be in double-quotes to protect against word splitting and pathname expansion.
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
add a comment |
This might also work
cd your_directory
cp -rp * /absolut_path_to_parent_directory
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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
});
}
});
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%2fsuperuser.com%2fquestions%2f879756%2fwildcard-not-working-in-cp-command-when-using-exec%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try:
find . -type d -regex './[a-z]*' -exec bash -c 'cp -v "$1"/* ..' Cp {} ;
Discussion
Consider:
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
When bash sees this line, it performs pathname expansion on {}/*
. Since there (typically) is no directory named {}
, the *
is left as a literal *
. This is not what you want. You need pathname expansion to occur after find
has substituted in for {}
. By putting the cp
command in quotes and passing it as an argument to bash -c
, we achieve that goal.
A sample form for bash -c
looks like:
bash -c 'code...' a b c
This tells bash to assign a
to $0
, b
to $1
, c
to $2
and then execute code...
. If an error occurs, the shell uses $0
as the name of the program being executed (in the error message). Above, we chose Cp
as a descriptive name. Also, as above, {}
is substituted for $1
and we use use $1
in the code...
. When used this way, {}
doesn't need to be quoted (but it doesn't hurt if you do); find
handles any escaping that is needed. Inside code...
, however, $1
should be in double-quotes to protect against word splitting and pathname expansion.
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
add a comment |
Try:
find . -type d -regex './[a-z]*' -exec bash -c 'cp -v "$1"/* ..' Cp {} ;
Discussion
Consider:
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
When bash sees this line, it performs pathname expansion on {}/*
. Since there (typically) is no directory named {}
, the *
is left as a literal *
. This is not what you want. You need pathname expansion to occur after find
has substituted in for {}
. By putting the cp
command in quotes and passing it as an argument to bash -c
, we achieve that goal.
A sample form for bash -c
looks like:
bash -c 'code...' a b c
This tells bash to assign a
to $0
, b
to $1
, c
to $2
and then execute code...
. If an error occurs, the shell uses $0
as the name of the program being executed (in the error message). Above, we chose Cp
as a descriptive name. Also, as above, {}
is substituted for $1
and we use use $1
in the code...
. When used this way, {}
doesn't need to be quoted (but it doesn't hurt if you do); find
handles any escaping that is needed. Inside code...
, however, $1
should be in double-quotes to protect against word splitting and pathname expansion.
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
add a comment |
Try:
find . -type d -regex './[a-z]*' -exec bash -c 'cp -v "$1"/* ..' Cp {} ;
Discussion
Consider:
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
When bash sees this line, it performs pathname expansion on {}/*
. Since there (typically) is no directory named {}
, the *
is left as a literal *
. This is not what you want. You need pathname expansion to occur after find
has substituted in for {}
. By putting the cp
command in quotes and passing it as an argument to bash -c
, we achieve that goal.
A sample form for bash -c
looks like:
bash -c 'code...' a b c
This tells bash to assign a
to $0
, b
to $1
, c
to $2
and then execute code...
. If an error occurs, the shell uses $0
as the name of the program being executed (in the error message). Above, we chose Cp
as a descriptive name. Also, as above, {}
is substituted for $1
and we use use $1
in the code...
. When used this way, {}
doesn't need to be quoted (but it doesn't hurt if you do); find
handles any escaping that is needed. Inside code...
, however, $1
should be in double-quotes to protect against word splitting and pathname expansion.
Try:
find . -type d -regex './[a-z]*' -exec bash -c 'cp -v "$1"/* ..' Cp {} ;
Discussion
Consider:
find . -type d -regex './[a-z]*' -exec cp -v {}/* .. ;
When bash sees this line, it performs pathname expansion on {}/*
. Since there (typically) is no directory named {}
, the *
is left as a literal *
. This is not what you want. You need pathname expansion to occur after find
has substituted in for {}
. By putting the cp
command in quotes and passing it as an argument to bash -c
, we achieve that goal.
A sample form for bash -c
looks like:
bash -c 'code...' a b c
This tells bash to assign a
to $0
, b
to $1
, c
to $2
and then execute code...
. If an error occurs, the shell uses $0
as the name of the program being executed (in the error message). Above, we chose Cp
as a descriptive name. Also, as above, {}
is substituted for $1
and we use use $1
in the code...
. When used this way, {}
doesn't need to be quoted (but it doesn't hurt if you do); find
handles any escaping that is needed. Inside code...
, however, $1
should be in double-quotes to protect against word splitting and pathname expansion.
edited Jan 7 at 18:10
Scott
15.8k113990
15.8k113990
answered Feb 19 '15 at 3:08
John1024John1024
12.7k43433
12.7k43433
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
add a comment |
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
Thank you very much! Could you explain why it works now?
– m0meni
Feb 19 '15 at 3:11
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
@AR7 I just updated the answer. Let me know if that helps.
– John1024
Feb 19 '15 at 3:13
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
It helps very much. You've made my day.
– m0meni
Feb 19 '15 at 3:15
add a comment |
This might also work
cd your_directory
cp -rp * /absolut_path_to_parent_directory
add a comment |
This might also work
cd your_directory
cp -rp * /absolut_path_to_parent_directory
add a comment |
This might also work
cd your_directory
cp -rp * /absolut_path_to_parent_directory
This might also work
cd your_directory
cp -rp * /absolut_path_to_parent_directory
edited Feb 19 '15 at 11:57
ChrisF
38.4k1388139
38.4k1388139
answered Feb 19 '15 at 10:01
ryderryder
1206
1206
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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%2fsuperuser.com%2fquestions%2f879756%2fwildcard-not-working-in-cp-command-when-using-exec%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