Lesson 2 | File attributes and access permissions |
Objective | Use the ls Command and its options to list File Ownership and Permissions Information. |
Use ls Command Options to list File Ownership and Permissions Information
For determining access to a file or directory (or any other resource managed like a file, which under UNIX means essentially everything),
the following attributes are crucial:
- Every file has an owner[1]
- Every file has a group[2].
-
Every file has a set of access permissions that regulate access to the file on the owner, group, and "everyone" levels. The interpretation of these access permissions varies according to the type of file (in other words, the permissions may mean something different for a file, directory, or device).
Throughout this course, we will use the term
file instead of
file or directory or
other resource. The ls command with the –alg option lists the files in the current directory, showing the permissions, the owner, and the group of the files. The –a option is necessary to see the "hidden" files whose names begin with a dot. The output takes the following form:
PERMISSIONS LINKS OWNER GROUP SIZE MODTIME NAME
so that, for example, the list
-rw-r--r-- 1 jeremy math 304363 Apr 7 13:21
Template.gz
describes a file with:
- Permissions -rw-r--r--
- One link
- Owner jeremy
- Group math
- Size 304363 bytes
- Modified on April 7 at 1:21 p.m.
- Named Template.gz
We will discuss permissions later in this module and the LINK field later in this course. To see the owner and group of a directory, use
ls –ldg directory_name
because, without the –d option, the ls command will list the directory contents instead of the directory itself.
Use the ls Command and options to list 1) file ownership and 2) permissions information
The `ls` command in Unix is very versatile and can be used to display detailed information about files and directories, including file ownership and permissions. To achieve this, you can use various options with the `ls` command. Here's how you can list file ownership and permissions information:
- Basic `ls` command: The basic `ls` command without any options just lists the names of files and directories in the current directory.
- Using `-l` option (long listing format): To display detailed information including file permissions, number of links, owner name, owner group, file size, and time of last modification, you can use the `-l` option. The command looks like this:
ls -l
In the output of this command, the first column represents the file type and permission flags, the third column shows the file owner, and the fourth column shows the group ownership.
- Understanding file permissions: In the output of `ls -l`, the first column has 10 characters that represent the type of file and its permissions. The first character indicates the file type (e.g., `-` for a regular file, `d` for a directory). The next nine characters are grouped in threes, representing the permissions for the user (owner), group, and others, respectively. Each set of three permissions is for reading (`r`), writing (`w`), and executing (`x`).
- File Ownership: As mentioned, the file owner is shown in the third column, and the group ownership is in the fourth column in the output of `ls -l`.
Here are some additional options you might find useful:
- `-h` option (human-readable): When used with `-l`, it shows file sizes in a more human-readable form (e.g., KB, MB).
ls -lh
- `-a` option (all files): Includes all files, including those that start with a dot (.), which are hidden files in Unix.
ls -la
- `-n` option (numeric UID and GID): Displays the numeric user ID and group ID instead of names.
ls -n
By combining these options, you can customize the output of the `ls` command to suit your needs. For example, `ls -lha` would list all files (including hidden ones) with detailed information in a human-readable format.
Displaying file Ownership
To display a file's user and group ownership, use the long form of the ls command by including the -l option.
For Solaris use -lg.
$ ls -l
-rwxr-xr-x 1 root system 120 Mar 12 09:32 bronze
-r--r--r-- 1 chavez chem 84 Feb 28 21:43 gold
-rw-rw-r-- 1 chavez physics 12842 Oct 24 12:04 platinum
-rw------- 1 harvey physics 512 Jan 2 16:10 silver
Columns three and four display the user and group owners for the listed files. For example, we can see that the file bronze is owned by user root and group system. The next two files are both owned by user chavez, but they have different group owners; gold is owned by group chem, while platinum is owned by group physics. The last file, silver, is owned by user harvey and group physics.
Who owns new files?
When a new file is created, its user owner is the user who creates it. On most Unix systems, the group owner is the current* group of the user who creates the file. However, on BSD-style systems, the group owner is the same as the group owner of the directory in which the file is created. Of the versions we are considering, FreeBSD and Tru64 Unix operate in the second manner by default. Most current Unix versions, including all of those we are considering, allow a system to selectively use BSD-style group inheritance from the directory group ownership by setting the set group ID (setgid) attribute on the directory.
File Attributes - Quiz
Unix System Administration
[1]owner: The file owner is typically the user who created the file. You can change permissions only on files you own.
[2]group: A group allows an arbitrary set of users to share files. When a user is a member of the file's group, the user will have that file's group permissions.