I'm trying to write a "janitor" script that will run as a cron job in one specific directory. It is supposed to create an archive folder with the date of creation in the name, and then find and move all files of a certain type into this new folder.

Here is my test code:


today=$(date +'%m:%d:%Y')
mkdir -p $target
find . -type f -name "*.zip" -exec mv -i {} /$target \;

It manages to create the folder correctly, but is unable to move the files it finds into the folder. I have only been doing this as a small test, and both the script and the files have been created by the same user. If I add sudo to the beginning it tries to move the files, but instead what happens is that it only deletes the files from the current directory, but does not place them in the newly created directory.

I am not trying to move .zip files. Just an example.

I have tried by having chmod 777 on both files and folders. Same thing happens.

I am running ubuntu 14.04 LTS.

If there is a much better way to do this, please tell me.

Any pointers in the right direction would be very much appreciated.


Now it works.

I updated the find statement to:

find . -maxdepth 1 -type f -name "*.zip" -exec mv -t "$target/" {} \;
  • 1
    mistype ' -iname ' !
    – Yunus
    Nov 2, 2015 at 13:23
  • 1
    also mv command , see answer below :)
    – Yunus
    Nov 2, 2015 at 13:30
  • 1
    Are you sure you want -i in the mv command of this script? Also, as younes changed in his answer, you probably don't want a leading / on your target directory since you aren't creating the directory in / Nov 2, 2015 at 13:32
  • 1
    You're right, Eric. Thank you. I will mark it as solved and update the answer shortly. Thanks to both you.
    – Evan
    Nov 2, 2015 at 13:50

1 Answer 1


find files in current dir not subdirs :

find . -maxdepth 1  -type f -name '*.zip'  -exec mv -t "$target/" {} \;

exclude dirs methode :

find . -type f -not -path "$target/*"  -name '*.zip'  -exec mv -t "$target/" {} \;

Note that this will exclude only today's archive while you'll have other archive , i recommand the first command , or creat archive-dirs outside the main dir !

  • Well, that was embarrassing, but thank you! :) That worked. Just one question: I have two testfiles, test1.zip and test2.zip, and I get a message when running the code now that says mv: './Archived-11:02:2015/test2.zip' and 'Archived-11:02:2015/test2.zip' are the same file Could you tell me why?
    – Evan
    Nov 2, 2015 at 13:33
  • 2
    If you're going to use mv -t, you might as well change mv -t "$target/" {} \; to mv -t "$target/" {} + . This lets find pass multiple files to mv, reducing the number of mv invocations needed
    – iruvar
    Nov 2, 2015 at 13:35
  • ops you must exclude target ,
    – Yunus
    Nov 2, 2015 at 13:37
  • answer edited !
    – Yunus
    Nov 2, 2015 at 13:39
  • 1
    or better creat archive-todays outside the main folder
    – Yunus
    Nov 2, 2015 at 13:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .