Some UNIX/linux commands

Syntax

UNIX is a command line based system. Most UNIX commands follow a standard convention:

command -options arguments

For example:

cat file.txt
ls -l *.sas
ps -f -u apj770
ps -fuapj770

Here, "file.txt" and "*.sas" are arguments, and "-l", "-f" and "-u" are options. Note that multiple options can usually be joined together, and that some options ("-u") can take additional arguments ("apj770").

Examples

mkdir, cd, touch, ls. Home directory.

Let us do the following:

  • create a directory (~/training)
    • the "~" stands for "your home directory"
  • change to it (ie. make it current)
  • create an empty file "zz.txt"
  • see the contents of the directory
mkdir ~/training
cd ~/training
touch zz.txt
ls -l
total 0
-rw-rw-r-- 1 apj770 apj770 0 Sep 28 13:18 zz.txt

Here, the touch command creates an empty file if no such file already exists. If there is such a file, it changes the date on the file to current, this is useful if you, for example, are working with files in /scratch.

pwd. Parent and current directories.

The parent directory (the one immediately above) is always referred to as "..", and the current directory is "."

  • create two subdirectories (test1 and test2):
mkdir test1
mkdir test2
ls -l
total 8
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test2
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
  • change directory to test1:
cd test1
pwd
/home/apj770/training/test1

Here, the "pwd" command shows the path to the current directory. We can, for example, list the parent directory:

cd ~/training/test1
ls -lrt ..
total 8
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:28 test2

Here, "-lrt" options specify that I wanted to list the contents in "full" format (l), and in reverse (r) order by time (t).

We also can switch directly to test2:

cd ../test2
pwd
/home/apj770/training/test2

echo, cat, output redirection

Let us print something on screen:

echo Hello!
Hello!

The command echo just prints its' argument to the standard output. Usually it is just the screen, but it can be also a file. For example:

echo Hello! > hello
ls -l
total 4
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:28 hello

To print a file, use cat:

cat hello
Hello!

cp, mv.

You can, of course, use cat to create a copy of a file:

cat hello > hello2
ls -lrt
total 8
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:36 hello2

A better way is to use a cp command though:

cp hello2 zzz
ls -lrt
total 12
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:36 hello2
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:37 zzz

Instead of copy, we can also move files with mv:

mv hello* ..
ls -l ..
total 16
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:38 test2
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt

mv is also what you use to rename files:

mv zzz kkk
ls -l 
total 4
-rw-rw-r-- 1 apj770 apj770 7 Sep 28 13:37 kkk

Pipes, more redirection

Instead of a file, you can redirect the standard output of one program into the standard input of another. Let us go back:

cd ..
ls -lrt
total 16
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:42 test2

Now, let us create a file containing a list of files in the current directory:

ls -lrt > list.txt
ls -lrt
total 20
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770  320 Sep 28 14:32 list.txt

Let us see the contents (notice any differences?)

cat list.txt
total 16
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 14:32 list.txt

To append the output to an existing file, instead of replacing, use ">>":

ls -lrt >> list.txt
cat list.txt
total 16
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 14:32 list.txt
total 20
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770  320 Sep 28 14:32 list.txt

If you want the output of one program to be processed by another, you can use a pipe, denoted "|". For example, a command tail prints last 10 lines of its' input:

cat list.txt | tail
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 14:32 list.txt
total 20
-rw-rw-r-- 1 apj770 apj770    0 Sep 28 13:18 zz.txt
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:28 hello
-rw-rw-r-- 1 apj770 apj770    7 Sep 28 13:36 hello2
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2
-rw-rw-r-- 1 apj770 apj770  320 Sep 28 14:32 list.txt

Deleting files

Use rm to delete files:

rm *
ls -lrt
total 8
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 13:18 test1
drwxrwxr-x 2 apj770 apj770 4096 Sep 28 14:31 test2

By default, it does not work on directories. You need to specify a parameter "-r" to delete them:

rm -r *
ls -lrt
total 0

Text editors

pico/nano: the simple text editor

  • nano on linux (skew4, SSCC), pico on UNIX (WRDS)
  • Fast, even under slow connections!
  • Very easy to use, but somewhat limited functionality
    • see the hint bar at the bottom of the screen
    • to exit: ^ X (i.e. Ctrl+x simultaneously)

vi/vim

  • Usually, the default on UNIX/linux
  • Fast and powerful, also exists for Windows/Mac
  • Somewhat counterintuitive interface
    • You know if you are in vi if you see a lot of ~'s on screen!
    • To exit without saving: press sequentially [ESC] : q ! [ENTER]
  • Exercise (advanced users only):
    1. start vi by typing "vi" at prompt
    2. exit vi as described above

emacs

  • Large, can be slow sometimes. Also exists for Win/Mac
  • If under X-Window, you can actually use the mouse!!!
  • Between pico and vi in user-friendliness
    • to exit: C-x C-c (i.e. first Ctrl+x then Ctrl+c)
    • open file: C-x C-f
    • cancel/"escape" (if something is weird): C-g
    • save file: C-x C-s
    • save file under different name: C-x C-w
  • Very useful for stats (ESS, works with R/SAS/Stata/etc.)
  • Also nice for writing papers (AucTeX)
  • Check out org-mode!
    • it was used to write this tutorial

Other commands

Other useful commands include:

  • less (or more): use it to read long files
    • e.g. "less myprogram.log" to read a SAS log file
    • [q] to exit, [SPACE] or [f] for page down, [b] for page up
    • /text [ENTER] : search for "text"
    • [>] : go to end of file (less only)
    • also works with pipes:
      • cat myprogram.log | less
      • ls | less
  • gzip: compress files
    • compress: gzip file.txt
      • creates file.txt.gz
    • decompress: gzip -d file.txt
    • decompress to standard output and view with less:
      • gzip -cd file.txt.gz | less
  • ssh: log into remote servers
    • ssh apj770@wrds.wharton.upenn.edu
  • sftp: transfer files
  • xterm: under X, create another window
    • xterm -sb -bg pink &
  • man: documentation
    • All built-in and many other commands have manual pages
      • interface same as in more
    • man ls
    • man gzip

© 2001-2010 Kellogg School of Management, Northwestern University