Find the answer to your Linux question:
Results 1 to 9 of 9
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Change Permissions by cronjob

    Our file server has a 'jobs' folder, and each quote we create on our system creates a corresponding folder on the server.

    I've a cron job that erases empty firectories that are older than 45 days.

    I'm trying to write one that will then change the permissions of job folders to read only after 60 days. This will prevent staff from editing archived jobs and will need to copy the files from the server and save them into a new 'job' folder.

    This is the script I have at the moment:

    45 11 * * 1-5 find /home/Samba/Jobs-2014/* -type d -name "REF*" -ctime +60 -exec chmod -R 550
    Which does find all the directories starting with REF that are 60 days old, but it doesn't recursively change the permissions to read only.

    What am I missing?

  2. #2
    Try the following

    45 11 * * 1-5 find /home/Samba/Jobs-2014/* -type d -name "REF*" -ctime +60 -exec chmod  550 {} \;
    will change the folders to read only then you'll need another entry to change the files

    45 11 * * 1-5 find /home/Samba/Jobs-2014/REF*/ -type f -ctime +60 -exec chmod  440 {} \;
    Your find utility may support the "-exec utility [argument ...] {} + " which is similar to piping the output of find to xargs.
    Last edited by fd0; 07-29-2014 at 12:45 PM.

  3. #3

    Currently using the code below, which I've slightly modified as the REF folders are inside directories named after customers.

    The customer_name folder needs to remain 770 permission, but the REF folders within that, and the folders inside of those can be made 550 after 60 days.

    Can't seem to get it to work again.

    #Makes files in Jobs Folder read only after 60days
    57 15 * * 1-5 find /home/Samba/Jobs-2014/* -type d -name "REF*" -ctime +60 -exec chmod -R 550 {} \;
    57 15 * * 1-5 find /home/Samba/Jobs-2014/*/* -type f -ctime +60 -exec chmod  440 {} \;

  4. $spacer_open
  5. #4
    I think find is choking on the wild cards in the paths. Can you give us an example of the directory hierarchy of one customer? You can autotomize the file names.

    In the mean time you could remove the wildcard in the file paths and use find's mindepth option. Something like this:
    find /home/Samba/Jobs-2014 -mindepth 2 -type d -name "REF*" -ctime +60 -exec chmod -R 550 {} \;
    Also, use full path names in your crontab (ie. /usr/bin/find), /bin/chmod). You should be OK without them but let's rule out that the problem is a path issue.

  6. #5
    The hirearchy looks something like this:

    Jobs-2014 ->
    Customer A ->
    REF1234 ->
    Print Files
    REF4321 ->
    Print Files
    Customer B ->
    REF3214 ->
    Print Files
    REF3412 ->
    Print Files

    The Customer directories are created automatically by our CRM software, and the REF, Artwork, Print & proofs are automatically created by the CRM software when a new quote is created.

    Just to minimise clutter I erase empty directories in the 'Jobs-2014' directory that are older than 45 days, and I would like to 'lock' the REF folders and sub directories as read only, just to prevent anyone from editing archived files. (example a customer requires a reprint of Job 1234, we know the file saved on the server hasn't been edited since it was last printed)

    I will make the changes to the Crontab tomorrow AM when I'm back in work.

  7. #6
    OK, that is clearer. You do not need the -R option in chmod. find will take care of all the directories named starting in REF.

    /usr/bin/find /home/Samba/Jobs-2014 -type d -name "REF*" -ctime +60 -exec /bin/chmod  550 {} \;
    #or this should be faster
    /usr/bin/find /home/Samba/Jobs-2014 -type d -name "REF*" -ctime +60 -exec /bin/chmod  550 {} +
    For your files, I would try this

    /usr/bin/find /home/Samba/Jobs-2014 -mindepth 3 -type f -ctime +60 -exec /bin/chmod  440 {} \;
    /usr/bin/find /home/Samba/Jobs-2014 -mindepth 3 -type f -ctime +60 -exec /bin/chmod  440 {} +
    Of coarse, you can test the find commands before placing them in your crontab by adding echo after the -exec option, i.e. -exec echo /bin/chmod 440 \; .

  8. #7
    Will the chmod change the permissions of Artwork/Print Files/Proof folders that are inside the 'REF' folders without the recursive command?

  9. #8
    I thought Artwork, Print files, Proofs were files and to answer your question, no. But think about what you are attempting to do.
    Recursively change permissions for files and folders to one mode, then concurrently change permissions on the same files to another mode. You could list multiple directory names in your find command or you could use the mindepth option without using the name option. Since your directories have spaces in their name do not use "-exec utility [argument ...] {} + " use the \; terminator.

  10. #9
    Sorry, I should have explained the directories a little better.

    I'll continue to experiment, what started off as a 'project with a purpose' which was simply setting up a file server which I've achieved, I'm now looking to undertake other little 'projects' to do some house keeping, and secure the archived folders.

    Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts