Unix: Final word

Other useful commands

This chapter is just an alphabetically sorted list of Unix commands that did not fit anywhere else in this ebook. The only criteria for including a command here is that it generally considered part of the Unix operating system, is widely distributed, and that I myself use it frequently.

clear

If your terminal is cluttered, it may not be easy to see what is going on. The following command will clear the screen:

$ clear

This will clear all text and leave you with the command line prompt at the top of the terminal window.

Managing disk space

The df (disk free) command reports on the space left on the file system.

The df command without any arguments shows the entire file system disk space using the default 1K-blocks.

$ df
Filesystem            1K-blocks     Used Available Use% Mounted on
udev                   32935544        0  32935544   0% /dev
tmpfs                   6597712   680740   5916972  11% /run
/dev/mapper/vg01-root  34219840 26103864   6359740  81% /
tmpfs                  32988552        0  32988552   0% /dev/shm
tmpfs                      5120        0      5120   0% /run/lock
tmpfs                  32988552        0  32988552   0% /sys/fs/cgroup
/dev/mapper/vg01-www   47926152  8146168  37322384  18% /www
/dev/mapper/vg01-home  23897980 10187504  12473484  45% /home
cgmfs                       100        0       100   0% /run/cgmanager/fs
tmpfs                   6597712        4   6597708   1% /run/user/1003

To get the same overview in a human-readable format, use the -h flag:

$ df -h
Filesystem             Size  Used Avail Use% Mounted on
udev                    32G     0   32G   0% /dev
tmpfs                  6,3G  665M  5,7G  11% /run
/dev/mapper/vg01-root   33G   25G  6,1G  81% /
tmpfs                   32G     0   32G   0% /dev/shm
tmpfs                  5,0M     0  5,0M   0% /run/lock
tmpfs                   32G     0   32G   0% /sys/fs/cgroup
/dev/mapper/vg01-www    46G  7,8G   36G  18% /www
/dev/mapper/vg01-home   23G  9,8G   12G  45% /home
cgmfs                  100K     0  100K   0% /run/cgmanager/fs
tmpfs                  6,3G  4,0K  6,3G   1% /run/user/1003

There is also a -m flag to show 1M-blocks:

Use the -T flag to see the type of file system. To see only a specific type of file system, use the -t (lower case “t”) flag followed by the type of file system. For example, to see the free space on major partitions in a human readable format, use:

$ df -h -t ext4
Filesystem             Size  Used Avail Use% Mounted on
/dev/mapper/vg01-root   33G   25G  6,1G  81% /
/dev/mapper/vg01-www    46G  7,8G   36G  18% /www
/dev/mapper/vg01-home   23G  9,8G   12G  45% /home

A full stop shows the current file system. For example, to find out how much space is left on the partion holding the files for the web server, type:

$ cd /www
$ df .
/dev/mapper/vg01-www   47926152  8146168  37322384  18% /www

While the df command reports how much disk space we have, the du (disk use) command reports how much disk space is being consumed by the files and folders.

The du command outputs the number of blocks used by each file or directory given as argument. The default block size 1K). This is useful if you are running short of space and you want to find out which sub-directory uses up most space. Example of use:

$ du -s *

The -s flag will display only a summary (total size) and the * means check all files and directories.

Like the df command, there is a -h flag to display the sizes in a human-readable format:

$ du -s -h *
112K	bin
664K	configfiles
203M	db_backups
5.7G	drush-backups
112K	pareview
2.2G	progressive
2.2M	sink
4.0K	z_tegn.txt

file

file classifies the named files according to the type of data they contain, for example ASCII (text), pictures, compressed data, etc.. To report on all files in your home directory, type:

$ file *

history

The shell keeps an ordered list of the most recent commands that you have entered. Each command is given a number according to the order it was entered. To see the list, type:

$ history

In most modern shells, including bash, you can use the exclamation character (!) as a shortcut to recall commands from the history

You can set the size of the history buffer by assigning to the shell variable HISTSIZE. Example:

$ HISTSIZE=200

Put this in you .profile and export it if you want the change to be permanent.

ln

The ln command let you create file system links. The -s flag creates an symbolic (as opposed to a hard) link. Example:

$ ln -s /usr/share/phpmyadmin phpmyadmin

A symbolic link points to another file or directory, while a hard link is an additional directory entry for the same data. Deleting the target of a symbolic link renders the link useless. Deleting a hard linked file only removes the directory entry, and the data is not removed until all directory references to it is deleted..

who

If you're using Unix on a multi-user machine, you may want to know who, in addition to yourself, are logged in.

The command who will list the users that are currently logged in. The list has the following four fields: user name, tty/pts number, date and time of log-in, and the serial port or the hostname of the machine used to connect.

$ who
gisle    tty3         2012-08-07 05:33 (:0)
sally    pts/0        2012-09-16 16:44 (hagbart.nvg.ntnu.no)
ellen    pts/1        2012-09-29 13:20 (slagelg.uio.no)
john     pts/2        2012-10-15 09:04 (login.roztr.com)

The letters “tty” refers to a teletype machine, which was used as a console for Unix systems back in the stone age. Teletype machines doesn't exist any more. Today, when a user is connected to a tty, it simply means that he or she is connected to the computer via a serial port (i.e. he or she is in the same building as the computer). The letters “pts” stands for pseudo-terminal slave. If a user is connected via a pts, he or she is using a network connection and may be using the computer from across the globe.

Obviously, if you're using Unix on your own personal computer, there are no other users, and the output from the who command is less interesting.

Summary

Command Meaning
clear clear the terminal screen
df report file system disk space usage
du estimate file space usage
file determine file type
history show command history list
who list users currently logged in