Basic UNIX commands

Unix Terminal Command

The most important 8 commands

ls dirname
Shows directory listing. If no directory dirname is specified, ls prints the names of the files in the current directory.

cd dirname
Change current directory. Without a dirname, it will return you to your home directory. Otherwise, it takes you to the directory named dirname.

mkdir dirname
Makes a sub-directory named dirname in the current directory.

rm filename(s)
Removes files. Careful with this one – it is irreversible. It is usually aliased ( in a user”s .cshrc file ) to rm -i which insures that rm asks you if you are sure that you want to remove the named file.

mv oldname newname
Changes the name of a file from oldname to newname. Can also be used to move a file from one directory to another, e.g. mv oldname a/oldname. After using mv, the file oldname no longer exists (at least not in its original location). Contrast with cp.

cat filename
Types a file to the screen without stopping. Usually more is better.

cp filename(s) path
Copies files from one directory/filename to another. cp f1 f2 makes a file f2 identical to f1. cp *.c src/ copies all files that end in “.c” into the “src” subdirectory. cp does not remove the original file. Contrast with mv.

more filename
Types a file to the screen, one page at a time

More Complete Alphabetical List

cd dirname
Change current directory. Without a dirname, it will return you to your home directory.
Otherwise, it takes you to the directory named dirname.

cmp file1 file2
Compares the contents of two files from eachother. Reports the first different character found, and the line nummber.

cp filename(s) path
Copies files from one directory/filename to another. cp f1 f2 makes a file f2 identical to f1. cp *.c src/ copies all files that end in “.c” into the “src” subdirectory. cp does not remove the original file. Contrast with mv.

diff file1 file2
Displays all the differences between two files or directories to the screen.

grep pattern filename
Finds all the occurrences of pattern in the file filename. grep has a sophisticated pattern matching capability – use man grep to see the details.

head –n filename
Type the first several lines of a file. If “-n´ is present, where n is an integer, the first n lines are shown. This is useful for getting the top of a large file for testing, especially if you redirect the output to a new file, e.g. head -50 big.txt > small.txt

ls dirname
Shows directory listing. If no directory dirname is specified, ls prints the names of the files in the current directory.

mkdir dirname
Makes a sub-directory named dirname in the current directory.

man name
Shows the full manual page entry for name. For example, man grep will give you the manual pages for the write command.

more filename
Types a file to the screen, one page at a time

mv filename oldname newname
Changes the name of a file from oldname to newname

pwd
Shows what directory you are currently in

rm filename(s)
Removes files. Careful with this one – it is irreversible. It is usually aliased ( in a user”s .cshrc file ) to “rm -i” which insures that “rm” asks you if you are sure that you want to remove the named file.

rmdir dirname
Removes the directory “dirname”.

tail –n filename
Type the last several lines of a file. If “-n´, where n is an integer, is present, the last n lines are shown. This is useful for checking if your program completed writing all of its expected output. See also cat, more, head.

wc filename
Counts the lines, words, and characters in a file. wc is particularly useful in conjunction with grep to count the number of occurrences of something, e.g., grep “>” seq.fa | wc can be used to count the number of sequences in a FASTA formatted sequence file.

Stringing Commands Together

One of the powerful features of the UNIX is its ability to string together commands using the vertical bar “|” or pipe operator. this is particularly useful to filter the output of one command through the more program in order to see it one page at a time, or through the grep program to extract only some of the output.

A few examples:

See a detailed directory listing, sorted by time, one page at a time
ls –alt | more

Run a program, a.pl, and see the result pagewise
a.pl | more

Get a directory listing that include only files with suffix ‘.dat’
ls –l | grep “.dat”

Count the files with suffix ‘.dat’ in the current directory
ls –l | grep “.dat” | wc