Move files and create daily or monthly directories

Hello;

I have the below command in the crontab to do moving for the files, but how I can move these files to directories based on the day or based on the month, for example if the day is 2013-11-13 then it is required to create the directory 2013-11-13 and move these files for it. Or if the month is November then to create Nov directory and move the files for it. How?

/usr/bin/find /var/spool/asterisk/monitorDONE/MP3 -maxdepth 2 -type f -mtime +21 -exec mv '{}' ABSOLUTE/PATH/TO/FTP/ \;

Thanks for the help in advance.
Regards
Bilal

To make a directory with the current date in yyyy-mm-dd format as name:

mkdir /PATH/TO/WHERE/YOU/WANT/IT/$(date +%Y-%m-%d)

See

man date

for other format possibilities of the date command…
Of course, when you want to use that directory name more then once in your script, then store it in a variable and use that variable later"

DIRNAME="/PATH/TO/WHERE/YOU/WANT/IT/$(date +%Y-%m-%d)"
mkdir ${DIRNAME}
mv SOMEFILE ${DIRNAME}

Hello;

All above commands were going fine with me except I was need to create a directory every month to be based on the year and month, but it did not work from the crontab but it is working if I type the same command from the CLI. It is working if I typed "

mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)
".

But from the crontab from the below command, it is not creating the directory, why?

10 22 1 * * mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)

Regards
Bilal

On Fri 24 Jan 2014 03:16:02 PM CST, bghayad wrote:

Hello;

All above commands were going fine with me except I was need to create a
directory every month to be based on the year and month, but it did not
work from the crontab but it is working if I type the same command from
the CLI. It is working if I typed “>
mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)”.

But from the crontab from the below command, it is not creating the
directory, why?

> 10 22 1 * * mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)

Regards
Bilal

Hi
Unless you have set the PATH statement in cron, you need to use the
full path to the commands, you have done that in your first example.


Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.2 Kernel 3.11.6-4-desktop
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!

Hello;

Even, I used the full path “as below” and did not work, any advise?

10 22 1 * * /usr/bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)

Regards
Bilal

On Fri 24 Jan 2014 05:36:02 PM CST, bghayad wrote:

Hello;

Even, I used the full path “as below” and did not work, any advise?

> 10 22 1 * * /usr/bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(date
> +%Y-%m)

Regards
Bilal

Hi
I wonder if it needs escaping eg;


date +\%Y\-%m

You should also probably use the -p with your mkdir command.


Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.2 Kernel 3.11.6-4-desktop
If you find this post helpful and are logged into the web interface,
please show your appreciation and click on the star below… Thanks!

I tried all of this and did not work!
Also, the command is working from the CLI even without the escape without the -p

Again, what it could be?

Regards
Bilal

By the way, I discovered that mkdir existed under /bin and not under /usr/sbin but the same problem, still I am using the below command and the directory is not created, although I can run the command from the CLI:

10 22 24 * * /bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)

Regards
Bilal

No. mkdir is in /usr/bin/. /bin/mkdir is just a symlink for compatibility.

Anyway, I just tried and you don’t have to use the full pathnames in the crontab. /usr/bin/ is in the path.
But I hope you are aware of the fact that the line you posted only gets run on the 1st day of the month at 22:10?
So it’s of course clear that the directory doesn’t get created. It will only be run on Feb. 1st the next time, not before that.

Or did you change that for testing?

Btw, you should get a mail from cron when there is an error in running your commands.
Type f.e. “mail” in a terminal window to read them.

Hi
The other option is to just script what you want and add to the relevant /etc/cron.[hourly/daily/weekly/monthly] directories to run…

Or just run the script from your crontab.

PS: the line should look like this:


10 22 24 * * /bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +\%Y-\%m)

i.e. you have to escape the ‘%’. Then it should work. (but not until Feb. 1st if you have exactly this line, as I wrote before… :wink: )

It seems the problem in this part:

$(date +%Y-%m)

But really I do not know what should I place it to create the directory based on the year and month specially that it is working from the CLI, so what it should be it the cront and why it does not accept it same as the CLI?

Regards
Bilal

Thanks a lot.
Yes, the escape is the solution and your below line is the correct, but why the escape is required?

10 22 24 * * /bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(date +%Y-%m)

Regards
Bilal

See “man 5 crontab”:

   The "sixth" field (the rest of the line) specifies the  command  to  be       run.   The entire command portion of the line, up to a newline or a "%"
   character, will be executed by /bin/sh or by the shell specified in the
   SHELL variable of the cronfile. ** A "%" character in the command, unless**

** escaped with a backslash (), will be changed into newline characters,**
** and all data after the first % will be sent to the command as standard**
** input**.

On 2014-01-24 22:16, wolfi323 wrote:
>
> bghayad;2619092 Wrote:
>> Yes, the escape is the solution and your below line is the correct, but
>> why the escape is required?
>>
> See “man 5 crontab”:
>> The “sixth” field (the rest of the line) specifies the command
>> to be run. The entire command portion of the line, up to a
>> newline or a “%”

Heh. Which is one of the reasons many people simply call scripts from
crontab, and put everything on those scripts. Makes life easier. :wink:


Cheers / Saludos,

Carlos E. R.
(from 12.3 x86_64 “Dartmouth” at Telcontar)

Full path of all commands? Just a guess, sitting in windows right now
10 22 24 * * /bin/mkdir /var/spool/asterisk/monitorDONE/FTP/$(**/bin/**date +%Y-%m)

/quinness

[edit] Sorry, didn’t understand that it was solved.