2.5.1. Files, and their properties#
In Listing the content of a directory you learned the basic usage of the ls command to âlistâ all files in a directory.
A very useful option to the ls command you saw before is -l,
which tells ls to give more information on each file or directory:
$ ls -l /bin
total 6632
-rwxr-xr-x 1 root root 818232 2010-04-19 03:51 bash
-rwxr-xr-x 3 root root 30200 2010-02-08 11:54 bunzip2
-rwxr-xr-x 1 root root 1269432 2010-04-22 22:04 busybox
-rwxr-xr-x 3 root root 30200 2010-02-08 11:54 bzcat
lrwxrwxrwx 1 root root 6 2010-07-07 11:48 bzcmp -> bzdiff
-rwxr-xr-x 1 root root 2140 2010-02-08 11:54 bzdiff
lrwxrwxrwx 1 root root 6 2010-07-07 11:48 bzegrep -> bzgrep
-rwxr-xr-x 1 root root 4874 2010-02-08 11:54 bzexe
(etc.)
First of all, it shows you how many blocks the entire directory takes up on the filesystem.
A block does not have the same size on all machines, but on Linux PCs itâs usually one kilobyte (1024 bytes).
The directory /bin seems to take up 6632 blocks, or roughly 6.6 megabytes.
Next, for each file, ls -l now shows the file type, the first character of the string on the left.
The file type indicates whether the file is a plain file (indicated by -), a link (l, see below) or a directory (d). There are some other file types, like b (block device) and c (character device), but you will not encounter these regularly, so donât worry about them.
Some other important information given by ls -l is the user and group of the file (here: user root, group root); the date and time of creation; and, of course, the filename.
2.5.1.1. Wildcards#
Above, you used ls to list files. However, often you want to limit the number of files you see, for example when you know that youâre looking for a filename containing a certain word. If this is the case, you can call ls with {\em wildcards}. The most important wildcards are:
*will match any piece of filename. For example:ls *: list all files in the current directory;ls my*: list all file names starting withmy;ls *.txt: list all file names ending with.txt(including the.);ls /etc/*.conf: list all files in the/etcdirectory ending with.conf.ls */*: list all files in all immediate subdirectories.
?will match a single character in a filename. For example:ls myfile?.txt: listsmyfile0.txt,myfile1.txt,myfileq.txt, etc.
[abZ]will match the lettera, {\em or} the letterb, {\em or} the letterZ. For example:ls myfile[abc].txt: listsmyfilea.txt,myfileb.txtandmyfilec.txt.
[a-z]matches all letters in the rangea - z, e.g.:ls myfile[A-K].txt: listsmyfileA.txtâŚmyfileK.txt.
You can always use wildcards in the Unix shell when you have to specify a filename.
Exercise 2.49
List all files in /usr/bin starting with a z. Also list all files in /usr/bin that contain the string text in their name.
2.5.1.2. Links#
A link (indicated by an l in the output of ls -l) is not a file, but a pointer to a file.
It can also be a pointer to a directory.
Links can be useful for when you want to have a file with changing content but a fixed name.
For example, you could create files news_01_01_2010, news_02_01_2010 and so on, and then have a link news_today which always points to the latest file. These links can be either hard or soft (also called symbolic). A hard link means that there is actually one physical file with two names; if you throw away the original, the other link still points to the same content. A soft link is different; you can remove the link without affecting the file itself, but if you remove the original, the link is left dangling. Soft links are probably the only ones you will be using.
In the directory listing of /usr/bin above,
you can see that bzcmp is a link to bzdiff,
as indicated by the arrow -> in the filename part (bzcmp -> bzdiff).
That means that if you run the program bzcmp,
really the program bzdiff is run.
Note that you can only see the arrows with the linked names if you include the -l option of ls.
You can create links using the ln command (for link).
Exercise 2.50
Try:
$ cd
$ ln -s /etc/passwd passwords
This will create a symbolic link in your home directory which points to the password file in the directory /etc. You can now use this link as if it were the password file itself:
$ less passwords
After youâre done looking through the password file, remove the link:
$ rm passwords
Fortunately, this will only remove the link, not the real password fileâŚ
If you donât specify a destination, ln will create a link in the current directory with the same name as the original file. Try:
$ ln -s /usr/share/common-licenses/GPL
2.5.1.3. File permissions#
Next to the file type, ls -l shows the file permissions, the row of râs, wâs and xâs. This list really consists of three groups of three characters each. The first three characters contains the permissions for the user, the owner of the file; the second three are for the group, the group of the file; and the last three are for others, everybody else. The idea of a user group will be explained below.
The three permission flags are:
r: if this flag is present, the file can be read from;w: if this flag is present, the file can be written to;x: if this flag is present for a file, the file can be executed; if itâs present for a directory, the directory contents can be read.
This might all sound a bit cryptic, so here are a couple of examples. These are the permissions you will encounter most:
rw-r--r-- myfile.txtmeans that the owner ofmyfile.txtcan read and write the file, but people in the same group and others can just read the file. This is usually the default for text files: you allow people to see the contents of the file but not to change them.rwxr-xr-x myprogrammeans that everybody can execute the programmyprogram, but only the owner can overwrite it. Again, this is often the default for programs.rwxr-xr-x mydirectorymeans that everybody can see which files reside inmydirectory, but only the owner of the directory can write new files to that directory.
Exercise 2.51
Try to list the files in the administratorâs home directory (see the Linux filesystem overview). Find out with ls -l why this doesnât work. Now try again, prefixing your command with sudo. Explain why this second method works by studying the sudo manual page[1].
You can change file permissions with the chmod command. The command, in its simplest form, looks like this:
$ chmod ugo+rwx somefile
This call of chmod gives permission to read (r), write (w) and execute (x) somefile to the user (u) of the file, its group (g) and others (o). Of course, you can specifiy subsets, e.g.:
$ chmod u+rx somefile
The + means âadd permissionâ. Itâs also possible to remove permissions, using -. For example, to make sure you are the only one able to read somefile:
$ chmod go-r somefile
Exercise 2.52
Change the permissions on your file myfile.txt so that you yourself (the user) cannot read it anymore. Verify this using cat or less. Finally change the permissions back so that you can read it again.
2.5.1.4. User groups#
As discussed above, you can assign permissions for the user of a file, the group of a file and others. The output of ls -l shows the user and group to which the files belong. In the listing of /usr/bin above, all files are owned by user root, who is in group root.
You might wonder what a group exactly is. Groups were made to make system administration easier. Say you run a company in which three project teams work on different projects. You would like the members of each team to be able to read and write the teamâs files, but members of one team shouldnât be able to read another teamâs files. The group mechanism makes this very easy, by putting users into three different groups and setting the file permissions correctly.
To see what groups you belong to, you can (for example) use id:
Exercise 2.53
Try this:
$ id
It should print your user id (that is, your user number), your group id and all groups you belong to.
If you belong to more than one group, you can decide which group your file should have. The command chgrp can be used to assign a new group to the file, for example:
$ chgrp othergroup myfile.txt
You are often part of many groups, such as the people allowed to use the modem (dialout) or that can administer the printer (lpadmin). The role of groups for sharing files with different people has been largely replaced by other mechanisms.