Replace missing standard folders from home directories.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace missing standard folders from home directories.
# 1  
Old 01-14-2009
Replace missing standard folders from home directories.

Hi, I want to develop a script to replace missing folders from home directories. These may have been deleted by the user. A standard home directory will have these folders in it and nothing else:

Desktop, Documents, Downloads, Library, Movies, Music, Pictures, Public, Sites

I also want to move anything that is not one of the standard folders listed here from the root of the home directory into ~user/Documents/ and not overwrite anything there, perhaps by appending a date or something to the file name.

If a folder is missing from the home directory, I want to replace it with one from: /System/Library/User\ Template/English.lproj/ where exists the folders used to create home directories. Then we need to change ownership so that it belongs to the user and not root.

Also, assume that every home directory name is the same as the userid, Liza Jane has userid 'janel' and her home directory is /Users/Students/janel


for user in /Users/Students/* ; do
#here is where I want to replace missing folders for example:
cp -R /System/Library/User\ Template/English.lproj/Sites $user/
chown -R $user $user/Sites
# and move anything non-standard into $user/Documents/
done
exit


Any advice would sure be appreciated.
# 2  
Old 01-14-2009
Code:
fix_student()
(
    user=$1
    cd /Users/Students/$user || return
    dirlist='Desktop  Documents  Downloads
            Library  Movies  Music  Pictures
            Public Sites'
    for dir in $dirlist
    do
      [ -d "$dir" ] || cp -R "/System/Library/User Template/English.lproj/Sites/$dir" .
    done
    chown -R "$user" */
)

for user in /Users/Students/*
do
   fix_student "$user"
done

(And fire whoever created a directory with a space in its name!)
# 3  
Old 01-14-2009
hope below can help you some

Code:
for i in Desktop  Documents  Downloads  Library  Movies  Music  Pictures  Public  Sites
do
	if ! [ -e $i ]
	then 
		mkdir $i
	fi
done
for i in *
do
	if [ -f $i ]
	then
		cp $i Documents
		rm $i
	fi
done

# 4  
Old 01-19-2009
Thanks to both of you for the code examples. Is very helpful to see different approaches to this. In regard to spaces in directory names, this is OS X and they are very liberal with using spaces in file and folder names.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

2. Solaris

Under /home there is no files and folders

When I'm logging as a user(i.e., oracle), I don't find any files and folders in /home Attached error message below, while logging... Last login: Fri Sep 7 08:08:09 2012 from ko.domain Could not chdir to home directory /home/oracle: No such file or directory Sun Microsystems Inc. SunOS... (6 Replies)
Discussion started by: karthikn
6 Replies

3. HP-UX

Home directories in packages

Hi, At my new company they use HP-UX on all the servers. They use Serviceguard to provide different packages, which are treated as if they where seperate systems. Therefore people log into packages instead of Host, and even the home directories live in the package. Now there are a different... (4 Replies)
Discussion started by: michas
4 Replies

4. Shell Programming and Scripting

Script to copy User home folders to mounted windows share

First of all, let me state that I am a windows admin. I have a windows share mounted to /mnt/server I need a script that will either login as sudo or perform commands with sudo rights. I need the script to copy all of the users /home folders to the mounted windows share. Now If I can... (7 Replies)
Discussion started by: EricM
7 Replies

5. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

6. Shell Programming and Scripting

check if multiple directories exist else create missing directories

Hi , I 'm trying to check if multiple directories exist on a server, if not create the missing ones and print " creating missing directory. how to write this in a simple script, I have made my code complex if ; then taskStatus="Schema extract directory exists, checking if SQL,Count and... (7 Replies)
Discussion started by: ramky79
7 Replies

7. Shell Programming and Scripting

copy some files from users home folders to my folder

i have users home directories in /home all the users have some files starting with character e and i want to copy all these files in a folder in my (root) home using a script i tried the script for i in m5 do cd m5 cp e1* /home/pc/exam cd .. done but get these... (3 Replies)
Discussion started by: pcrana
3 Replies

8. UNIX for Dummies Questions & Answers

UNIX home directories

Hi All, Could someone help, am a complete beginner when it comes to UNIX. However I have been tasked with investigating automatic creation of UK unix home directories. Is someone able to help? Thanks in advance! (7 Replies)
Discussion started by: zainster
7 Replies

9. Solaris

How to backup /home directories?

I know that how to backup the home directories in sun sparc server. Firstly, umount the filesystem, Secondly, fsck the filesystem, Thirdly, ufsdump the filesystem. Anybody know how to type the full command line backup the /home directory? (1 Reply)
Discussion started by: kingsan
1 Replies

10. UNIX for Dummies Questions & Answers

Delete old home directories

I have a script that deletes obselete users from /etc/passwd then moves their home directories to another location. After 30 days, I need to delete the home directories that were moved to the new location. I would appreciate any ideas on how to delete the directories after the 30 days? (2 Replies)
Discussion started by: munch
2 Replies
Login or Register to Ask a Question