-   Linux - Newbie (
-   -   Find directory older than x and mv directory with sub files command (

ajhart 07-13-2011 04:30 PM

Find directory older than x and mv directory with sub files command
Hi guy's, I'm quite new to linux but I have configured a simple ftp server and it's working great. I have a FTP-Shared folder with upload and download subfolders. Under upload's and download's I have identical category subfolders like mp3's, movies, software etc. in both. As the guy's upload, I would like to create a line crontab where I can move all the content under /FTP-Shared/upload/mp3/* older than 14 day's to FTP-Shared/downloads/mp3/ recursively (Like in cp command), but the timestamp must be searched on the first directory and not sub files example: /mp3/Club Dance/CD1/Hallo world.mp3

This is how far I got:
[root@clients ~]# /usr/bin/find /FTP_Shared/upload/Mp3s/ -depth -mindepth 1 -mtime +14 -type d -exec mv -f {} /FTP_Shared/download/Mp3s/ \;

This command moves the directory and files, but it is not recursively

Any ideas would be great!!
Thanks in advance

theNbomr 07-13-2011 05:35 PM

If you manually type the command part:

# substitute the appropriate source directory, of course
mv -f {} /FTP_Shared/download/Mp3s/

what happens that you think is deficient? mving a directory by definition also moves all of its subdirectories; no recursion required. You would need only to iterate over all found directories that meet the time criteria, and the find command should do that.

--- rod.

ajhart 07-14-2011 12:16 AM

If I use the mv command manually, it works. It is only when I use the find command where it finds all the files and folders timestamp and moves them accordingly. It does not move the like in the manual command (recursion). Could I maybe use the grep command to find the 1st directory and the get the timestamp of that and then try to move it with all subdirectory’s and files?


theNbomr 07-14-2011 08:44 AM

Okay, maybe what is happening is that find is reporting the found directories in a depth-first order, so the lower directories are being moved first. To diagnose, insert the 'echo' command in front of 'mv', so you can see what is happening:

/usr/bin/find /FTP_Shared/upload/Mp3s/ -depth -mindepth 1 -mtime +14 -type d -exec echo mv -f {} /FTP_Shared/download/Mp3s/ \;
This is generally good practice when developing such scripts, to make sure you've got everything right before you start actually affecting the filesystem. Actually, now that I see your find command includes the -depth option, I think this is the problem. Remove that, and use breadth-first recursion.

--- rod.

ajhart 07-15-2011 05:24 AM

Thanks for the advice, I'll have a look into that.

All times are GMT -5. The time now is 05:20 AM.