The University of Western Australia
Computer Science and Software Engineering
 
 

Department of Computer Science and Software Engineering

CITS4407 Open Source Tools and Scripting

Frequently used Unix commands

This page presents a list of the most frequently used commands available from the Unix command interpreter (the shell). It is not the intention of this page to document all common commands, simply to list them and provide one or two common examples. All of these commands (their names) are common to both Linux-based and macOS systems, and the default execution of each command is identical on both platforms. However, when certain switches (or options) are provided to some of the commands, their execution can be slightly different on different platforms.

Although their are many websites delivering Unix manuals, it is very important to read the documentation that applies to the platform you are using. Until you are familiar with the documentation, read the manual from the command-line on the platform you're using.

Documentation

man - find and read online documentation
Perhaps the most important command to remember is man, which provides access to the online documentation of all other commands. The online manuals are divided into (at least) 10 sections and are kept on the local disk for fast access (and are not dependent on the network working). The sections are:

  1. standard Unix utility programs
  2. the Unix operating system calls
  3. standard Unix library routines
  4. special Unix files and hardware support
  5. significant file and kernel structure definitions
  6. the standard Unix games
  7. miscellaneous manual entries
  8. Unix system administration
  1. local enhancements to the system
  1. any networking details

If no section number is provided to the man command, then the manuals are locating by searching in the order: 1, n, l, 6, 8, 2, 3, 4, 5 and 7.

    man date                 # display the online manual for the 'date' command
                             # 'space' to page forward, 'b' backwards, 'q' quit
    man -k vim               # summarize all manuals involving the keyword 'vim'
    man 2 time               # display the online manual for the 'time' command from section 2 of the manual
    man man                  # !

The filesystem

pwd - report the present working directory
    pwd                      # print the present working directory

ls - list the contents of a directory
    ls                       # print a list of files in current directory
    ls -a                    # print a list of all files (including 'dot' files)
    ls -l                    # print a long list of files with more attributes
    ls -lt                   # print a long list of files sorted by modification time
    ls -lR                   # print a long list of files recursively

cd - change the present working directory
    cd                       # change current working directory to your home directory
    cd dirname               # change to the named directory
    cd ..                    # change to the parent directory of the current directory

mkdir - make a new directory
    mkdir dirname            # make a new directory with the given name

rmdir - remove an existing directory
    rmdir dirname            # remove the named directory (which must be empty)

Manipulating whole files

cp - copy an existing file to create a new file, or into an existing directory
    cp oldfile newfile       # copy an existing file to create or overwrite another
    cp oldfile dirname       # copy an existing file to an existing directory
    cp oldfile .             # copy an existing file to current directory
    cp -R olddir newdir      # recursively copy files from an existing directory to another
    cp -p oldfile newfile    # copy an file and preserve its permissions and modification time

mv - move an existing file, either to change change its name, or to move it into an existing directory
    mv oldfile newfile       # 'move' an existing file to a have a new name
    mv oldfile dirname       # 'move' an existing file to a new directory
    mv oldfile .             # 'move' an existing file to a current directory

rm - remove existing files or directories
    rm oldfile               # remove an existing file
    rm -r dirname            # remove an existing directory and its contents

du - report the disk usage of the current directory
    du                       # report the disk usage of the current directory
    du dirname               # report the disk usage of the named directory
    du -s dirname            # summarize the disk usage of the named directory
    du -sh dirname           # report disk usage in a 'human-readable' format

Displaying files

cat - display the whole contents of files
    cat filename             # display full file's contents to the screen
    cat -s filename          # display file's contents, suppressing multiple blank lines
    cat -v filename          # display file's contents, including non-printing characters

head - display the first few lines of a text file
    head filename            # display first 10 lines of a text file
    head -3 filename         # display first 3 lines of a text file
    head -c 100 filename     # display first 100 characters of a text file

tail - display the last few lines of a text file
    tail filename            # display last 10 lines of a text file
    tail -3 filename         # display last 3 lines of a text file
    tail -c 100 filename     # display last 100 characters of a text file

less - display the contents of a text file, one screen at a time
    less filename            # display a text file one page/screen at a time
                             # 'space' to page forward, 'b' backwards, 'q' quit

Examining files

wc - 'count' lexical items in a text file
    wc filename              # report the number of characters, words, and lines in a text file
    wc -l filename           # report just the number of lines

grep - find a pattern in text files
    grep pattern filename    # search for a pattern in a text file
    grep -c pattern filename # report the count of pattern matches
    grep -i pattern filename # ignore alphabetic case when matching

vim - edit text files using a powerful character-based editor
    vim filename             # edit a text file using vim
    vim +30 filename         # commence editing from line 30
    vim +/pattern filename   # commence editing at the location of the pattern

chmod - change the mode (permissions) of files and directories
    chmod -w filename        # turn off (disable) ability to write to a file
    chmod +x filename        # make a file executable

Manage processes

ps - list operating system processes
    ps                       # report your processes
    ps au                    # report your processes and their attributes
    ps aux                   # report all processes and their attributes

kill - terminate a running process
    kill processID           # terminate the process with the given numeric process-ID
    kill -9 processID        # terminate a process that refuses to terminate

This Page

Written by: [email protected]