shell script for moving folders within a directory older than 30 days to another directory

Posted on 2005-04-14
Last Modified: 2013-12-26
i need a script that i can have cron run everyday, to move folders within a directory with a date of more than 30 days to another directory.

its important that the script only looks at the folder dates and not whats inside because it will start moving files out of the folders.

i want to keep the amount of orders in our order folder down to increase performance

should look something like this:

find /path/to/orderfolder -d -mtime +30 -exec mv -f {} /path/to/archivefolder \;

i get this response:
find: -d: unknown expression primary

Question by:dareach
    LVL 23

    Expert Comment

    If I understand your question, you only want to test the timestamp of directories.
    Therefore your find command should only look for items of type directory.
    I think you want to use '-type d' rather than  '-d' switch:

    find /path/to/orderfolder -type d -mtime +30 -exec mv -f {} /path/to/archivefolder \;


    Author Comment

    this one didnt do anything but i didnt get an error either!

    imagine opening two folders, looking at the contents side by side and sorted the first one by date. then you dragged all the folders that were older than 30 days to the other folder. that is essentialy what the script should do only without the GUI


    Author Comment

    this script seems to go inside each folder and look at the dates of the files inside and if they are older than 30 days it moves them out of the folder. thats not good, im trying to have it look only at the first level folder date and move based on that, not its contents
    LVL 23

    Accepted Solution

    I understand.   Replace '-d' with '-maxdepth 1', add '-type f' to narrow the search to ordinary files:

    find /path/to/orderfolder -maxdepth 1 -type f -mtime +30 -exec mv -f {} /path/to/archivefolder \;

    LVL 84

    Expert Comment

    find /path/to/orderfolder -maxdepth 1 -type d -mtime +30 -exec mv -f {} /path/to/archivefolder \;
    mv -f `perl -e '$,=" ";print grep -d&&-M>30,@ARGV' /path/to/orderfolder/*` /path/to/archivefolder

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now