Introduction to Unix commands for s420 students

Here is a brief introduction to some useful Unix commands, including examples of how to use each command. For more extensive information about any of these commands, use the man command as described below.

cat

This command outputs the contents of a text file. It can be used to read brief files or to concatenate files together.

To append file1 onto the end of file2, enter at the Unix prompt:

  cat file1 >> file2
To view the contents of a file named myfile, enter:
  cat myfile
Because cat displays text without pausing, its output may quickly scroll off your screen. Use the more utility (described below) or an editor for reading longer text files.

To print the contents of a file named myfile, enter: (this is not a standard Unix command)

  cat myfile | ansiprt
 

cd

This command changes your current directory location. By default, your Unix login session begins in your home directory.

To switch to a subdirectory (of the current directory) named myfiles, enter:

  cd myfiles
To switch to a directory named /usr/dvader/empire_docs, enter:
  cd /usr/dvader/empire_docs
To move to the parent directory of the current directory, enter:
  cd ..
To move to the root directory, enter:
  cd /
To return to your home directory, enter:
  cd
or
  cd ~

cp

This command copies a file, preserving the original and creating an identical copy. If you already have a file with the new name, cp will overwrite and destroy the duplicate. For this reason, it's safest to always add -i after the cp command which will ask for your approval before it destroys any files. The general syntax for cp is:
  cp -i oldfile newfile
To copy a file named meeting1 in the directory /usr/dvader/notes to your current directory, enter:
  cp -i /usr/dvader/notes/meeting1 .
The period (dot) indicates the current directory as destination, and preserves the old filename meeting1.

To copy a file named oldfile in the current directory to the new name newfile in a subdirectory of your home directory named mystuff, enter:

  cp -i oldfile ~/mystuff/newfile
The ~ character, or tilde, is interpreted as the path of your home directory.

Note that you must have permission to read a file in order to copy it.

du

This command reports disk usage, the amount of space taken up by a group of files. du descends all subdirectories from the directory in which you enter the command, reporting the size of their contents, and finally reporting a total size for all the files it found.

To find out how much disk space you're using in your account, switch to your home directory with the cd command, and enter:

  du
The numbers reported are the sizes of the files; on different systems, these sizes will be in units either of 512 byte blocks, or of kilobytes.

To learn which is the case, use the man command as described below. On most systems, du -k will give sizes in kilobytes.

find

The find command lists all of the files within a directory and its subdirectories that match a set of conditions. This command is most commonly used to find all of the files that have a certain name.

To find all of the files that are named myfile.txt that exist in your current directory and all of its subdirectories, enter:

  find . -name myfile.txt -print
To look in your current directory and its subdirectories for all of the files that end in the extension .txt, enter:
  find . -name "*.txt" -print
In these examples, the period (dot) represents your current directory. It can be replaced by the full pathname of another directory to search. For instance, to search for files named myfile.txt in the directory /home/user/myusername and its subdirectories, without actually being in that directory, enter:
  find /home/user/myusername/ -name myfile.txt -print
On some systems, omitting the final / (slash) after the directory name can cause find to fail to return any results.

As a shortcut for searching in your home directory, enter:

  find "$HOME/" -name myfile.txt -print

java/javac

The javac command will compile your java program. The java command will execute your java class file compiled by javac.

To compile test.java and redirectly all the error messages to a text file, error.txt, enter:

  javac test.java >& error.txt
Note: the above command will only work under csh. To change your command shell, enter:
  chsh csh
After you login again, your command shell will be changed to csh.

ln

The ln command will make a symbolic link to another directory.

Example:

  ln -s source_file myfile
OR
  ln -s /N/u/benwu/WWW s220
In the example, source_file is an existing file (which can be any existing file or directory across the file systems), and

myfile is a symbolic link to it, created by the ln command. After the symbolic link is made, you can do an operation on or execute myfile, just as the source_file. You can use normal file management commands (cp, rm, etc) on the symbolic link.
 
 

Note: once the source file is deleted or moved to a different location, your symbolic file still exists, and the only thing you can do is either delete or move it. If you try to use it for other purposes (edit, or execute, for example), the system will send a "file nonexistent" message.

When you need to remove the link, use unlink command.

You can find out more about symbolic links by typing:

man ln

lpr and lp

These commands print a file on a printer connected to the computer network. The lpr command is used on BSD systems and the lp command is used in System V. Both commands may be used on the UITS systems.

To print a file named myfile on a printer named lp1 with lpr, enter at the Unix prompt:

  lpr -Plp1 myfile
To print the same file to the same printer with lp, enter:
  lp -dlp1 myfile
WARNING: Do not print to a printer with an unfamiliar name and location; you may annoy its owners.

ls

This command will list the files stored in a directory. To see a brief, multi-column list of the files in the current directory, enter:
  ls
To also see "dot" files (files that begin with a period, such as .login), enter:
  ls -a
To see the file permissions, owners, and sizes of all files, enter:
  ls -la
If the listing is long and scrolls off your screen before you can read it, combine ls with the more utility, described later. For example:
  ls -la | more

man

This command displays the manual page for a particular command. Whenever you are unsure how to use a command or want to find out all its options, use man to view the manual page.

To learn more about the ls command, enter:

  man ls
To learn more about man, enter:
  man man
If you aren't sure of the exact command name, you can use man with the -k option to help you find the command you need. If you enter:
  man -k keyword
man will display one line summaries of each reference page that contains the keyword you specify.

mkdir

This command will make a new subdirectory.

To create a subdirectory named mystuff in the current directory, enter:

  mkdir mystuff
To create a subdirectory named morestuff in the existing directory named /tmp, enter:
  mkdir /tmp/morestuff
Note that to make a subdirectory in a particular directory, you must have permission to write to that directory.

more

This utility displays the contents of a text file, one screen at a time, waiting for you to press the spacebar between screens. This lets you read text without it scrolling quickly off your screen.

To read the contents of a file named textfile in the current directory, enter:

  more textfile
The more utility is often used for reading the output of other commands. For example, to read the output of the ls command one screen at a time, enter:
  ls -la | more
Note: Don't use more with executables (binary files), such as output files produced by compilers. Doing so will display garbage and may lock up your terminal.

mv

This command will move a file. You can use mv not only to change the directory location of a file, but also to rename files. Unlike the cp command, the original file will not be preserved.

As with the cp command, you should always use -i to make sure you don't overwrite an existing file.

To rename a file named oldname in the current directory to the new name newname, enter:

  mv -i oldname newname
To move a file named hw1 from a subdirectory named newhw to another subdirectory named oldhw (both subdirectories of the current directory), enter:
  mv -i newhw/hw1 oldhw
If, in this last operation, you also wanted to give the file a new name, such as firsthw, you would have entered:
  mv -i newhw/hw1 oldhw/firsthw

pine

This command will launch your mail program, pine. You can send your program as attachments to your professors via pine. Enter the command by itself:
  pine

pwd

This command reports the current directory path. This is useful if you have switched to a different directory, but don't remember its name or path. Enter the command by itself:
  pwd

rm

This command will remove (destroy) a file. If you add a -i after the rm command, you'll be asked to confirm each file deletion, which makes this command somewhat less dangerous. To remove a file named junk, enter:
  rm -i junk
WARNING: Your file will be gone forever! Be sure you really want to get rid of a file before you use rm.

rmdir

This command will remove a subdirectory. To remove a subdirectory named oldstuff, enter:
  rmdir oldstuff
Note that the directory you specify for removal must be empty. To clean it out, switch to the directory and (carefully!) use the ls and rm commands to inspect and delete files.

w

This command lists everyone currently logged into the computer, and includes brief information about what they are doing. To see the listing, enter:
  w

who

This command will list everyone logged into the computer, and the dialup connection or host computer from which they are connected. To see the listing, enter:
  who

This is a modified version of UITS document, Introduction to Unix commands. For more information, see the UITS publications, "Unix Commands: A Quick Guide" and "Unix: The least you need to know", as well as the online manual pages.You can also view a collection of Unix tutorials and introductory help files on the Web at

  http://www.mcsr.olemiss.edu/unixhelp/