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/" {} \;
-i
in themv
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/