How to copy recursive only the files from folders and sub folders?
Under /home folder we have many sub folders
as the following
/home/user1
/home/user2/user_sub_2
/home/user3/user_sub_3/info_sub
/home/user4/INFO_FOLDER
We want to copy all the files under /home recursive to /tmp/calculation folder
What is the right approach to do this action ?
linux bash shell-script find rsync
|
show 1 more comment
Under /home folder we have many sub folders
as the following
/home/user1
/home/user2/user_sub_2
/home/user3/user_sub_3/info_sub
/home/user4/INFO_FOLDER
We want to copy all the files under /home recursive to /tmp/calculation folder
What is the right approach to do this action ?
linux bash shell-script find rsync
You tagged your question with rsync. Does that mean you've tried usingrsync
but failed? Likewise forfind
.
– Kusalananda
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.
– Kusalananda
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago
|
show 1 more comment
Under /home folder we have many sub folders
as the following
/home/user1
/home/user2/user_sub_2
/home/user3/user_sub_3/info_sub
/home/user4/INFO_FOLDER
We want to copy all the files under /home recursive to /tmp/calculation folder
What is the right approach to do this action ?
linux bash shell-script find rsync
Under /home folder we have many sub folders
as the following
/home/user1
/home/user2/user_sub_2
/home/user3/user_sub_3/info_sub
/home/user4/INFO_FOLDER
We want to copy all the files under /home recursive to /tmp/calculation folder
What is the right approach to do this action ?
linux bash shell-script find rsync
linux bash shell-script find rsync
edited 2 hours ago
PRY
2,55831026
2,55831026
asked 3 hours ago
yaelyael
2,60622571
2,60622571
You tagged your question with rsync. Does that mean you've tried usingrsync
but failed? Likewise forfind
.
– Kusalananda
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.
– Kusalananda
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago
|
show 1 more comment
You tagged your question with rsync. Does that mean you've tried usingrsync
but failed? Likewise forfind
.
– Kusalananda
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.
– Kusalananda
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago
You tagged your question with rsync. Does that mean you've tried using
rsync
but failed? Likewise for find
.– Kusalananda
3 hours ago
You tagged your question with rsync. Does that mean you've tried using
rsync
but failed? Likewise for find
.– Kusalananda
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of
/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.– Kusalananda
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of
/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.– Kusalananda
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago
|
show 1 more comment
3 Answers
3
active
oldest
votes
find /home ! -type d -exec bash -c '
for pathname do
if [ "$pathname" -nt "/tmp/calculation/${pathname##*/}" ]
then
cp "$pathname" /tmp/calculation
fi
done' bash {} +
This would find all non-directory files under /home
, and for batches of these it would call a short bash
script.
The short bash
script would loop over the current batch of pathnames, and for each would test with the -nt
test whether current file is newer than the copy in the target directory (or whether a copy does not exist there). If the file in the target directory is older or if it does not exist, cp
is used to copy the current file to the target directory.
Related:
- Understanding the -exec option of `find`
Mostly unrelated:
The -nt
test is a non-standard test. This is why I chose to use bash
for the internal script that find
calls. Using sh -c
instead of bash -c
would probably have worked, but the semantics of the test may differ slightly between shell that may masquerade as sh
.
For example, in the bash
, zsh
and ksh
shells, the -nt
test is true if the first operand has a modification timestamp that in newer than that of the second operand, or if the second operand does not exist.
In the dash
shell, however, both files most exist and the first file has to be newer than the second for the test to be true (according to the documentation). This difference would not have been an issue in this case.
In the yash
shell it's not specified in the manual what happens if either file does not exist.
It is therefore safest to use a specific shell when using a non-standard facility, even if it, in this specific case, would probably have worked with sh -c
anyway.
(The downside with using bash
in this instance is that it only has a one second resolution in the timestamps that it compares, but that's another story)
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used! -type d
not-type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of-type f
I used! -type d
to copy any type of non-directory file.
– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
add a comment |
For RSYNC
Depending on permissions you can simply use the command:
rsync -r /home/ /tmp/calculation
The trailing / on /home/ is important if you want to backup/move the contents of home without moving home itself.
Realistically there are numerous methods to do the function you want:
https://www.linux.com/learn/how-move-files-using-linux-commands-or-file-managers
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
New contributor
rysync
does not move, it copies.
– Kusalananda
3 hours ago
add a comment |
= find =
find /home -type f -exec cp {} /tmp/calculation ;
New contributor
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%2f502979%2fhow-to-copy-recursive-only-the-files-from-folders-and-sub-folders%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
find /home ! -type d -exec bash -c '
for pathname do
if [ "$pathname" -nt "/tmp/calculation/${pathname##*/}" ]
then
cp "$pathname" /tmp/calculation
fi
done' bash {} +
This would find all non-directory files under /home
, and for batches of these it would call a short bash
script.
The short bash
script would loop over the current batch of pathnames, and for each would test with the -nt
test whether current file is newer than the copy in the target directory (or whether a copy does not exist there). If the file in the target directory is older or if it does not exist, cp
is used to copy the current file to the target directory.
Related:
- Understanding the -exec option of `find`
Mostly unrelated:
The -nt
test is a non-standard test. This is why I chose to use bash
for the internal script that find
calls. Using sh -c
instead of bash -c
would probably have worked, but the semantics of the test may differ slightly between shell that may masquerade as sh
.
For example, in the bash
, zsh
and ksh
shells, the -nt
test is true if the first operand has a modification timestamp that in newer than that of the second operand, or if the second operand does not exist.
In the dash
shell, however, both files most exist and the first file has to be newer than the second for the test to be true (according to the documentation). This difference would not have been an issue in this case.
In the yash
shell it's not specified in the manual what happens if either file does not exist.
It is therefore safest to use a specific shell when using a non-standard facility, even if it, in this specific case, would probably have worked with sh -c
anyway.
(The downside with using bash
in this instance is that it only has a one second resolution in the timestamps that it compares, but that's another story)
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used! -type d
not-type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of-type f
I used! -type d
to copy any type of non-directory file.
– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
add a comment |
find /home ! -type d -exec bash -c '
for pathname do
if [ "$pathname" -nt "/tmp/calculation/${pathname##*/}" ]
then
cp "$pathname" /tmp/calculation
fi
done' bash {} +
This would find all non-directory files under /home
, and for batches of these it would call a short bash
script.
The short bash
script would loop over the current batch of pathnames, and for each would test with the -nt
test whether current file is newer than the copy in the target directory (or whether a copy does not exist there). If the file in the target directory is older or if it does not exist, cp
is used to copy the current file to the target directory.
Related:
- Understanding the -exec option of `find`
Mostly unrelated:
The -nt
test is a non-standard test. This is why I chose to use bash
for the internal script that find
calls. Using sh -c
instead of bash -c
would probably have worked, but the semantics of the test may differ slightly between shell that may masquerade as sh
.
For example, in the bash
, zsh
and ksh
shells, the -nt
test is true if the first operand has a modification timestamp that in newer than that of the second operand, or if the second operand does not exist.
In the dash
shell, however, both files most exist and the first file has to be newer than the second for the test to be true (according to the documentation). This difference would not have been an issue in this case.
In the yash
shell it's not specified in the manual what happens if either file does not exist.
It is therefore safest to use a specific shell when using a non-standard facility, even if it, in this specific case, would probably have worked with sh -c
anyway.
(The downside with using bash
in this instance is that it only has a one second resolution in the timestamps that it compares, but that's another story)
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used! -type d
not-type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of-type f
I used! -type d
to copy any type of non-directory file.
– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
add a comment |
find /home ! -type d -exec bash -c '
for pathname do
if [ "$pathname" -nt "/tmp/calculation/${pathname##*/}" ]
then
cp "$pathname" /tmp/calculation
fi
done' bash {} +
This would find all non-directory files under /home
, and for batches of these it would call a short bash
script.
The short bash
script would loop over the current batch of pathnames, and for each would test with the -nt
test whether current file is newer than the copy in the target directory (or whether a copy does not exist there). If the file in the target directory is older or if it does not exist, cp
is used to copy the current file to the target directory.
Related:
- Understanding the -exec option of `find`
Mostly unrelated:
The -nt
test is a non-standard test. This is why I chose to use bash
for the internal script that find
calls. Using sh -c
instead of bash -c
would probably have worked, but the semantics of the test may differ slightly between shell that may masquerade as sh
.
For example, in the bash
, zsh
and ksh
shells, the -nt
test is true if the first operand has a modification timestamp that in newer than that of the second operand, or if the second operand does not exist.
In the dash
shell, however, both files most exist and the first file has to be newer than the second for the test to be true (according to the documentation). This difference would not have been an issue in this case.
In the yash
shell it's not specified in the manual what happens if either file does not exist.
It is therefore safest to use a specific shell when using a non-standard facility, even if it, in this specific case, would probably have worked with sh -c
anyway.
(The downside with using bash
in this instance is that it only has a one second resolution in the timestamps that it compares, but that's another story)
find /home ! -type d -exec bash -c '
for pathname do
if [ "$pathname" -nt "/tmp/calculation/${pathname##*/}" ]
then
cp "$pathname" /tmp/calculation
fi
done' bash {} +
This would find all non-directory files under /home
, and for batches of these it would call a short bash
script.
The short bash
script would loop over the current batch of pathnames, and for each would test with the -nt
test whether current file is newer than the copy in the target directory (or whether a copy does not exist there). If the file in the target directory is older or if it does not exist, cp
is used to copy the current file to the target directory.
Related:
- Understanding the -exec option of `find`
Mostly unrelated:
The -nt
test is a non-standard test. This is why I chose to use bash
for the internal script that find
calls. Using sh -c
instead of bash -c
would probably have worked, but the semantics of the test may differ slightly between shell that may masquerade as sh
.
For example, in the bash
, zsh
and ksh
shells, the -nt
test is true if the first operand has a modification timestamp that in newer than that of the second operand, or if the second operand does not exist.
In the dash
shell, however, both files most exist and the first file has to be newer than the second for the test to be true (according to the documentation). This difference would not have been an issue in this case.
In the yash
shell it's not specified in the manual what happens if either file does not exist.
It is therefore safest to use a specific shell when using a non-standard facility, even if it, in this specific case, would probably have worked with sh -c
anyway.
(The downside with using bash
in this instance is that it only has a one second resolution in the timestamps that it compares, but that's another story)
edited 2 hours ago
answered 3 hours ago
KusalanandaKusalananda
132k17252416
132k17252416
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used! -type d
not-type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of-type f
I used! -type d
to copy any type of non-directory file.
– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
add a comment |
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used! -type d
not-type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of-type f
I used! -type d
to copy any type of non-directory file.
– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
why you use - -type d , this searched only folders
– yael
2 hours ago
why you use - -type d , this searched only folders
– yael
2 hours ago
@yael I used
! -type d
not -type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of -type f
I used ! -type d
to copy any type of non-directory file.– Kusalananda
2 hours ago
@yael I used
! -type d
not -type d
. I did this because you never said anything about what types of files you wanted to copy, so instead of -type f
I used ! -type d
to copy any type of non-directory file.– Kusalananda
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
ok , lets say - /tmp/calculation folder is empty folder , in that case can you add the right syntax for this case?
– yael
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
@yael I don't understand what you mean by your last comment. If the folder is empty, it should copy files there. Does it not do that for you?
– Kusalananda
2 hours ago
add a comment |
For RSYNC
Depending on permissions you can simply use the command:
rsync -r /home/ /tmp/calculation
The trailing / on /home/ is important if you want to backup/move the contents of home without moving home itself.
Realistically there are numerous methods to do the function you want:
https://www.linux.com/learn/how-move-files-using-linux-commands-or-file-managers
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
New contributor
rysync
does not move, it copies.
– Kusalananda
3 hours ago
add a comment |
For RSYNC
Depending on permissions you can simply use the command:
rsync -r /home/ /tmp/calculation
The trailing / on /home/ is important if you want to backup/move the contents of home without moving home itself.
Realistically there are numerous methods to do the function you want:
https://www.linux.com/learn/how-move-files-using-linux-commands-or-file-managers
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
New contributor
rysync
does not move, it copies.
– Kusalananda
3 hours ago
add a comment |
For RSYNC
Depending on permissions you can simply use the command:
rsync -r /home/ /tmp/calculation
The trailing / on /home/ is important if you want to backup/move the contents of home without moving home itself.
Realistically there are numerous methods to do the function you want:
https://www.linux.com/learn/how-move-files-using-linux-commands-or-file-managers
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
New contributor
For RSYNC
Depending on permissions you can simply use the command:
rsync -r /home/ /tmp/calculation
The trailing / on /home/ is important if you want to backup/move the contents of home without moving home itself.
Realistically there are numerous methods to do the function you want:
https://www.linux.com/learn/how-move-files-using-linux-commands-or-file-managers
https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/
New contributor
New contributor
answered 3 hours ago
Matthew WilliamsMatthew Williams
11
11
New contributor
New contributor
rysync
does not move, it copies.
– Kusalananda
3 hours ago
add a comment |
rysync
does not move, it copies.
– Kusalananda
3 hours ago
rysync
does not move, it copies.– Kusalananda
3 hours ago
rysync
does not move, it copies.– Kusalananda
3 hours ago
add a comment |
= find =
find /home -type f -exec cp {} /tmp/calculation ;
New contributor
add a comment |
= find =
find /home -type f -exec cp {} /tmp/calculation ;
New contributor
add a comment |
= find =
find /home -type f -exec cp {} /tmp/calculation ;
New contributor
= find =
find /home -type f -exec cp {} /tmp/calculation ;
New contributor
edited 2 hours ago
jimmij
31.9k873108
31.9k873108
New contributor
answered 3 hours ago
olioliolioliolioli
111
111
New contributor
New contributor
add a comment |
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%2f502979%2fhow-to-copy-recursive-only-the-files-from-folders-and-sub-folders%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
You tagged your question with rsync. Does that mean you've tried using
rsync
but failed? Likewise forfind
.– Kusalananda
3 hours ago
I tag rsync because I think rsync is one of the options
– yael
3 hours ago
It's unclear whether you need to reproduce the directory hierarchy of
/home
under the destination directory, or whether you're expecting all files to placed in a flat directory structure. You are also showing some specific directories, but it's unclear how these named paths relates to your question.– Kusalananda
3 hours ago
my target is to copy every file ( only files and not folders ) under /home to flat folder - /tmp/calculation , so all files will be under - /tmp/calculation
– yael
3 hours ago
And how would you want to handle duplicated filenames?
– Kusalananda
3 hours ago