Previous Up Next

12  Other useful commands and information

12.1  Changing user accounts

Although you will only have one Unix account from the University sometimes it might be necessary, or convenient, to change which user account you are logged into in the middle of a session (If for example someone is logged in and you need to log in to quickly show them a file or something).

While it is always preferable to just open another login to the Unix server sometimes you will want to just change to another user account. The command which does this is called “su”. And its usage is fairly simple:

% su - newusername 

Where newusername is the username you’d like to log in as. Be warned that not writing a username here will prompt you for the root (administrative user) account’s password. And all attempts to log in as root on unix.lancs.ac.uk are logged (and Admins will want to know what you were doing trying to gain control of their machine). You have been warned.

The - (dash) flag is generally used when using su as otherwise while you will change over to the new account your environment will be preserved, will generally causes issues. Use of the - flag means you will have the same environment as if you logged in as the other user instead of using su.

12.2  Finding out about your local Unix system

There are lots of commands that we’ve seen output information about the currently running system, ps and who etc. But these are more concerned with the processes and users, who could be logged into any type of Unix. There are two more commands that are helpful for finding out about the machine you’re logged into.

uname prints out the name of the operating system running on the current machine. This can be handy, but often isn’t very helpful as you will know which machine you are logged into (it can be handy for putting in scripts that will run on multiple machines however). However uname can give you a lot more information about the machine, for example:

% uname -a 

While most of this is useless to the average user it can prove quite interesting if you’re interested in the system you use.

Another interesting command to know is one that will tell you how long the system has been running, thus if you were wondering why a long process you were running died (see section 13.6 for a more likely explanation), or a number of other things, simply type:

% uptime

12.3  Links, symbolic and hard

Several times throughout the file management section the concept of links was mentioned (see section 4). They aren’t essential to the use of Unix but will certainly enhance your ability to use the system, as they’re quite handy, there are two types, both created by the ln program.

Both can be seen by using “ls -l”, symbolic links will contain the filename followed by an arrow symbol “->” then the path that the link goes to.

Hard links (since they can only be files) can be seen by value in the second column of “ls -l” which is the reference count, this is equal to the number of hard links pointing to that inode.

12.3.1  Symbolic Links

The first are symbolic links, these can be thought of as pointers in the file system. A simple example might help to illustrate:

% ln -s ~/stuff/foo bar 

This will create a symbolic link that goes between the file “~/stuff/foo” to a file called “bar” in your current directory. Now whenever you do anything to bar (such as view or edit it) it will affect “~/stuff/foo”. However it is a separate file in its own right, so deleting bar doesn’t affect foo (however if you delete foo then bar will become a “broken link” pointing at nothing).

You can form symbolic links from anything to anywhere, you can even link other directories to somewhere else, so a common trick is to place a link from your $scratch directory and $WWWHOME directories into your home directory for ease of use.

12.3.2  Hard Links

Hard links are a little more complex, and it’s worth reading “man ln” for the full description of them. Essentially a hard link is a separate directory entry that points to the same inode (an inode is essentially the bit of the disk that contains the file’s data). At this point there are simply two file names that point to the same data, and thus they are literally indistinguishable.

It’s worth noting with hard links that the data pointed to isn’t deleted until the last hard link is gone. So be very careful with them.

12.4  screen

The screen program is a “terminal multiplexer”. What this means is that using screen you can log into a machine once and then run multiple shells on it at the same time (Which gives you one for mail, one for news, one for a remote login and one for random commands for example). This is handy as it reduces the number of logins you need to a single machine (thus reducing load on that machine) and also it means if your connection dies you can simply log back in and reconnect to your screen, and all your programs will be running the same as before.

The full documentation for screen is available via “man screen” and
http://www.gnu.org/software/screen/

As a quick introduction screen can be started simply by typing:

% screen 

You will then see a quick message before you get your shell back. Except thats not the shell you had before, it’s your first shell inside screen. In its default state screen commands are prefixed with ^a (Hold down control and press ‘a’). The most common ones you’ll want are:

^a c (in other words, hold control and press ‘a’, then release those two keys and press the ‘c’ key). This will create a new shell inside screen.

^a w This will list all the shells you have open in screen. The one you are currently in will have a * before its name, ones with new output you haven’t seen will have a @ symbol in.

^a n Go to the next shell.

^a p Go to the previous shell.

^a a Will actually send your shell a control+a combination.

^a ? Will print out all the keyboard commands that affect screen.

To quit screen simply close all the shells in the normal way (with the “logout” command or “^d”).

Screen is controlled by a file called “~/.screenrc”. The two most important entries are how to change the key that controls it to something else (its normal position ^a gets in the way of some software, such as emacs). And giving it a permanent status line.

To change the key that controls it you simply need to put a line like the following one in your .screenrc

escape ^nn 

Where ‘n’ is the character you want, popular choices include ‘o’ and ‘r’ (although ^r also clashes with some things).

To make screen display a status line, listing all the currently running screens, you will need to add the following line in your .screenrc

hardstatus alwayslastline "\%Lw" 

To change screen back to its default behaviour simply delete these lines from your .screenrc

12.5  talk and write

There are several ways to contact another user logged into the same machine, firstly if they’re in the same lab as you, (check who, and also look around) you can simply go and speak to them. Secondly if you don’t need them straight away you can email.

However if you want their direct attention you can use “write” or “talk”. write is used to send a message directly to the terminal (see section 6.4) someone is logged in at, not their username or their shell. For this reason it’s advisable to use who and make sure you’ve got the right terminal.

If you want a more one-to-one real time discussion you can use talk, (actually its best to use ytalk). This will enable you to chat with another user. Be aware however that things typed into talk are not buffered until you press enter, the letters appear as you type.

Users may however elect to disable the ability for others to contact them like this, by the use of the mesg command (see section 6.9.5). Other methods of generally getting in contact with users on the same machine as you include LuBBs (see 13.11).

12.6  Looking at binary files

Sometimes you will have a binary file (i.e. one not composed of plain text) that you want to see the contents of, often to try and work out what it does. The best program for this is strings. This simply goes through a file (any file) and extracts all the parts that would display as ASCII characters in your terminal. Using it is very simple:

% strings filename | less

This will use strings on the file called “filename” and pass the output to less for viewing. This can be used on any file, although the output is often not very interesting it can be handy for analysing binary files.

Another program that is quite useful for working with binary files is od which stands for “octal dump”. This program can be used to print out the contents of a file in octal codes (or hexadecimal, or as integers). For example:

% od filename | less

Will print the contents of filename where each “word” (16 bits) is translated into an octal number. It will also print the offset from the beginning of the file for each line of these. See “man od” for more information.

12.7  Scheduling commands to run later

Sometimes you may want to run a command regularly, such as running fetchmail to get your mail every half hour or so (Remembering that fetchmail is part of homegrown and thus subject to a number of restrictions, see section 13.1) or you want to schedule a single command to run at a later time. For these two situations there are two commands. The first is cron, the second is at.

12.7.1  cron

cron is the name of a scheduling daemon that checks its crontab every minute and starts any jobs that need running. It is designed to be used for jobs that are scheduled every X interval (such as every 10 minutes, once a day, every Monday, once every 6 months), and this service is made available to regular users of the system.

% crontab -e 

The above command will open a file in your $VISUAL, this is your crontab and has to be formatted according to the rules laid out in “man cron”. Essentially it is five fields, minute, hour, day, month and day of week. Any of these fields may continue the values that should trigger it or a single ‘*’ as a standard wild-card. This is followed by the command you want to run. An example will clear this up:

0,15,30,45 * * * * date >> ~/date-stamp 

This will execute the command “date >> ~/date-stamp”, echoing the current system date into a file in your home directory, this is fairly useless but serves as an example only.

If you look at the format of the columns before it you’ll see the minutes column has the entry “0,15,30,45” and every other column has a wild-card. This means the command will run at those times past the hour on every hour, of every day, of every month.

If you wanted to run a backup script to scp a file of data from your home directory to another machine, and run it once a week at 9:00am on a Monday morning the command would look like this:

0 9 * * 1 scp ~/vital-data frank@melody.lancs.ac.uk:~/ 

This would run at the 0 minute, of the 9th hour on any day of the month, and any month of the year that was also a Monday, so on otherwords every monday at 9am. Again would make use of the fictional account “frank@melody.lancs.ac.uk”.

12.7.2  at

at is used to schedule more one off jobs, and its operation is a little different from cron. The full details are contained in “man at” but a short example follows:

First you need to invoke at and tell it the time the program should run at, so the first line you should do will be:

% at 18:58 August 6th 

This will schedule the job to run at that time and that date (you can also use “now” to mean right now, but the applications beyond testing of that are limited). at will then present you with a prompt that looks like this:

at> 

The idea is that you then type in the commands you want executed, one at a time, pressing enter after each of them. Once you have entered the last command press ^d (control and ‘d’), and you should get your prompt back.

At will at this point run the command at the specified time, and mail the results back to your user account, however at has a few other functions that come in handy:

% at -l 

The ‘-l’ (dash ell) flag will list all the jobs you currently have scheduled with the “at” command.

% at -r 976385710.a 

The ‘-r’ flag will remove a scheduled job from the list, “976385710.a” is an example of the kind of job ID that “at -l” will give you.


Previous Up Next