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 with my;

    • ls *.txt: list all file names ending with .txt (including the .);

    • ls /etc/*.conf: list all files in the /etc directory ending with .conf.

    • ls */*: list all files in all immediate subdirectories.

  • ? will match a single character in a filename. For example:

    • ls myfile?.txt: lists myfile0.txt, myfile1.txt, myfileq.txt, etc.

  • [abZ] will match the letter a, {\em or} the letter b, {\em or} the letter Z. For example:

    • ls myfile[abc].txt: lists myfilea.txt, myfileb.txt and myfilec.txt.

  • [a-z] matches all letters in the range a - z, e.g.:

    • ls myfile[A-K].txt: lists myfileA.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.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.txt means that the owner of myfile.txt can 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 myprogram means that everybody can execute the program myprogram, but only the owner can overwrite it. Again, this is often the default for programs.

  • rwxr-xr-x mydirectory means that everybody can see which files reside in mydirectory, 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.

2.5.1.5. Hidden files#

To avoid cluttering the screen whenever you type ls, the convention is that all files or directories with a name that starts with a single . are considered hidden.

There is nothing special about hidden files and directories themselves, you can use them (cd into them, rename them, delete them, etc.) just like regular files and directories. It is only certain programs, such as ls but also the graphical file explorer in the desktop environment, that have builtin logic to names starting with a . by default.

A common use case is for programs to store their configuration options in a hidden file or directory (usually in your home directory), so that you don’t notice them unless you specifically look for them.

For example, a version control system like git, can store information about a repository in a hidden .git/ directory in the repository root. Bash stores various configuration files in hidden files directly in your home directory. Let’s take a look.

Ensure you are in your home directory, and list the files there, e.g., with ls or ls -l,

$ cd ~
$ ls

You should not see any file or directory names starting with a . in the list output.

If you do want to see them, you will have to use ls -a or ls -la (the -a switch indicates you want to see all files).

$ ls -a

You should now see the hidden files and subdirectories in your home directory, including several ones related to Bash, such as .bashrc and .bash_history. You can inspect these files just as any other file, e.g.

$ less .bash_history

Note that with the -a switch ls also lists the current directory . and parent directory .., which also comply with the rule of the hidden names and are therefore not included in the listing by default.