Some Linux commands

Some Linux commands

One of the steepest Unix/Linux learning curves is the sheer number of built-in commands, all of which have many options -- most of which you'll never use. Plus there are a number of advanced commands that are extremely powerful but also extremely complex.

To help address this, this page introduces a number of built-in Linux utilities along with some of their common options, by category.

Command line file arguments can be replaced by standard input

Most built-in Linux commands that obtain data from a file can also accept the data piped in on their standard input.

And here's a Linux commands cheat sheet you may find useful:

Basic commands

Displaying file contents

  • cat outputs all the contents of its input (one or more files and/or standard input) or the specified file

    • cat -n prefixes each line of output with its line number

    • CAUTION – only use on small files!

  • zcat <file.gz> like cat, but understands the gzip (.gz) format, and decompresses the data before writing it to standard output

    • CAUTION – only use on small files! ..or pipe the output to more

    • Another CAUTION – does not understand .zip or .bz2 compression formats

  • more and less pagers

    • both display their (possibly very long) input one Terminal "page" at a time

    • in more:

      • use spacebar to advance a page

      • use q or Ctrl-c to exit more

    • in less:

      • q – quit

      • left, right arrows - move back or forward one character

      • up, down arrows - move up or down one line

      • Ctrl-f or spacebar – page forward

      • Ctrl-b – page backward

      • /<pattern> – search for <pattern> in forward direction

        • n – next match

        • N – previous match

      • ?<pattern> – search for <pattern> in backward direction

        • n – previous match going back

        • N – next match going forward

    • use less -N to display line Numbers

    • use less -I to use case Insensitive pattern searches

    • less can be used directly on .gz format files

  • head and tail

    • show you the first or last 10 lines (by default) of their input

    • head -n 20 or just head -20 shows the first 20 lines

    • tail -n 2 or just tail -2 shows the last 2 lines

    • tail -n +100 or just tail +100 shows all lines starting at line 100

    • tail -n +100 | head -20 shows 20 lines starting at line 100

    • tail -f shows the last lines of a file, then follows the output as more lines are written (Ctrl-c to quit)

  • gzip <file> – compresses <file> contents creating a <file.gz> compressed file

    • and removes the original <file>

  • gunzip <file.gz> – un-compresses <file.gz> contents creating a <file> file

    • and removes the <file.gz> file

    • gunzip -c <file.gz> | more (or less)

      • like zcat, un-compresses lines of <file.gz> and outputs them to standard output

      • <file.gz> is not altered on disk

      • always pipe the output to a pager!

File system navigation

  • ls - list the contents of the specified directory

    • -l says produce a long listing (including file permissions, sizes, owner and group)

    • -a says show all files, even normally-hidden dot files whose names start with a period ( . )

    • -h says to show file sizes in human readable form (e.g. 12M instead of 12201749)

    • -t says to sort files on last modification time

    • -r says to reverse the current sort order

    • -d says to show directory listing information only, instead of directory contents

      • usually combined with -l, e.g.:  ls -ld <dirname>

  • cd <whereto> - change the current working directory to <whereto>. Some special <wheretos>:

    •  .. (period period) means "up one level"

    •  ~ (tilde) means "my home directory"

    • - (dash) means "the last directory I was in"

    • . (period) means "the current directory"

  • find <in_directory> [ operators ] -name <expression> [  tests ]

    • looks for files matching <expression> in <in_directory> and its sub-directories

    • <expression> can be a double-quoted string including pathname wildcards (e.g. "[a-g]*.txt")

    • there are tons of operators and tests:

      • -type f (file) and -type d (directory) are useful tests

      • -maxdepth NN is a useful operator to limit the depth of recursion

      • -iname ignores case when matching

  • file <file> tells you what kind of file <file> is

  • df shows you the top level directory structure of the system you're working on, along with how much disk space is available

    • -h says to show sizes in human readable form (e.g. 12G instead of 12318201749)

  • pwd - display the present working directory

    • -P says to display the full absolute path

  • tree <directory> -  shows the file system hierarchy of the specified directory

    • tree is not available on all Linux systems

Create, rename, link to, delete files