Skip to content

Linux Shell Usage (101)

Basic Commands

  • ls - list files
  • cd - change directory
  • pwd - get working directory
  • pushd - saves last location
  • popd - pops back to saved spot
  • . - "here" or current working directory
  • .. - up one level from current directory
  • ~ - current user's home directory
  • - - last working directory

Reading Files

  • cat - concatenate and print
  • less - display output one screen at a time
  • head - first lines of a file
  • tail - end of a file often combined with -f (live follow)
  • cat - concatenate and print (display) the content of files
    • example: cat /etc/passwd

Changing Files

  • mkdir - make new directory
  • echo - print to standard out
  • > - redirect and overwrite
  • >> - redirect and append
  • touch - create or change time
  • cp - copy files
  • mv - move or rename
  • rm - remove file

Searching

  • find - find /path -option “searchstring”
  • grep - prints lines that match a given pattern
  • grep -e - matches an extended expression (egrep)
  • zgrep - search compressed files

Find

Find syntax:

  • find <PATH> -name “<STRING>”
  • find /etc/elasticsearch -name "*.conf"
  • find /etc -name jvm* -maxdepth 2

Grep

  • Options:

    • -i ignore case
    • -v invert search
    • -c count results
    • -o only chars match
    • -r recursive
    • -E extended regex (same as egrep)

    • Example 1: grep -i 'root' /etc/passwd

    • Example 2: pipe output of other commands to grep
      • find / -name *.txt | grep apache

Sorting & Filtering

  • pipe ( | ) - passes output from one command to another

    • echo “echo will print this message”
    • echo “echo will print this message” | wc
      • output: line word characters
    • cat file | grep search
  • tee - send the output of a command to a file and the screen? tee

    • ls | tee lsout.txt
    • ls | tee -a lsout.txt # appends
    • find /etc | sort | tee etcsort.txt | wc -l
    • find /etc 2> etcerr.txt | sort | tee etcsort.txt | wc -l
  • wc - print byte, word, and line counts

  • cut - divide a file into several parts
  • uniq - uniquify files (dedupe)
  • sort - sort text files