Few years ago I had an assignment in a intro to linux class. Basically they gave us 17 different tasks that a script needed to do and we had to do this with shell scripting. It was actually easier than I expected since linux gives you incredible flexibility.

We were expected to create a script to maintain a user filesystem. Our script had to be driven by a main menu.

I don’t know how much of help this would be to anyone but every once in a while I get stuck on a code and I search the web for hours trying to find a solution and nothing comes up so if this helps one person that’s good enough for me. Code is really self explanatory with all the comments in it but if you have a question please do not hesitate to ask in the comments below.

For the code to work, you would need super user privileges as it uses functions that only super user has access to.

The functionality that is needed in the script:

  1. Create new directories in a specified user’s filesystem
  2. Add new users to the system in a specified home directory (one other than the default directory home).
  3. Delete a user’s filesystem.
  4. Delete specified files from a specified user’s filesystem.
  5. Maintain a log file the saves all operations that were performed.
  6. Maintain a file called “user_info” that contains all user information (firstname, lastname, user id, password, course name, course number, course time). When a user is added to, or deleted from the system this file should be modified to show these changes.
  7. A copy operation to copy a specified file from a specified user’s filesystem to another specified user’s filesystem.
  8. An operation to locate a specified file in a specified user’s filesystem.
  9. A print operation that prints the information for a user’s filesystem.
  10. An operation to print the network information for the system you are using.
  11. An operation to modify the permission(s) (read,write, execute) for owner, group, and/or others for a specified file in a specified user’s filesystem.
  12. An option to open a linux terminal.
  13. Add a specified user’s to a group.
  14. Remove a specified user’s membership from a specified group.
  15. An operation to allow a user to rename a file.
  16. An operation to move a user’s file to another directory or filesystem.
  17. Change the ownership of a directory.
#!/bin/bash
clear
#reading user and group id numbers from the passwd file
	while read ids
	do
	puid=$uid
	  uid="$(echo $ids | cut -d: -f3)"
	  gid="$(echo $ids | cut -d: -f4)"
	if [ $puid -le $uid ]
		then
		uid=$((uid+1))
		gid=$((gid+1))
	fi
	done < /etc/passwd
#opening log file
	date=$(date)
	echo "MyFileSystem.txt opened. Date: $date" >> MyFileSystem.txt
#function to get folder path of a user
function foldername {
while read myline
do
   usern="$(echo $myline | cut -d: -f1)"
   foldn="$(echo $myline | cut -d: -f6)"
	if [ $usern = $username ]
		then
	echo $foldn
	path=$foldn
	fi
done < /etc/passwd
}
echo "All your operations are save in a file called MyFileSystem.txt"
#Main menu loop
while :
do
echo " MAIN MENU "
echo "1. Create New directory in a specified user's filesystem"
echo "2. Add new user to the system in a specified home directory"
echo "3. Delete User's filesystem"
echo "4. Delete specified files from a specified user's filesystem"
echo "5. Copy a file from a user to a different user"
echo "6. Locate a file"
echo "7. Print information for a user"
echo "8. Print the network information"
echo "9. Modify the permissions for a file/folder"
echo "10. Open a Linux Terminal"
echo "11. Add a user to a group"
echo "12. Remove a user from a group"
echo "13. Allow a user to rename a file"
echo "14. Move user's file to another directory or filesystem"
echo "15. Change the ownsership of a directory"
echo "16. Quit"
echo -n "Please enter your choice:"
read opt
case $opt in
1)#Getting the user name and the directory name and then creating the directory
   echo "Enter username"
   read username
   echo "Enter directory name"
   read dest_path
	foldername
	echo $path/$dest_path >> MyFileSystem.txt
	mkdir $path/$dest_path;;
2) #Getting all the information from the user and creating the user also saving the information in to the user_info file
   echo "Enter username"
   read username
   echo "Enter password"
   read password
   echo "Enter directory name"
   read directory
   echo "Enter the class name"
   read classname
   echo "Enter class number"
   read classno
   echo "Enter class time"
   read classtime
	   newline=$username:$password:$uid:$gid::$directory/$username:$classname:$classno:$classtime
	echo $newline >> user_info
	echo $newline >> MyFileSystem.txt
	useradd -p $password -d $directory $username;;
3)#removing users filesystem looks it up in passwd with the username
   echo "Enter username"
   read username
	foldername
	echo $path >> MyFileSystem.txt
	rm $path;;
4)#removing a file from a users filesystem. Gets the filesystem path from the function above
   echo "Enter file name"
   read filename
   echo "Enter the username"
   read username
	foldername
	echo "rm $path/$filename" >> MyFileSystem.txt
	rm $path/$filename;;
5)#copying a file from a users directory to anywhere you want
   echo "Enter First username"
   read username
   echo "Enter the file name"
   read filename
   echo "Enter the destination path"
   read copyafile
	foldername
	echo $path/$filename $copyafile >> MyFileSystem.txt
	cp $path/$filename $copyafile;;
6)#finding a file in the system
   echo "Please enter the file name:"
   read search
	echo "find -name $search" >> MyFileSystem.txt
	find -name $search >> MyFileSystem.txt
	find -name $search;;
7)#showing user information both from /etc/passwd and user_info files
   echo "Enter username:"
   read username
	echo "From /etc/passwd:"
	echo `grep $username /etc/passwd`
	echo "From user_info file:"
	echo `grep $username user_info`
	echo "From /etc/passwd:" >> MyFileSystem.txt
	echo `grep $username /etc/passwd` >> MyFileSystem.txt
	echo "From user_info file:" >> MyFileSystem.txt
	echo `grep $username user_info` >> MyFileSystem.txt
	echo "";;
8) #showing network information
   ifconfig >> MyFileSystem.txt
   ifconfig;;
9)#changing permission of a file
   echo "Enter the file name to change permissions with the path"
   read chmodfile
   echo "Enter the permission"
   read permission
	echo "chmod $permission $chmodfile" >> MyFileSystem.txt
	chmod $permission $chmodfile;;
10)#opening terminal
    gnome-terminal;;
11) #assigning a user to a group
    echo "Enter the username"
    read usern
    echo "Enter the group name"
    read groupn
	echo "usermod -a -G $groupn $usern" >> MyFileSystem.txt
	usermod -a -G $groupn $usern;;
12)#taking a user out of a group
    echo "Enter the username"
    read username
    echo "Enter group name"
    read groupname
	echo "usermod -G $groupname $username" >> MyFileSystem.txt
	usermod -G $groupname $username;;
13)#chaning the ownership of a file to a user so he can move it and then moves it
    echo "Enter the username"
    read username
    echo "Enter the file name"
    read filename
    echo "Enter the new file name with the path"
    read newfilename
    foldername
	echo "chown $username $filename" >> MyFileSystem.txt
	chown $username $filename
	echo "mv $path/$filename $newfilename" >> MyFileSystem.txt
	mv $path/$filename $newfilename;;
14)#moving a file from a users directory to anywhere you want
    echo "Enter the username"
    read username
    echo "Enter the file name"
    read filename
    echo "Enter the new path and the file name"
    read newfilename
	foldername
	echo "mv $path/$filename $newfilename" >> MyFileSystem.txt
	mv $path/$filename $newfilename;;
15)#changing the ownership of a file
    echo "Enter the new user"
    read newuser
    echo "Enter directory with the path"
    read file
	echo "chown $newuser $file" >> MyFileSystem.txt
	chown $newuser $file;;
16)#Exiting the program
    newdate=$(date)
	echo "Exiting Program on $newdate" >> MyFileSystem.txt
	exit;;
esac
done

Download





Other Posts

  1. I want to quote your post in my blog. It can?
    And you et an account on Twitter?

  2. I love the tips on this site, they are always to the point and just the information I was looking for. Its hard to find good content these days in the world of spam and garbage sites.

  3. Actually,good post. thx

  4. Hopefully more people can learn from this post what I have. A great contribution to the world at large.

  5. I glad reading your blog post. Thank you so much for share nice information.

  6. Please don’t take this the wrong way. I think your overall ideas are fine but you might want to put a little more thought into your next posts. I say this becuase it seems like your writing style has gone downhill a bit as opposed to your previous posts. – C.

  7. Genial fill someone in on and this enter helped me alot in my college assignement. Gratefulness you as your information.

  8. Very nice article I love your blog keep up the good posts

Leave a Reply