Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

doesnt work: makes 000Hourly for admin

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.

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
you should never set things to permissions of 777 - see this question for more info:… – 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

Here's a better way:


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
[ -d 000Hourly ] || mkdir -m 777 000Hourly
mkdir -m 777 000Hourly/$(date +"%m-%d-%y_%T")
share|improve this answer

Your Answer


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.