Module 0 - Getting Started

Written by: Trey Parker

Glossary of Basic Commands

Links to the full Linux manual pages are available for each basic command. These are formal documents written by the creators of each command that specify all functionality and options that are available for your use. In some of the manual pages there are sample programs that each command is used in.

echo - display text or variable to terminal

Syntax - echo [options] <text|variable>
$ echo "Hello world"
Hello world

ls - lists directory contents

Syntax - ls [options] <location>
$ ls # no location provided so assume current directory
file.txt document.pdf child_directory

pwd - print working directory

Syntax - pwd [options]
$ pwd
/home/user

cd - change the current working directory

Syntax - cd [options] <directory>
$ cd Downloads # directory can be absolute or relative
$ ls 
file.txt download.pdf untitled.xls

mkdir - make directory

Syntax - mkdir [options] <directory>
$ mkdir newDirectory # directory can be absolute or relative
$ ls
newDirectory file.txt old_document

rmdir - remove directory

Syntax - rmdir [options] <directory name>
$ rmdir newDirectory # directory can be absolute or relative
$ ls 
file.txt old_document

cat - used to join multiple files

Syntax - cat [options] <filename>
  • Where the <filename> can be repeated infinitely many times for concatenation
  • Colloquially only one file is specified just so the user can view its contents in the terminal
    $ cat myfile
    this is the text contained in the myfile
    

touch - creates a new blank file

Syntax - touch [option] <filename>
$ touch newdocument # file path can be absolute or relative
$ ls
newdocument file.txt old_document

rm - remove file

Syntax - rm [option] <filename>
$ rm old_document # file path can be absolute or relative
$ ls
newdocument file.txt

mv - move file or directory

Syntax - mv [option] <source> <destination>
$ ls 
directory1 file1 file2 file3
$ mv file1 directory1 
$ ls
directory1 file2 file3
$ cd directory1
$ ls 
file1

Input/Output Redirection

Tip

When writing a script in Linux it is often nice to change the behavior of the input, output, and error locations (the terminal is the default). For example, if you wanted to save time typing out input you could change the standard input (stdin) for your script from the terminal to a file.

You can also redirect output such that instead of getting the standard output (stdout) in your terminal you could redirect the output to a file. This might be preferred when writing a script to reduce the noise and text overload on the screen. With piping we can even make the output of one command an input for another command.

Resource

The examples below are adapted from this resource. If you would like to try out more complex types of redirection check out that article. Do not worry about how these commands specifically work as they will be explained later. For now, focus on understanding the redirections that are happening.

Redirection using >

command > file

  • send standard output to file
    $ echo "writinghelloworldtofile" > myfile.txt
    $ cat myfile.txt
    writinghelloworldtofile
    

command &> file

  • sends standard output and error output to file
    $ find /usr -name ls &> myfile.txt
    $ cat myfile.txt
    /usr/bin/ls # standard output
    find: '/usr/share/polkit-1/rules.d': Permission denied #standard error
    

Redirection using <

command < input

  • Feeds a command input from the input file. This acts as if the contents of input are entered by the user.
    $ sort < names.txt
    abigail
    nathan
    william
    

Piping

Piping is a mechanism that makes it possible to allow commands to be combined and executed. Specifically, one command's output is used as an input for a concurrent command. It is referred to as "piping" as it funnels the output from one command through a "pipe" to another command.

The piping operator is a vertical bar (|) to pipe one command to another, separating each command using this operator looks like:

$ command1 | command2 | command3
$ echo "joe joe bob bob jane" | wc -w | grep [0-9]

The second example sends the output of the echo command (the string "joe joe bob bob jane") to the wc command, which counts the number of words in the string. Then, the output of the wc command is sent to the grep command, which searches the input for numbers.