Cron job to move file from one directory to another


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job to move file from one directory to another
# 1  
Old 04-08-2009
Cron job to move file from one directory to another

Hi,

I have following directory structure

Media (Inside media directory I have two folders namely videos and images)
-->videos
-->images

Inside media directory I have some video files with extension .mp4 and images with extension of .jpg and .jpeg

I want to write a cron job which will run every 30 min and will move all .mp4 files inside media directory to videos directory and images files to images directory.


Can anyone please help me with this.


Thanks!
# 2  
Old 04-08-2009
Try:
Code:
#!/bin/bash
find Media -type f -maxdepth 1 -name "*.mp4" -exec mv {} Media/videos \;
find Media -type f -maxdepth 1 -name "*.jpg" -exec mv {} Media/images \;
find Media -type f -maxdepth 1 -name "*.jpeg" -exec mv {} Media/images \;

Put that in a script, e.g. /usr/local/bin/movemedia.sh
and then add a crontab line:
Code:
1,31 * * * * /usr/local/bin/movemedia.sh >> /var/log/movemedia.log 2>&1

If you are using Linux then you want to rotate the log file each day by creating a file: /etc/logrotate.d/movemedia containing something like:
Code:
/var/log/movemedia.log {
        rotate 14
        daily
        missingok
}

Which will move movemedia.log to movemedia.log.0 up to movemedia.log.14 so keeping the last 14 day's logs.

Last edited by TonyFullerMalv; 04-08-2009 at 03:46 PM..
# 3  
Old 04-08-2009
Thanks a lot for your help.
Just one question.

I know the exact path of media directory. How can incorporate that path in this command.

$sDirectory = "/www/sites/projects/media";
$sVideos = "/www/sites/projects/media/videos";
$sImages = "/www/sites/projects/media/images";

I am not very sure of how can i use this?

$sDirectory -type f -maxdepth 1 -name "*.mp4" -exec mv {} $sVideos \;
# 4  
Old 04-08-2009
Quote:
Originally Posted by tusharkale
$sDirectory -type f -maxdepth 1 -name "*.mp4" -exec mv {} $sVideos \;
Yes with the find:
Code:
 find $sDirectory -type f -maxdepth 1 -name "*.mp4" -exec mv {} $sVideos \;

and your lines at the before the find lines in the script (note no spaces on either side of the "="):
Code:
$sDirectory="/www/sites/projects/media"
$sVideos="/www/sites/projects/media/videos"
$sImages="/www/sites/projects/media/images"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Add TimeStamp to cron job and write file name sent

Hello, I have written a cron job to automate the sftp of files using key authentication. I wanted to add a timeStamp and name of file sent to a log file and append each these details to the same file each time files are sent and if possible include whether the files were sent successfully or not.... (3 Replies)
Discussion started by: KidKoder
3 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

Cron Job failure - No such file or directory

Hi all, I'm having an issue with a script i wrote to pull information from the Amazon AWS API. Basically the script takes arguments from the command line and attempts to grab user information for each AWS access group. The command is issued like this: # sh awsReport.sh <outputFileName>... (3 Replies)
Discussion started by: ChocoTaco
3 Replies

4. Shell Programming and Scripting

How to create a cron job to take an uploaded filename and move it?

OK, So complete newbie here. I would normally do this in PHP or through my FTP program script but I can't in this case (the files are not coming from me, coming from a third party FTP upload). I have an FTP server (Linux) accepting files coming in from a standard FTP program. Each file... (2 Replies)
Discussion started by: bull_frog
2 Replies

5. Shell Programming and Scripting

Running script file using cron job every 5 second

Hi All, I beginner in unix, i have no idea how to set the script file using cron job every 5 second. I also want to execute automatically the output to text file.This is my script name countsys.sh and my textfile abc.txt. (6 Replies)
Discussion started by: mastercar
6 Replies

6. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

7. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies

8. Shell Programming and Scripting

file size-cron job

Dear all, I have the following case,, i need to transfer a group of file from one server to another ....when the size of any of these file reach a specified value (Ex: 10MB) i need to transfer it to another server ....my problem is that i dont know how to determine when the size of the file... (1 Reply)
Discussion started by: mm00123
1 Replies

9. UNIX for Dummies Questions & Answers

CRON job to execute all scripts in a directory

Hi everyone: I'm trying to make a CRON job that will execute Fridays at 7am. I have the following: * 7 * * 5 I've been studying up on CRON and I know to have this in a file and then "crontab filename.txt" to add it to the CRON job list. The CRON part I believe I understand, but I would... (6 Replies)
Discussion started by: Annorax
6 Replies

10. UNIX for Dummies Questions & Answers

cron job to run php file

Hello, I have searched and searched google to do this and i want my websever to be able to run a php file everyday automatically. How do I go about doing this? Php is installed as an apache module not CGI. Thank you! (3 Replies)
Discussion started by: christo16
3 Replies
Login or Register to Ask a Question