Sharing Files in Linux

Introduction

You can share access to your Linux files with other Linux users. You can grant “read” and/or “execute” access to everybody, or you can grant “read”, “write” and/or “execute” access to a group of users.

Sharing a File with Everybody: The “Cookbook” Method

To share a file with everybody, you grant read access to the file and its directory. To make the file readable (but not writable) by everybody, enter:

chmod 644 filename

or chmod 755 filename if it is executable.

The chmod command is explained below. To make the current directory accessible, enter:

chmod 755 .

The period is the current directory. If the file is in a subdirectory, all of the directories involved must be accessible. Shift to each of the parent directories using:

cd ..

and repeat the chmod command until you get to your login directory.

The person who wants to access the file can use:

~your_name

to refer to your login directory. For example, if the file were in your login directory, that person could copy the file to a file by the same name in his or her current directory by entering:

cp ~yourname/filename .

The ls -l Command

To see the access permissions for a file, enter:

ls -l filename

The response is something like:

-rw-r--r-- 1 yourname group 4779 Jun 28 10:56 filename

The meaning of this output line is:

  • - normal disc file—d used if directory or other letters for various special files
  • rw- owner can read or write file

    the - would be x if file were executable

  • r-- members of the file's group can read file
  • r-- anyone can read the file
  • 1 number of "hard links" to the file (usually 1)
  • your_name name of the file's owner
  • group name of the file's group
  • 4779 size of the file in bytes
  • Jun 28 10:56 when file was last changed
  • filename name of the file

To delete a file, a person must have write access to its directory. For a directory, a person must have execute access to use anything in the directory, read access to list the contents of the directory with an ls command, and write access to add or delete files in the directory.

The chmod Command

chmod stands for “change mode”. The three-digit number after chmod controls the file's access permissions. The first digit controls the access for you (you could remove your own write access to avoid accidentally changing a file); the second digit controls the access for a group of users (how to set up a group is explain later); the third digit controls the access for everyone else. Each access type is assigned a number:

  • 4 Read access
  • 2 Write access
  • 1 Execute access

The value of a digit is the sum of the numbers for the accesses you are granting.

Setting up a Group

To set up a group, you must do the following:

  1. Select a group name: up to eight lower case letters and digits starting with a letter.
  2. Select a group administrator: any one person in your group what grants permission for others to join the group.
  3. Make the group: The group administrator goes to http://www.cae.wisc.edu/cae-auth/ and logs in. (This page is usually reached by clicking on "CAE Account Management" at the lower left of CAE's homepage.) Use "Create a New Group" under "Group Memberships" at the left. The group administrator can edit groups using this same page.
  4. Wait 10 minutes: Changes to a group do not take immediately.

To send e-mail to a group, mail to:

group/group-name@cae.wisc.edu

Note: Everyone in the group must have a Unix account at CAE.

The chgrp Command

To make a file accessible to a group that is not your default group, you must change the file's group assignment. chgrp stands for “change group”. The form of the command is:

chgrp groupname filename

To change the group for all the files in a directory (including subdirectories), enter:

chgrp -R groupname directoryname

To ensure that the group owns all new files and subdirectories created within a directory, enter:

chmod g+s directoryname

To see the current group of a file, enter:

ls -lg filename

To change your current default group for the remainder of your session, enter:

newgrp groupname

To change your default group permanently, mail a message requesting the change to HelpDesk@cae.wisc.edu.

Note: you cannot change your default group by entering a newgrp command at the end of your .cshrc file. Each newgrp command starts a new subshell which in turn reruns .cshrc. Thus, a newgrp command in .cshrc results in an infinite loop.