Is there any other short/easier and smarter way to do the following in Linux?
cd /home/abcd/dammi
mkdir $(date +%Y-%m)
cd $(date +%Y-%m)
mkdir $(date +%d)
# RESULT : /home/abcd/dammi/2011-05/26
cd /home/wxyz/harrami
mkdir $(date +%Y-%m)
cd $(date +%Y-%m)
mkdir $(date +%d)
cd /home/abcd/harrami
mkdir $(date +%Y-%m)
cd $(date +%Y-%m)
mkdir $(date +%d)
cd /home/wxyz/harrami
mkdir $(date +%Y-%m)
cd $(date +%Y-%m)
mkdir $(date +%d)
I need to use crontab to create folders every day and every month inside /home/abcd/dammi, /home/abcd/harrami, /home/wxyz/dammi and /home/wxyz/harrami. Can anyone help me with this?
Info:
GROUPS : USERS
--------------------
abcd : abcd
wxyz : dammi, harrami
Best Answer
What about the following:
Well, of course you could also do it without
DATE_DIR
andDAY_DIR
variables but this algorithm makes sure the date is not generated many times which is faster and assures the date to be the same for all users (even if you start it right before midnight and the last directory is created after midnight).You might also have to think about enhancing the script using chown in order to allow users to write to the newly created directories.
Edit: If you want to create the same folder structure within two
/home
sub-folders you might combine the script of RolKau with my one:Edit2: I assume the user list contains the user names. So let's extend the script so the directory owner is changed properly as well:
Edit3: To change the ownership I think the easiest way is to read the ownership from the directory at
/home/<subdir>/<user>
Sure you could use stat as well to read the owner; but it's slightly slower than just reading it from the variable - as you asked for it:
Edit4: Alternative solution using hard-coded group membership.
Edit5: Looking at your initial post the desired structure might be slightly different. I guess you want to have a sub-directory in
/home
for each group and only add user-directories for each group member. So here you go:If a user is member in multiple groups just specify it multiple times in USER_LIST.