Linux Workshop

Helpful Command Line Utilities

July 27, 2005

Quick Special Symbols Primer

  • ~ - expands to your home directory (Example: cd ~)
  • . - expands to the current directory (Example: ls .)
  • .. - expands to the parent directory (Example: ls ..)

Getting Help

  • help - bash command-line/scripting help
  • apropos - use this if you're looking for a command that does <insert task here> (Example: apropos edit)
  • man - looks up help files for various commands (Example: man grep)
  • info - opens an emacs-like interface for extensive guides to commands (Example: info make)
  • whatis - shows a quick overview of the command (Example: whatis ls)

Nifty Bash Commands

  • pushd and popd - quick directory traversal
    • Example:
      cd ~
      pushd /usr/share/doc
    • You can also cd - to change to the directory you were just in.
  • alias - command aliasing
    • Example:
      alias -p
      alias changecd="eject; sleep 5; eject -t"
  • control-r - reverse search
  • ~/.bashrc can be used to automatically add aliases

Nifty File Manipulation Tools

  • grep - string searching in files
    • lots of different options (Use grep --help or man grep to see them all)
    • ways of calling grep: grep, egrep, rgrep
    • Examples:
      • grep -v apple fruits.txt
      • egrep '.*berry.*' fruits.txt
      • rgrep a * (recursive search)
  • wc - count the number of letters, words, and paragraphs in a file
  • cat - print out a file's contents
  • sort - sort a file alphabetically or numerically (Example: sort -r vegetables.txt)
  • tail - print the last few lines of a file
  • head - print the first few lines of a file
  • uniq - remove (sequential) duplicate lines in a file (Example: uniq names.txt)
    • You can use sort and uniq together to quickly tidy up files.
    • Example: sort names.txt | uniq (see Piping below)
  • sed - regular expression/scripting tool
    • Regular expressions are a way to flexibly match/replace text.
    • Example: sed -r -e 's/tomato/squash/' vegetables.txt (replaces all occurrances of "tomato" with "squash")
  • Handy console text editors:
    • vim
    • emacs
    • nano

Nifty File Searching Tools

  • ls - list directory contents
  • find - a tool for recursive directory searching
    • Many options, check out man find or find --help to see them all.
    • Example: find . -iname '*.log' (will list all files/directories contained in the current directory (".") that match the pattern "*.log" (case-insensitive))
  • locate - a simple tool for finding files system-wide
    • Note: you may need to run updatedb (as root) first before you use locate (especially if you're searching for a new file), but many Linux systems automatically run this command on a daily basis.
    • Example: locate apache
  • whereis - shows the full directory path to a command you're looking for (Example: whereis bash)

Nifty Commands Related to Processes and Running Applications

  • ps - view running processes
    • Run with no options, ps shows the user's processes.
    • To show all processes, type something like this: ps -ax
  • pstree - view running processes in a tree format
  • killall - kill errant processes (use with caution!) (Example: killall kwrite)
  • nice - run a process at a different priority level
    • Use nice if you want a costly program to use up less of your system resources.
    • You can also use nice to boost the priority of a process (see man nice)
    • Example: nice find / -name '*'
  • lsof - show which files processes are using (helpful for umounting)

Nifty Disk Usage Utilities

  • df - show disk usage on all mounted drives (Example: df -h)
  • du - show disk usage in a specified directory (Example: du . -sh)
  • mount - mount a device to a directory (see man mount for complete instructions)
    • Can mount devices such as the CD-ROM drive, USB memory stick, or iPod to a specified directory.
    • Type mount with no arguments to see what devices are mounted.
    • Run umount to unmount a drive.

Nifty Printing Utilities

  • lpstat - show information about printers (Example: lpstat -p)
  • lpr - print files from the command line (Example: lpr -P laser1 fruits.txt)

Other Nifty Commands

  • lynx - console web browser
  • bc - math interpreter (Example: echo 5*6/3+23 | bc)
  • cal - calendar (Example: cal -3)

Commands Just For Fun

  • fortune - random quotes and witty sayings (install by running apt-get install fortune as root)
    • Example quotes from fortune:
      • Abandon the search for Truth; settle for a good fantasy.
      • Celebrate Hannibal Day this year. Take an elephant to lunch.
    • Install the "fortunes" package for extra fortunes (run apt-get install fortunes as root)
      • Examples:
        • fortune startrek - "Live long and prosper." -- Spock, "Amok Time", stardate 3372.7
        • fortune linux - "Why use Windows when you can have air conditioning? Why use Windows, when you can leave through the door?" -- Konrad Blum
  • scrabble - a text-based scrabble game (installation: apt-get install scrabble)
  • adventure - a text-based roleplaying game (installation: apt-get install bsdgames)

Piping and Redirecting Output

  • Many commands that run in the console can redirect their output to a file or another program using special characters.
  • Redirecting output
    • >, >>, and &>
      • > will truncate any file that you specify before outputting.
      • >> will append output to a file.
      • &> will truncate and send both normal and error messages to a file.
    • Make sure you don't use > when you mean >>!
    • Examples:
      • sort names.txt > sorted-names.txt
      • cat vegetables.txt >> fruits.txt
      • grep &> grep-error.txt
  • Piping
    • Use the | symbol to pipe output.
    • Examples:
      • sort names.txt | uniq
      • sort vegetables.txt | egrep '^c' | wc

Other resources

Topic attachments
I Attachment Action Size Date Who Comment
fruits.txttxt fruits.txt manage 0.1 K 25 Jul 2005 - 14:16 JeremyStephens List of fruits
names.txttxt names.txt manage 0.6 K 25 Jul 2005 - 14:36 JeremyStephens List of common names
vegetables.txttxt vegetables.txt manage 0.1 K 25 Jul 2005 - 14:30 JeremyStephens List of vegetables
Topic revision: r4 - 27 Jul 2005, JeremyStephens

This site is powered by FoswikiCopyright © 2013-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback