Linux-User message #24 from Janis Kracht to All.
Entered on 30th October, 1999 at 19:44, 243 lines.
Linux Tips 003
Under Linux, every item you see listed in a directory list is a file. That includes directories, binary (executible) files, data files, etc. You can tell what a particular listing is by looking at the first column of a long directory display. The following is a clip of a directory listing using ls -ltr -d (long format, sorted by time/date, reverse, -d lists directory entries instead of the contents of the directory).
for example:
[bbs@jkracht bbbs]$ ls -ltr -d test2
drwxrwxr-x 2 bbs bbs 1024 Oct 30 13:41 test2/
The first character describes the type of file it is: directory (d). If it is not a directory, a '-' is listed.
for example:
-rw-rw-r-- 1 bbs bbs 12 Jul 21 13:11 tst.txt
Under DOS, you are given information in directory listings regarding type, creation date/time:
Under linux, you not only see the creation date/time and type, but you also see who has read, write and execute privileges, who "owns" the file, etc.:
(permissions) (owner) (group) (size)(date/time) (name) drwxrwxr-x 2 bbs bbs 1024 Oct 30 13:41 test2/
In each listing, you see the d (or -) in the first field, followed by rwx rwx rwx.
These characters display who has read/write/execute permissions for this file. These characters list the permissions for this file in the order of user/group/everyone-else:
(user/group/everyone-else)
drwxrwxr-x 2 bbs bbs 1024 Oct 30 13:41 test2/
So here you can see that the user has read/write/execute permission, anyone in the group listed has the same permissions, and "everyone-else" who may try to access this file may only read it/execute it. Everyone-else then, cannot modify the file.
drwx rwx r-x 2 bbs bbs 1024 Oct 30 13:41 test2/ ---------- -------------- ------------ ------
d direcotry
-r who can see this
--w who can write to it
---x it is executible or not (- means not) for this person
|ownername groupname
|Creation Date/time
|filename
So, the above file, test2, is a directory, owned by user bbs. User bbs has read/write/execute permissions for this file, while anyone else in the _group_ bbs may also read/write/execute. All other persons may only read/execute.
There may be times when you would like to keep a directory "private" so that it is only visible (readable) by yourself. Or there may be times when you want a file to be only readable and not executible, etc. You can make any file on a linux system private by changing the attributes with the chmod command. i.e., Since directories are only files, you can therefore change the directory's attributes.
In order to change a files attributes, you need to understand the bit pattern of the chmod command.
Using this bit pattern, you assign a number to the user/group/everone-else fields of one or all files. It is the cumulative value of octal digits 0-7 for bits 4, 2 and 1 that defines the permissions as to who may r/w/x.
A value of 4 gives the field read access.
A value of 2 gives write access.
A value of 1 gives execute access.
If you add the values of the bits you want to change, you have the value of the field.
Sounds scary, but it's easier to see when you look at it with this table:
adding up the bits with values of bits 4, 2, and 1.
(USER GROUP EVERYONE-ELSE/USER GROUP EVERYONE-ELSE/USER GROUP EVERYONE-ELSE) xxx xxx xxx
775
4+2+1 4+2+1 4+1
rwx rwx r-x
660
4+2+0 4+2+0 0+0+0
rw- rw- ---
So to make our new directory rwx by user bbs ONLY,
Original at creation:
drwxrwxr-x 2 bbs bbs 1024 Oct 30 13:41 test2/
you would type:
chmod 700 test2
This changes the permissons to:
drwx------ 2 bbs bbs 1024 Oct 30 13:41 test2/
(4+2+1/0+0+0/0+0+0)
7 0 0
To make it readable by all, but not executible:
chmod 666 test2
drw-rw-rw- 2 bbs bbs 1024 Oct 30 13:41 test2/
4+2/4+2/4+2
6 6 6
To make it rw for everyone, but rwx only for user bbs:
chmod 766 test2
So now the permissions look like this:
drwxrw-rw- 2 bbs bbs 1024 Oct 30 13:41 test2/
4+2+1/4+2+0/4+2+0
7 6 6
There is something you learn very quickly when you run a *nix operating system.. #1, don't log in as root (superuser) to perform general tasks. If a file is removed, changed, whatever, user root can do it anywhere on the system, in any directory. Therefore user root could delete an entire directory tree "by mistake" and nothing on the system would hinder this. I.e., there is no prompt "Are you sure?", like you'd see on a DOS command like DEL *.*.
Likewise, when you use chmod to change the access of a file, it is generally a good idea to log in to the system as "non-root", for example, user janis, or user bbs to change the permisions of a file. That way, if you mistakenly change the attributes to something you didn't mean, or if you attempt to change a file that you didn't MEAN to change, your limited permissions can generally prevent total disaster <smile>.
You can change the permissions of entire directories/subdirectories with the -R switch (recursive):
chmod -R 775 /home/ftp/pub
This makes every file and directory rwx by the user that owns it, everyone in the group, and r-x (read/execute) for everyone-else.
Also, another often used switch to chmod is a+x. a+x adds the executible flag to a file while not affecting the other previously set flags (such as who can execute the file).
To use the a+x switch, you would type:
chmod a+x filename
One of the benefits once you realize that everything is considered a file under linux, is that you can make a simple text file executible with chmod, and then execute it. Of course, if the text file you do this to doesn't contain any shell commands to execute, it won't do anything <g>.. but if you create a shell script with your favorite text editor, like joe, or vi, that does contain commands you can save the file, issue the command chmod a+x filename, and then that file becomes an executible program.
Here's an example of shell script that changes the text in the file files.bbs from all upper case to all lower case:
==========start bash script==========
#!/bin/sh
cat files.bbs | tr A-Z a-z > files.new
if you save this file as say, up2low, you can then type
chmod a+x up2low
and then execute it
./up2low
By Janis Kracht Questions/Comments: Linux-user echo, or to jkracht@aye.net