2

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:

#!/bin/bash

today=$(date +'%m:%d:%Y')
target="Archived-$today"
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.

Edit

Now it works.

I updated the find statement to:

find . -maxdepth 1 -type f -name "*.zip" -exec mv -t "$target/" {} \;
  • mistype ' -iname ' ! – Jonah Nov 2 '15 at 13:23
  • also mv command , see answer below :) – Jonah Nov 2 '15 at 13:30
  • 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 / – Eric Renouf Nov 2 '15 at 13:32
  • You're right, Eric. Thank you. I will mark it as solved and update the answer shortly. Thanks to both you. – Evan Nov 2 '15 at 13:50
3

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 '15 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 '15 at 13:35
  • ops you must exclude target , – Jonah Nov 2 '15 at 13:37
  • answer edited ! – Jonah Nov 2 '15 at 13:39
  • 1
    or better creat archive-todays outside the main folder – Jonah Nov 2 '15 at 13:41

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.