Files
Contents
Types
We have 3 different types of files in Unix.1) An ordinary file is a file on the system that contains data, text, or program instructions. When we execute a command such as: date > 1.txt We are creating an ordinary file. 2) Directories store both ordinary files and special files. They are similar to folders in the Windows system. We have created folders in a previous section. 3)Special Files There are different kinds of special files. These are files that you normally can't read ( cat command will not work these ). One example of special file is called a device file and we can find these in the "/dev" folder. These files store information about the hard drive, cd rom and other devices. The file "/dev/null" is a special file. Anything we write to this file is discarded by the system. Other special files are links to other files. We shall study this later on in this section.
Permissions
The "chmod" command is used to change permissions of a file. A file on the Unix system has read, write and execute permissions. There are 3 groups for which these permissions can be applied. The user/owner who created the file. The group that the user is part of and others which refers to everyone in the organization. Example: $ ls -l total 0 -rw-r--r-- 1 a4m13w7 Domain Users 0 Jun 10 09:50 new.txt drwxr-xr-x+ 1 a4m13w7 Domain Users 0 Jun 9 18:06 sub1 drwxr-xr-x+ 1 a4m13w7 Domain Users 0 Jun 9 18:07 sub2 drwxr-xr-x+ 1 a4m13w7 Domain Users 0 Jun 9 21:58 zipped The first character "d" denotes if the file is a directory or not. Then we can have "rwx" to denote the read, write , execute permissions. There are 3 sets of these permissions mapping to user, group and others. We can change permissions. There are 2 ways to do this. One is by symbolic mode. chmod ug+x new.txt $ ls -l total 0 -rwxr-xr-- 1 a4m13w7 Domain Users 0 Jun 10 09:50 new.txt The "ug" means user and group and the "+x" means add execute permission. The command adds the execute permission for user and group. The letter "o" means others . We have "r", "w", "x" for read write permissions. $ ls -l 1.txt -rw-r--r-- 1 Deller None 28 Dec 26 18:12 1.txt $ chmod go+wx 1.txt $ ls -l 1.txt -rw-rwxrwx 1 Deller None 28 Dec 26 18:12 1.txt $ chmod u+r,go+wx 1.txt This adds the read permission for the owner and the write, execute permissions for the group,others. We can also delete permissions from the file with the below syntax. $ chmod u-r,g-r 1.txt The command "chmod go+wx" adds the "write" and "execute" permissions to the "group" and "others" . Another way to use this command is as follows: $ ls -l 1.txt -rw-rw-r--+ 1 Deller None 0 Oct 2 10:47 1.txt $ chmod u=r,g=rx,o=w 1.txt $ ls -l total 0 -r--r-x-w-+ 1 Deller None 0 Oct 2 10:50 1.txt The "u=r" means assign only read permission for the owner and "g=rx" means assign only read,execute permissions for the group and "o=w" means assign write permission for others. The other is using binary notation. It is necessary to understand binary arithmetic. Binary Arithmetic We work in a numbering system that is base 10. If we have a number say 132. It means 1 3 2 10 power 2 10 power 1 10 power 0 We are going to use apples as objects. So we have 100 apples and 30 apples and 2 apples. As we go one place to the left then the base power increases. The range of digits in our base 10 is form 0 to 1 less than the base so from 0 to 9 . Computers work with base 2 . So we only have 2 digits to work with and that is 0-1 . Let's see what how many apples we get with the following binary number of 101. 1 0 1 2 power 2 2 power 1 2 power 0 We get 1*4 + 0*2 + 1 *1 = 5 apples If we have 3 digits for our binary number then the conversion are: 000 0 001 1 010 2 011 3 100 4 101 5 110 6 111 7 Let's assume we have the file "new.txt" -rw-rw-r-- 1 a4m13w7 Domain Users 0 Jun 10 21:37 new.txt To add execute permissions to the user and group our permissions need to be of the form: rwxrwxr-- We substitute the letters by 1 and the entries without any letters by 0 . This gives us: 111 111 100 This converted to decimal gives us 774 chmod 774 new.txt We can verify the new permissions. -rwxrwxr-- 1 a4m13w7 Domain Users 0 Jun 10 22:43 new.txt Folder Read , Write , Execute permissions on a folder. These permissions have the following meanings when applied to a folder. Read We can list the files in the folder . If we "cd" to the folder then we can also list the files. $ ls -l drwxr-xr-x+ 1 user None 0 Jan 16 22:58 temp1 $ chmod 333 temp1 $ ls temp1 ls: cannot open directory 'temp1': Permission denied $ cd temp1 $ ls ls: cannot open directory '.': Permission denied Write We can add or delete files in the folder. $ chmod 555 temp1 $ cd temp1 $ ls 1.txt $ touch 2.txt touch: cannot touch '2.txt': Permission denied Execute This decides if we can enter the directory. $ pwd /home/Deller/cs160/1/search $ ls -l total 0 drwxrwxrwx 1 Deller None 0 Dec 31 19:31 1 drw-rw-rw- 1 Deller None 0 Dec 31 19:46 first $ chmod 777 1 Deller@DESKTOP-DNEP4MC ~/cs160/1/search $ ls -l total 0 drwxrwxrwx 1 Deller None 0 Dec 31 19:31 1 drw-rw-rw- 1 Deller None 0 Dec 31 19:46 first $ cd first bash: cd: first: Permission denied $ cd 1 We are unable to enter the folder "first" as it's execute permissions are missing. Sticky Bit This is a bit that is set on a folder. What it means is if a user enters this folder then they can only delete modify their own folders and files. On the hills server the "/tmp" folder has the sticky bit turned on ( denoted by the letter "t" ) . [amittal@hills /]$ pwd / [amittal@hills /]$ ls -l | grep tmp drwxrwxrwt. 9 root root 4096 Jan 3 06:01 tmp The "t" at the end of the permission string above denotes the sticky bit. #We go to some other folder not created by us. #and try to create a file. We can't. [amittal@hills hsperfdata_galforte]$ pwd /tmp/hsperfdata_galforte [amittal@hills hsperfdata_galforte]$ touch 1.txt touch: cannot touch '1.txt': Permission denied [amittal@hills hsperfdata_galforte]$ cd .. [amittal@hills tmp]$ mkdir mine [amittal@hills tmp]$ cd mine [amittal@hills mine]$ pwd /tmp/mine #We can create a file in our own folder. [amittal@hills mine]$ touch 1.txt [amittal@hills mine]$ The sticky bit is turned on with the command: # chmod +t allAccess/ and turned off with the command:
Links
We can have links in Unix. To understand links we need to understand the concept of inode. An inode is a structure in the filesystem that keeps track of a file such as where the contents are on disk, the size and so on. There are 2 kinds of links in Unix: hard and soft.Hard Links
Let's create a file "1.txt" in a folder called "links" . $ echo "Learning Unix" > 1.txt $ ln 1.txt hard_link_to_1_txt $ ls -l total 1 -rw-r--r-- 1 Deller None 14 Jan 5 23:33 1.txt We notice that the link count is 1 . $ ls -l total 2 -rw-r--r-- 2 Deller None 14 Jan 5 23:33 1.txt -rw-r--r-- 2 Deller None 14 Jan 5 23:33 hard_link_to_1_txt Here the link count is 2. Also note that the inode number is going to be the same for both the files. $ cat 1.txt Learning Unix $ cat hard_link_to_1_txt Learning Unix Both the files have the same content because there is only 1 inode pointing to only 1 place on the hard drive. We can modify one file and the change will be reflected in the other file. $ echo "Second line." >> hard_link_to_1_txt $ cat 1.txt Learning Unix Second line. $ cat hard_link_to_1_txt Learning Unix Second line. We can delete 1 file but the other file will stay intact and the contents will stay intact. Only the link count will be decremented. $ rm 1.txt $ ls -l total 1 -rw-r--r-- 1 Deller None 27 Jan 6 19:17 hard_link_to_1_txt #Contents are still there. $ cat hard_link_to_1_txt Learning Unix Second line. A Hard Link is similar to making a copy of a file but the contents are only in 1 place. We cannot create a hard link to a file on another file system. We can have hard links to a folder but usually need superuser permissions in order to do so. [amittal@hills ~]$ mkdir links [amittal@hills ~]$ cd links [amittal@hills links]$ mkdir dir1 [amittal@hills links]$ ln dir2 dir1 ln: failed to access 'dir2': No such file or directory [amittal@hills links]$ ln dir1 dir2 ln: dir1: hard link not allowed for directory With the "-i" option we can confirm that the inode numbers are the same for both files. $ ls -li total 2 19703248369807851 -rw-r--r-- 2 Deller None 14 Jan 7 06:51 1.txt 19703248369807851 -rw-r--r-- 2 Deller None 14 Jan 7 06:51 hard_link_to_1_txt
Soft Links
A Soft Link is like a short cut in the Windows system. Again there is only 1 copy of the file somewhere.$ echo "Learning Unix" > 2.txt Let us create a soft link. $ ln -s 2.txt soft_link_to_2.txt $ ls -l total 1 -rw-r--r-- 1 Deller None 14 Jan 6 19:26 2.txt lrwxrwxrwx 1 Deller None 5 Jan 6 19:27 soft_link_to_2.txt -> 2.txt Doing a "ls" shows that the "soft_link_to_2.txt" is pointing to "2.txt" . Doing a "cat" shows that they have the same contet. $ cat 2.txt Learning Unix $ cat soft_link_to_2.txt Learning Unix The soft link is essentiall getting the contents from "2.txt" . If we make a change to soft_link_to_2.txt then we are essentially changing the file "2.txt". $ echo "Second Line" >> soft_link_to_2.txt $ cat 2.txt Learning Unix Second Line $ cat soft_link_to_2.txt Learning Unix Second Line This creates a problem. $ rm 2.txt The soft link still points to a file that does not exist. $ ls -l total 0 lrwxrwxrwx 1 Deller None 5 Jan 6 20:03 soft_link_to_2.txt -> 2.txt $ cat soft_link_to_2.txt cat: soft_link_to_2.txt: No such file or directory Removing the soft link does not create a problem. $ ls -l total 1 -rw-r--r-- 1 Deller None 14 Jan 7 06:21 2.txt lrwxrwxrwx 1 Deller None 5 Jan 7 06:21 soft_link_to_2.txt -> 2.txt $ rm soft_link_to_2.txt $ cat 2.txt Learning UnixHard vs Soft
When should we use hard vs soft links. It depends on the application. Hard links do not work across different filesystems and need permissions for directories. Soft links have the rist that the original file can be deleted with the soft link not pointing to anything. However they can point to another file on another filesystem. Links are useful for organization and naming. Let's say we have a long path to a log file.
/Applications/someapp/somefolder/http/log.txt We can creat a soft link of the form: /dev/log.txt to point to the long path.