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?

  • Does your mv command have a -v (--verbose) option? – steeldriver Feb 19 '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 '19 at 5:31
  • @steeldriver HP-UX's mv does not have -v as far as I can see. – Kusalananda Feb 19 '19 at 6:42

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


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 '19 at 6:52
  • Good point. It should of course go after the exec. – marcular Feb 19 '19 at 18:33

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 '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 '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 '19 at 6:40
  • No comment on the lack of quoting, @Sparhawk? – Kusalananda Feb 19 '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 '19 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.