Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm putting a file in cron.hourly which needs to create a folder on the 1st time, and giving it access to an user so i don't need to use sudo if i want to remove all content in the folder. And in it makes folder usable also. -m 777 doesn't work for missing folder when using -p

#!/bin/sh 
mkdir -m 777 -p 000Hourly/$(date +"%m-%d-%y_%T")

doesnt work: makes 000Hourly for admin

#!/bin/sh
mkdir -m 777 000Hourly && mkdir -m 777 -p 000Hourly/$(date +"%m-%d-%y_%T")

gives on next hour error mkdir: cannot create directory '000Hourly': File exists and stops executing the rest of it.

This is what im trying to make so i need 777 so i could delete all folder when its too full.

#!/bin/sh
profiledir=/home/kangarooo/.mozilla/firefox/a2t42sie.default/
cd $profiledir;
mkdir -m 777 000Hourly ; mkdir -m 777 -p 000Hourly/$(date +"%m-%d-%y_%T") && cp -p sessionstore.js sessionstore.bak 000Hourly/$(date +"%m-%d-%y_%T")
exit 0
share|improve this question
1  
you should never set things to permissions of 777 - see this question for more info: askubuntu.com/questions/20105/… –  ImaginaryRobots Nov 8 '12 at 23:05
    
i need that couse i need in my firefox folder to create this folder and i could delete it when theres too many copies of sessionrestore. Ok ill put full my code so ull know what im trying to do. –  Kangarooo Nov 9 '12 at 23:53
add comment

2 Answers 2

Here's a better way:

#!/bin/bash

dirname=000Hourly/$(date +"%m-%d-%y_%T") # Save the directory name for later access
user=bob   # Set to the username of the user who should have access.
group=bob  # Set to a group that should have access to the directory

mkdir -p "$dirname"
chown "$user:$group" "$dirname"

If only one user needs access, you're finished.

If more than one user needs to have access, create a group for everyone who needs access:

sudo groupadd mygroup

Read man groupadd for additional details, and change mygroup to whatever name you want (but it should begin with a lowercase letter or underscore and not include any spaces.

Then, add all necessary users to your new group, where user1, user2, and user3 and the usernames to add. You can add as many as you want.:

for i in user1 user2 user3; do sudo usermod -aG mygroup "$i"; done

The new group memberships will take effect after the user logs out (or the machine is rebooted).

Now, modify your script above. Set $group to your new group, and add the following line:

chmod 775 "$dirname"
share|improve this answer
add comment
[ -d 000Hourly ] || mkdir -m 777 000Hourly
mkdir -m 777 000Hourly/$(date +"%m-%d-%y_%T")
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.