Find and Move files

I have the below command to delete all .xml files older than 90 days

 
find . -type f -name '*.xml' -mtime +90 -exec rm {} \;

What will be the command to move all the .xml files older than 90 days to this folder -> "/tmp/my_bk"

My OS: SunOS my-pc 5.10 Generic_150400-17 sun4v sparc SUNW,T5440

The find command works like find foldername ...

You are currently giving it . , that is, the current directory.

As always, I recommend

find /tmp/my_bk -type f -name '*.xml' -mtime +90 -exec echo rm {} \;

to make sure it's doing what you want. The echo will cause it to print 'rm filename' instead of execute it. Remove the echo once you've tested.

Thank you Corona but that's not my concern .. i may give the foldername or the current directory ... my concern is using the mv command with -exec

Sorry, I misunderstood.

find . -type f -name '*.xml' -mtime +90 -exec echo mv '{}' /tmp/my_bk ';'

Again, remove the echo once you've tested.

Beware that if /tmp/my_bk is not a folder, this could remove your files!

A trailing / should fail with an error if the target dir is missing or is a file.

find ... -exec mv '{}' /tmp/my_bk/ ';'
1 Like