how to get the logs of integrated command of find and mv?
I used this command to my script to find specific files and to moved to different folder; however, I would need to generate a log of which file/s I have moved.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
What to add to that command to be able to generate a log?
find logs mv hp-ux
add a comment |
I used this command to my script to find specific files and to moved to different folder; however, I would need to generate a log of which file/s I have moved.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
What to add to that command to be able to generate a log?
find logs mv hp-ux
Does yourmv
command have a-v
(--verbose
) option?
– steeldriver
Feb 19 at 3:41
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
@steeldriver HP-UX'smv
does not have-v
as far as I can see.
– Kusalananda
Feb 19 at 6:42
add a comment |
I used this command to my script to find specific files and to moved to different folder; however, I would need to generate a log of which file/s I have moved.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
What to add to that command to be able to generate a log?
find logs mv hp-ux
I used this command to my script to find specific files and to moved to different folder; however, I would need to generate a log of which file/s I have moved.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
What to add to that command to be able to generate a log?
find logs mv hp-ux
find logs mv hp-ux
edited Feb 19 at 10:24
Jeff Schaller
43.1k1159137
43.1k1159137
asked Feb 19 at 3:23
Gerard SacayGerard Sacay
1
1
Does yourmv
command have a-v
(--verbose
) option?
– steeldriver
Feb 19 at 3:41
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
@steeldriver HP-UX'smv
does not have-v
as far as I can see.
– Kusalananda
Feb 19 at 6:42
add a comment |
Does yourmv
command have a-v
(--verbose
) option?
– steeldriver
Feb 19 at 3:41
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
@steeldriver HP-UX'smv
does not have-v
as far as I can see.
– Kusalananda
Feb 19 at 6:42
Does your
mv
command have a -v
(--verbose
) option?– steeldriver
Feb 19 at 3:41
Does your
mv
command have a -v
(--verbose
) option?– steeldriver
Feb 19 at 3:41
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
@steeldriver HP-UX's
mv
does not have -v
as far as I can see.– Kusalananda
Feb 19 at 6:42
@steeldriver HP-UX's
mv
does not have -v
as far as I can see.– Kusalananda
Feb 19 at 6:42
add a comment |
3 Answers
3
active
oldest
votes
Your command,
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
should, first of all, be written as
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ;
otherwise you disqualify it from being used on directories and files with spaces in their names.
To output the pathnames of the things actually moved by the mv
in this command, simply add -print
last:
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ; -print
This would output the pathnames of all files (or directories) that were successfully moved to the destination given by "$4"
.
add a comment |
You can add a -print before the -exec to have the filename printed before moved.
1
Adding-print
before the-exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.
– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
add a comment |
You can just add another -exec
block with an echo
command to write to a log file, i.e.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec echo mv {} $4 >> /path/to/log ;
If you want less information, just modify the final -exec
block, e.g. -exec echo {} >> /path/to/log ;
Error logging
If you also want to log whether the command was successful or not, you could also pipe the standard error from the mv
command to the log file. I don't think it's guaranteed that the first -exec
necessarily acts first, so I would use one -exec
in this case. N.B. the syntax gets a bit fiddlier here.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec sh -c 'echo mv $1 $2 >> /path/to/log; mv $1 $2 2>> /path/to/log' . {} $4 ;
Some comments
I'm not sure of your exact use case, but it's good practice to quote your variables, e.g. find "$1"* …
, in case there are filenames with spaces/etc. in them.
Secondly, -execdir
is preferred over -exec
. From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
mv -v instead
Alternatively, as per steeldriver's comment, if your version of mv
supports the verbose -v
option, you could modify the original -exec
block instead, to
-exec mv -v {} $4 >> /path/to/log ;
which will provide output similar to renamed 'foo' -> 'bar'
.
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added theecho
, without reading the full command. He now has two-exec
bits. You need both.
– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the*
s are doing in there, so I just tried to be agnostic about it all.
– Sparhawk
Feb 19 at 6:45
|
show 2 more comments
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%2f501497%2fhow-to-get-the-logs-of-integrated-command-of-find-and-mv%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
Your command,
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
should, first of all, be written as
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ;
otherwise you disqualify it from being used on directories and files with spaces in their names.
To output the pathnames of the things actually moved by the mv
in this command, simply add -print
last:
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ; -print
This would output the pathnames of all files (or directories) that were successfully moved to the destination given by "$4"
.
add a comment |
Your command,
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
should, first of all, be written as
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ;
otherwise you disqualify it from being used on directories and files with spaces in their names.
To output the pathnames of the things actually moved by the mv
in this command, simply add -print
last:
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ; -print
This would output the pathnames of all files (or directories) that were successfully moved to the destination given by "$4"
.
add a comment |
Your command,
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
should, first of all, be written as
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ;
otherwise you disqualify it from being used on directories and files with spaces in their names.
To output the pathnames of the things actually moved by the mv
in this command, simply add -print
last:
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ; -print
This would output the pathnames of all files (or directories) that were successfully moved to the destination given by "$4"
.
Your command,
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ;
should, first of all, be written as
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ;
otherwise you disqualify it from being used on directories and files with spaces in their names.
To output the pathnames of the things actually moved by the mv
in this command, simply add -print
last:
find "$1"* -prune -name "*.$2" -mtime +"$3" -exec mv {} "$4" ; -print
This would output the pathnames of all files (or directories) that were successfully moved to the destination given by "$4"
.
answered Feb 19 at 10:31
KusalanandaKusalananda
134k17255418
134k17255418
add a comment |
add a comment |
You can add a -print before the -exec to have the filename printed before moved.
1
Adding-print
before the-exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.
– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
add a comment |
You can add a -print before the -exec to have the filename printed before moved.
1
Adding-print
before the-exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.
– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
add a comment |
You can add a -print before the -exec to have the filename printed before moved.
You can add a -print before the -exec to have the filename printed before moved.
answered Feb 19 at 6:34
marcularmarcular
12
12
1
Adding-print
before the-exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.
– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
add a comment |
1
Adding-print
before the-exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.
– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
1
1
Adding
-print
before the -exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.– Kusalananda
Feb 19 at 6:52
Adding
-print
before the -exec
would print the pathnames that the command tried to move, not the pathnames that were successfully moved.– Kusalananda
Feb 19 at 6:52
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
Good point. It should of course go after the exec.
– marcular
Feb 19 at 18:33
add a comment |
You can just add another -exec
block with an echo
command to write to a log file, i.e.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec echo mv {} $4 >> /path/to/log ;
If you want less information, just modify the final -exec
block, e.g. -exec echo {} >> /path/to/log ;
Error logging
If you also want to log whether the command was successful or not, you could also pipe the standard error from the mv
command to the log file. I don't think it's guaranteed that the first -exec
necessarily acts first, so I would use one -exec
in this case. N.B. the syntax gets a bit fiddlier here.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec sh -c 'echo mv $1 $2 >> /path/to/log; mv $1 $2 2>> /path/to/log' . {} $4 ;
Some comments
I'm not sure of your exact use case, but it's good practice to quote your variables, e.g. find "$1"* …
, in case there are filenames with spaces/etc. in them.
Secondly, -execdir
is preferred over -exec
. From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
mv -v instead
Alternatively, as per steeldriver's comment, if your version of mv
supports the verbose -v
option, you could modify the original -exec
block instead, to
-exec mv -v {} $4 >> /path/to/log ;
which will provide output similar to renamed 'foo' -> 'bar'
.
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added theecho
, without reading the full command. He now has two-exec
bits. You need both.
– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the*
s are doing in there, so I just tried to be agnostic about it all.
– Sparhawk
Feb 19 at 6:45
|
show 2 more comments
You can just add another -exec
block with an echo
command to write to a log file, i.e.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec echo mv {} $4 >> /path/to/log ;
If you want less information, just modify the final -exec
block, e.g. -exec echo {} >> /path/to/log ;
Error logging
If you also want to log whether the command was successful or not, you could also pipe the standard error from the mv
command to the log file. I don't think it's guaranteed that the first -exec
necessarily acts first, so I would use one -exec
in this case. N.B. the syntax gets a bit fiddlier here.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec sh -c 'echo mv $1 $2 >> /path/to/log; mv $1 $2 2>> /path/to/log' . {} $4 ;
Some comments
I'm not sure of your exact use case, but it's good practice to quote your variables, e.g. find "$1"* …
, in case there are filenames with spaces/etc. in them.
Secondly, -execdir
is preferred over -exec
. From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
mv -v instead
Alternatively, as per steeldriver's comment, if your version of mv
supports the verbose -v
option, you could modify the original -exec
block instead, to
-exec mv -v {} $4 >> /path/to/log ;
which will provide output similar to renamed 'foo' -> 'bar'
.
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added theecho
, without reading the full command. He now has two-exec
bits. You need both.
– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the*
s are doing in there, so I just tried to be agnostic about it all.
– Sparhawk
Feb 19 at 6:45
|
show 2 more comments
You can just add another -exec
block with an echo
command to write to a log file, i.e.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec echo mv {} $4 >> /path/to/log ;
If you want less information, just modify the final -exec
block, e.g. -exec echo {} >> /path/to/log ;
Error logging
If you also want to log whether the command was successful or not, you could also pipe the standard error from the mv
command to the log file. I don't think it's guaranteed that the first -exec
necessarily acts first, so I would use one -exec
in this case. N.B. the syntax gets a bit fiddlier here.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec sh -c 'echo mv $1 $2 >> /path/to/log; mv $1 $2 2>> /path/to/log' . {} $4 ;
Some comments
I'm not sure of your exact use case, but it's good practice to quote your variables, e.g. find "$1"* …
, in case there are filenames with spaces/etc. in them.
Secondly, -execdir
is preferred over -exec
. From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
mv -v instead
Alternatively, as per steeldriver's comment, if your version of mv
supports the verbose -v
option, you could modify the original -exec
block instead, to
-exec mv -v {} $4 >> /path/to/log ;
which will provide output similar to renamed 'foo' -> 'bar'
.
You can just add another -exec
block with an echo
command to write to a log file, i.e.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec echo mv {} $4 >> /path/to/log ;
If you want less information, just modify the final -exec
block, e.g. -exec echo {} >> /path/to/log ;
Error logging
If you also want to log whether the command was successful or not, you could also pipe the standard error from the mv
command to the log file. I don't think it's guaranteed that the first -exec
necessarily acts first, so I would use one -exec
in this case. N.B. the syntax gets a bit fiddlier here.
find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; -exec sh -c 'echo mv $1 $2 >> /path/to/log; mv $1 $2 2>> /path/to/log' . {} $4 ;
Some comments
I'm not sure of your exact use case, but it's good practice to quote your variables, e.g. find "$1"* …
, in case there are filenames with spaces/etc. in them.
Secondly, -execdir
is preferred over -exec
. From man find
:
There are unavoidable security problems surrounding use of the -exec action; you should use the -execdir option instead.
mv -v instead
Alternatively, as per steeldriver's comment, if your version of mv
supports the verbose -v
option, you could modify the original -exec
block instead, to
-exec mv -v {} $4 >> /path/to/log ;
which will provide output similar to renamed 'foo' -> 'bar'
.
edited Feb 19 at 9:30
answered Feb 19 at 3:29
SparhawkSparhawk
10.2k74398
10.2k74398
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added theecho
, without reading the full command. He now has two-exec
bits. You need both.
– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the*
s are doing in there, so I just tried to be agnostic about it all.
– Sparhawk
Feb 19 at 6:45
|
show 2 more comments
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added theecho
, without reading the full command. He now has two-exec
bits. You need both.
– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the*
s are doing in there, so I just tried to be agnostic about it all.
– Sparhawk
Feb 19 at 6:45
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
Hi Sparhawk, just tried, and it just log the action, but it didn't action (it didn't move the files)
– Gerard Sacay
Feb 19 at 5:33
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay That's really odd. The rest is the same as your original command. Were there any errors?
– Sparhawk
Feb 19 at 6:21
@GerardSacay You probably just added the
echo
, without reading the full command. He now has two -exec
bits. You need both.– Kusalananda
Feb 19 at 6:40
@GerardSacay You probably just added the
echo
, without reading the full command. He now has two -exec
bits. You need both.– Kusalananda
Feb 19 at 6:40
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
No comment on the lack of quoting, @Sparhawk?
– Kusalananda
Feb 19 at 6:42
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the
*
s are doing in there, so I just tried to be agnostic about it all.– Sparhawk
Feb 19 at 6:45
@Kusalananda TBH I find the whole command a bit strange. I'm not entirely sure what the
*
s are doing in there, so I just tried to be agnostic about it all.– Sparhawk
Feb 19 at 6:45
|
show 2 more comments
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%2f501497%2fhow-to-get-the-logs-of-integrated-command-of-find-and-mv%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
Does your
mv
command have a-v
(--verbose
) option?– steeldriver
Feb 19 at 3:41
Hi Steeldriver, here is the command in my script: find $1* -prune -name "*.$2" -mtime +$3 -exec mv {} $4 ; , where will i insert the verbose option?
– Gerard Sacay
Feb 19 at 5:31
@steeldriver HP-UX's
mv
does not have-v
as far as I can see.– Kusalananda
Feb 19 at 6:42