Find and copy with exec not working
Clash Royale CLAN TAG#URR8PPP
I'm using the following command to get the most recent file in a directory
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/"
This returns only the file name not the entire path.
I then want to copy the file I found into another folder, so I append the following to the previous find command:
-exec cp /home/user2/folder2 ;
So the full command looks like this:
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/ -exec cp /home/user2/folder2 ;
But this returns
cut: invalid option -- 'e'
What am I doing wrong here?
find file-copy
add a comment |
I'm using the following command to get the most recent file in a directory
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/"
This returns only the file name not the entire path.
I then want to copy the file I found into another folder, so I append the following to the previous find command:
-exec cp /home/user2/folder2 ;
So the full command looks like this:
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/ -exec cp /home/user2/folder2 ;
But this returns
cut: invalid option -- 'e'
What am I doing wrong here?
find file-copy
6
-exec
belongs together withfind
, you can't just tuck it on at the end of the pipeline like that.
– Kusalananda
Jan 18 at 22:10
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37
add a comment |
I'm using the following command to get the most recent file in a directory
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/"
This returns only the file name not the entire path.
I then want to copy the file I found into another folder, so I append the following to the previous find command:
-exec cp /home/user2/folder2 ;
So the full command looks like this:
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/ -exec cp /home/user2/folder2 ;
But this returns
cut: invalid option -- 'e'
What am I doing wrong here?
find file-copy
I'm using the following command to get the most recent file in a directory
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/"
This returns only the file name not the entire path.
I then want to copy the file I found into another folder, so I append the following to the previous find command:
-exec cp /home/user2/folder2 ;
So the full command looks like this:
/usr/bin/find /home/user1/folder1/ -type f -printf '%T@ %pn' | sort -n | tail -1 | cut -f2- -d" " | cut -f5 -d"/ -exec cp /home/user2/folder2 ;
But this returns
cut: invalid option -- 'e'
What am I doing wrong here?
find file-copy
find file-copy
asked Jan 18 at 22:05
user53029user53029
98041845
98041845
6
-exec
belongs together withfind
, you can't just tuck it on at the end of the pipeline like that.
– Kusalananda
Jan 18 at 22:10
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37
add a comment |
6
-exec
belongs together withfind
, you can't just tuck it on at the end of the pipeline like that.
– Kusalananda
Jan 18 at 22:10
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37
6
6
-exec
belongs together with find
, you can't just tuck it on at the end of the pipeline like that.– Kusalananda
Jan 18 at 22:10
-exec
belongs together with find
, you can't just tuck it on at the end of the pipeline like that.– Kusalananda
Jan 18 at 22:10
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37
add a comment |
2 Answers
2
active
oldest
votes
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. n
), and (ii) as already noted by Kusalananda, the -exec
option belongs to the find
command, and can thus not be separated therefrom as you have tried.
Using the GNU utilities, these issues can be fixed with the following pipeline, which will find the most recent file in (or below) the directory /home/user1/folder1/
and copy it to /home/user2/folder2/
:
find /home/user1/folder1/ -type f -printf '%T@ %p' 2>/dev/null |
sort -znk1,1 | tail -zn1 | cut -zf2- -d' ' |
xargs -0 cp -t /home/user2/folder2/
As to issue (i): note the at the end of the
-printf
format string, and the -z
and -0
options to the various commands in the pipeline, which ensure that the identified filename is passed in a NUL-delimited fashion, and thus enable it to include blanks and/or newlines.
As to issue (ii): you can use the xargs
command to collect arguments from stdin
and to build a new commandline with them. Part of the trick here is to use the -t
option to the cp
command, to specify the target directory before providing any filename to be copied there, since xargs
will build a commandline by appending any arguments it receives on stdin
to a given command.
add a comment |
Using the zsh
shell, assuming you want to copy the most recently modified file in the /home/user1/folder1
directory:
cp /home/user1/folder1/*(.om[1]) /home/user2/folder2
If zsh
is not your interactive shell, then you may do
zsh -c 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
The pattern /home/user1/folder1/*(.om[1])
would expand to the name of the most recently modified regular file in the given directory. The *(.om[1])
at the end is what orders (o
) the regular files (.
) by modification time (m
) and picks the first ([1]
).
If you need to match hidden filenames, then use
zsh -c -4 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
(add the -4
)
Would you need to additionally look into subdirectories, use
zsh -c -4 'cp /home/user1/folder1/**/*(.om[1]) /home/user2/folder2'
The shell globbing pattern **
in zsh
matches across /
in pathnames.
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%2f495361%2ffind-and-copy-with-exec-not-working%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
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. n
), and (ii) as already noted by Kusalananda, the -exec
option belongs to the find
command, and can thus not be separated therefrom as you have tried.
Using the GNU utilities, these issues can be fixed with the following pipeline, which will find the most recent file in (or below) the directory /home/user1/folder1/
and copy it to /home/user2/folder2/
:
find /home/user1/folder1/ -type f -printf '%T@ %p' 2>/dev/null |
sort -znk1,1 | tail -zn1 | cut -zf2- -d' ' |
xargs -0 cp -t /home/user2/folder2/
As to issue (i): note the at the end of the
-printf
format string, and the -z
and -0
options to the various commands in the pipeline, which ensure that the identified filename is passed in a NUL-delimited fashion, and thus enable it to include blanks and/or newlines.
As to issue (ii): you can use the xargs
command to collect arguments from stdin
and to build a new commandline with them. Part of the trick here is to use the -t
option to the cp
command, to specify the target directory before providing any filename to be copied there, since xargs
will build a commandline by appending any arguments it receives on stdin
to a given command.
add a comment |
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. n
), and (ii) as already noted by Kusalananda, the -exec
option belongs to the find
command, and can thus not be separated therefrom as you have tried.
Using the GNU utilities, these issues can be fixed with the following pipeline, which will find the most recent file in (or below) the directory /home/user1/folder1/
and copy it to /home/user2/folder2/
:
find /home/user1/folder1/ -type f -printf '%T@ %p' 2>/dev/null |
sort -znk1,1 | tail -zn1 | cut -zf2- -d' ' |
xargs -0 cp -t /home/user2/folder2/
As to issue (i): note the at the end of the
-printf
format string, and the -z
and -0
options to the various commands in the pipeline, which ensure that the identified filename is passed in a NUL-delimited fashion, and thus enable it to include blanks and/or newlines.
As to issue (ii): you can use the xargs
command to collect arguments from stdin
and to build a new commandline with them. Part of the trick here is to use the -t
option to the cp
command, to specify the target directory before providing any filename to be copied there, since xargs
will build a commandline by appending any arguments it receives on stdin
to a given command.
add a comment |
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. n
), and (ii) as already noted by Kusalananda, the -exec
option belongs to the find
command, and can thus not be separated therefrom as you have tried.
Using the GNU utilities, these issues can be fixed with the following pipeline, which will find the most recent file in (or below) the directory /home/user1/folder1/
and copy it to /home/user2/folder2/
:
find /home/user1/folder1/ -type f -printf '%T@ %p' 2>/dev/null |
sort -znk1,1 | tail -zn1 | cut -zf2- -d' ' |
xargs -0 cp -t /home/user2/folder2/
As to issue (i): note the at the end of the
-printf
format string, and the -z
and -0
options to the various commands in the pipeline, which ensure that the identified filename is passed in a NUL-delimited fashion, and thus enable it to include blanks and/or newlines.
As to issue (ii): you can use the xargs
command to collect arguments from stdin
and to build a new commandline with them. Part of the trick here is to use the -t
option to the cp
command, to specify the target directory before providing any filename to be copied there, since xargs
will build a commandline by appending any arguments it receives on stdin
to a given command.
Your command appears to have two issues, the first of which may not matter much in your case, but is nevertheless worth pointing out: (i) it is not generic in the sense that it will not be able to process arbitrary filenames, in particular filenames that contain newlines (i.e. n
), and (ii) as already noted by Kusalananda, the -exec
option belongs to the find
command, and can thus not be separated therefrom as you have tried.
Using the GNU utilities, these issues can be fixed with the following pipeline, which will find the most recent file in (or below) the directory /home/user1/folder1/
and copy it to /home/user2/folder2/
:
find /home/user1/folder1/ -type f -printf '%T@ %p' 2>/dev/null |
sort -znk1,1 | tail -zn1 | cut -zf2- -d' ' |
xargs -0 cp -t /home/user2/folder2/
As to issue (i): note the at the end of the
-printf
format string, and the -z
and -0
options to the various commands in the pipeline, which ensure that the identified filename is passed in a NUL-delimited fashion, and thus enable it to include blanks and/or newlines.
As to issue (ii): you can use the xargs
command to collect arguments from stdin
and to build a new commandline with them. Part of the trick here is to use the -t
option to the cp
command, to specify the target directory before providing any filename to be copied there, since xargs
will build a commandline by appending any arguments it receives on stdin
to a given command.
edited Jan 19 at 9:46
answered Jan 18 at 23:04
ozzyozzy
74715
74715
add a comment |
add a comment |
Using the zsh
shell, assuming you want to copy the most recently modified file in the /home/user1/folder1
directory:
cp /home/user1/folder1/*(.om[1]) /home/user2/folder2
If zsh
is not your interactive shell, then you may do
zsh -c 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
The pattern /home/user1/folder1/*(.om[1])
would expand to the name of the most recently modified regular file in the given directory. The *(.om[1])
at the end is what orders (o
) the regular files (.
) by modification time (m
) and picks the first ([1]
).
If you need to match hidden filenames, then use
zsh -c -4 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
(add the -4
)
Would you need to additionally look into subdirectories, use
zsh -c -4 'cp /home/user1/folder1/**/*(.om[1]) /home/user2/folder2'
The shell globbing pattern **
in zsh
matches across /
in pathnames.
add a comment |
Using the zsh
shell, assuming you want to copy the most recently modified file in the /home/user1/folder1
directory:
cp /home/user1/folder1/*(.om[1]) /home/user2/folder2
If zsh
is not your interactive shell, then you may do
zsh -c 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
The pattern /home/user1/folder1/*(.om[1])
would expand to the name of the most recently modified regular file in the given directory. The *(.om[1])
at the end is what orders (o
) the regular files (.
) by modification time (m
) and picks the first ([1]
).
If you need to match hidden filenames, then use
zsh -c -4 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
(add the -4
)
Would you need to additionally look into subdirectories, use
zsh -c -4 'cp /home/user1/folder1/**/*(.om[1]) /home/user2/folder2'
The shell globbing pattern **
in zsh
matches across /
in pathnames.
add a comment |
Using the zsh
shell, assuming you want to copy the most recently modified file in the /home/user1/folder1
directory:
cp /home/user1/folder1/*(.om[1]) /home/user2/folder2
If zsh
is not your interactive shell, then you may do
zsh -c 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
The pattern /home/user1/folder1/*(.om[1])
would expand to the name of the most recently modified regular file in the given directory. The *(.om[1])
at the end is what orders (o
) the regular files (.
) by modification time (m
) and picks the first ([1]
).
If you need to match hidden filenames, then use
zsh -c -4 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
(add the -4
)
Would you need to additionally look into subdirectories, use
zsh -c -4 'cp /home/user1/folder1/**/*(.om[1]) /home/user2/folder2'
The shell globbing pattern **
in zsh
matches across /
in pathnames.
Using the zsh
shell, assuming you want to copy the most recently modified file in the /home/user1/folder1
directory:
cp /home/user1/folder1/*(.om[1]) /home/user2/folder2
If zsh
is not your interactive shell, then you may do
zsh -c 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
The pattern /home/user1/folder1/*(.om[1])
would expand to the name of the most recently modified regular file in the given directory. The *(.om[1])
at the end is what orders (o
) the regular files (.
) by modification time (m
) and picks the first ([1]
).
If you need to match hidden filenames, then use
zsh -c -4 'cp /home/user1/folder1/*(.om[1]) /home/user2/folder2'
(add the -4
)
Would you need to additionally look into subdirectories, use
zsh -c -4 'cp /home/user1/folder1/**/*(.om[1]) /home/user2/folder2'
The shell globbing pattern **
in zsh
matches across /
in pathnames.
edited Jan 19 at 9:58
answered Jan 19 at 0:05
KusalanandaKusalananda
128k16241398
128k16241398
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%2f495361%2ffind-and-copy-with-exec-not-working%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
6
-exec
belongs together withfind
, you can't just tuck it on at the end of the pipeline like that.– Kusalananda
Jan 18 at 22:10
I'm realizing that now. Is there a solution for this though to accomplish my objective?
– user53029
Jan 18 at 22:37