Command-line interface on Linux/UNIX

From csml-wiki.northwestern.edu
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This is not a UNIX guide, but just a loose list of tips, hints, etc., that may come in handy when working in a command-line environment.

Bash

  • To use the output of one command as the input for the next command, use back quotes ("inverted commas") around the first command. This is called command substitution.

Useful (and sometimes underappreciated) command-line tools

  • To combine files side-by-side into a single file use
    paste file1 file2 ... > output
    

    If file1, file2, ... contain the same number of lines, output will contain their contents as columns.

  • Splitting files. To split a file into parts that each contain N lines (except possibly for the last chunk) and that will be labeled "out00", "out01", etc., use
    split -d -l N file out
    

    For more than 100 chunks, use the -a option (see man split).

    To instead split a file into a fixed number (M) of chunks, use

    split -d -n M file out
    
  • You may have many entries in your PATH variable, or there may be multiple copies of an executable on a file system. To determine which version of program will be executed, use
    which program
    
  • Comparing text files. To find out if two files are identical except for differences in white space, use
    diff -w file1 file2
    

    To compare two text files in which lines may have been rewrapped, use wdiff ('word diff')

    wdiff -3 file1.tex file2.tex
    
  • To find out which libraries are needed/used by a dynamically linked executable, use
    ldd /path/program
    

    Note that ldd will not use PATH to search for program; just must specify it via an explicit path. However, you can take advantage of which as follows

    ldd `which program`
    

    Note the back quotes. Side note: On OS X, if you have xcode installed the ldd command can be mimicked via otool -L