NC State Computing
Center

Helpful Unix Commands to Know

This section includes other unix commands that will assist you as you use the system.

The more command

In unix, if a file is a text file, you can list the contents of the file to your screen using the more command. The more command lets you scroll through the file, though no editing can be done. The format is more filename.

	unity% more "example2" [return]
Since this is a short file, there is only one screen of data. However, had it been a larger file, the file would have scrolled by on your screen one page at a time. To see the next screen, press your space bar. To see one line at a time, use your return key. You would type a "q" if you wanted to break out of a long file.

There are two other commands called tail and head that allow you to look at the first or last "n" number of lines in a file. The format for the command is tail -n filename or head-n filename; replace "n" with the number of lines you want to view. You can change the number of lines displayed by using optional parameters; by default you are shown the first or last ten lines:

	unity% tail -5 filename [return] (last 5 lines)
	unity% head -4 filename [return] (first 4 lines)
The head and tail commands are helpful if you are trying to locate a file and have several with similar names. You could look at the beginning or ending of the file and hopefully figure out which is the correct file you need.

The more command can also be used with other commands such as ls to allow you to view, one screen at a time, all the files in a directory (if you have more than one screen of files). The format would be ls -la | more. To use the more command in this manner, we first must introduce the piping command.

The pipe command

Piping is a term used quite often in unix and other operating systems. It allows you to send the output to another command or series of commands. Said another way, the output from one command becomes the input for yet another command. When you string or "pipe" more than one command together, you create a pipeline. Information is piped from one command to another by putting a vertical bar (shown as the on your computer) between the commands on a single line.

Let's take the ls -la example mentioned above. If we had 100 files in our directory, the files would scroll by on our screen. However, we could take the output from the ls -la command and pipe it into the more command. Then, the file listing would scroll by one screen at a time.

	unity% ls -la | more  [return]
Another example would be to issue the "zwho" command and pipe the output to "sort" so that the users who are currently logged on to the system are listed to your screen alphabetically:

	unity% zwho | sort | more [return]

The cat command

There may come a time when you will want to join or append two files together; the "cat" (concatenate) command can be used to join files end-to-end and can also be used to display the file content. Take the two files we have in our "data" directory, "test1" and "example2" and "cat" them together.

	unity% cat test1 file2_test  [return]

		This is file1.
 		This is file2. (or whatever sentence you had in these files.)
	unity%
Notice that this command simply listed the contents of the file to the screen; it did not join them together in a more permanent sense. However, the cat command is normally used to join two files together and create a third file.

Using the ">" sign allows you to redirect the output of the cat command. This process of redirection is used frequently in unix. Because the screen is the standard input and output for a unix command, unix expects you to input information from your keyboard and it directs the output to your screen. Redirection allows you to change the standard input and output sources. In this next example, you change the output destination so that it is a file rather than the screen.

	
	unity% cat test1 file2_test > file3 [return] 
If you do an ls, you will see that you have a file called file3 (the two files combined).

Another example of redirection is:

	
	unity%  ls -la > file.list [return]
This example takes the output from the ls -la command and redirects it to a file called file.list rather than to the screen.

The alias command

Many unix commands tend to be cryptic or hard to remember. You can alias commands to another word or character that is easier for you to remember. The format for this command is

alias new_command unix_command.

On Unity, you will find that many commands have already been aliased for you (for example, the "rm" command has been aliased to "rm -i"). Another way to use aliases would be to set up an alias that changes you into another directory. This would eliminate you having to "col directory_path" everytime you wanted to access this file space. (You would place this alias in your .mycshrc file.) For example, suppose you have access to filespace where your web documents are located. To access this space, you enter

cd /afs/unity/project/www/ncsu/testspace

You could create an alias called "webspace" and reference the command below. Be sure to include the command in single quotes.

       alias webspace 'cd /afs/unity/.....'

The grep command

The command "grep" searches a file for the lines which contain a given character string. You may find "grep" useful for listing all files which contain a certain character pattern. When you use "grep", it will list each line within a file that contains the specified pattern. The format for this command is

grep string_to_find filename;

the following example searches the "man" pages on the "mt" (mount) command for every line that contains the word "tape":

	
	unity% man mt | grep tape [return]
The grep command is also be used to search a file for a particular character string such as a "userid" or "name."

Checking disk usage

There are two commands you can use to check your quota, "fs lq ." and "quota". Use whichever one you prefer; both provide you with the same information. Since users of the Unity realm receive only a limited quota (8 MB for students and 10 for faculty and staff), it is important to check your quota periodically to make sure you are not close to exceeding your quota. If you go over, you will not be able to get to your files without first deleting some old ones.

	unity% fs lq . [return]
	Volume Name            Quota    Used    % Used   Partition
	users.noell            30000   20216       67%         73%
	
	unity% quota[return]
	Volume Name            Quota    Used    % Used   Partition
	users.noell            30000   20216       67%         73%
In the above example, we have used 68% of our assigned disk quota. The 73% indicates partition usage, a partition being how users are grouped together on a very large disk. You need only be concerned with your quota usage (% used); the system administrators worry about the partition usage.

The date command

The date command displays the current date and time:
	unity% date [return]
	Mon Jul 29 10:41:23 EDT 1996

Go on to next section, Customizing your environment

Return to Table of Contents