Top

A Linux User Reference

Search tips
  • search ignores words that are less than 4 characters in length
  • searches are case insensitve
  • if a search does not return anything try it in Boolean mode then Query expansion mode by checking the appropriate radio button e.g. searching for 'cron' in just the Administration category returns nothing - presumably because the 50% threshold is reached. Boolean mode ignores this threshold so a search for 'cron' returns several hits
  • in Boolean mode preceding a word with a '+' means the result must include that word, a '-' means it must not
  • in Boolean mode '+crontab -anacron' means match articles about crontab that DO NOT mention anacron
  • to match a phrase e.g. 'manage system' check the Boolean mode radio button and enclose the phrase in quotes "some phrase ..."
  • in Query expansion mode the search context is expanded beyond the keywords you entered - relevancy of hits may well be degraded

THE SHELL

Running commands

  • Output arguments
    echo
    • Bash builtin command.
    • Outputs arguments passed, separated by spaces, followed by a newline.
    • The return status is always 0.
    echo [-neE] [arg ...]
    
    Options:
     -n             Do not print trailing newline
     -e             Interpretation of the following backslash-escaped 
                    characters is enabled.
     -E             Disables the interpretation of the following backslash
                    escaped characters.
                      \a     Alert (bell)
                      \b     Backspace
                      \c     Suppress trailing newline
                      \e     An escape character
                      \f     Form feed
                      \n     New line
                      \r     Carriage return
                      \t     Horizontal tab
                      \v     Vertical tab
                      \\     Backslash
                      \0nnn  The eight-bit character whose value is the octal 
                             value nnn (zero to three octal digits)
                      \xHH   The eight-bit character whose value is the 
                             hexadecimal value HH (one or two hex digits)
    

    Examples

    $ echo "fred"
    fred
    
    $ echo -n "fred"
    fred$
    
    $ echo -e "fred\n\tbert\n\t\tsue"
    fred
        bert
            sue
    
  • Run a program in a modified environment
    /usr/bin/env

    Command usage

    env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
    
    Set each NAME to VALUE in the environment and run COMMAND.  
    If no COMMAND, print the resulting environment.
    
    Options:
     -i | --ignore-environment     Start with an empty environment
     -u | --unset=NAME             Remove variable from the environment
     --help
     --version
     -                             Implies -i.  
    

    Print current environment

    $ env
    SSH_AGENT_PID=2532
    GPG_AGENT_INFO=/run/user/mark/keyring-iUWnWs/gpg:0:1
    .....
    _=/usr/bin/env
    
    $ env bert="hello there" 
    SSH_AGENT_PID=2532
    GPG_AGENT_INFO=/run/user/mark/keyring-iUWnWs/gpg:0:1
    .....
    _=/usr/bin/env
    bert=hello there                                (bert is set)
    

    A little env demo script - env-demo.sh

    #!/bin/bash
    # Script name: env_demo.sh
    
    echo "Variable bert = "$bert
    echo "Displaying environment variables:"
    env
    

    Run the script in current environment

    $ ls -al env-demo.sh
    -rw-r--r-- 1 mark mark 114 Dec 13 17:34 env-demo.sh
    
    $ . ./env-demo.sh
    Variable bert =                                 (bert is not set)
    Displaying environment variables:
    SSH_AGENT_PID=2532
    .....
    _=/usr/bin/env
                                                    (or)
    $ source ./env-demo.sh
    Variable bert = 
    Displaying environment variables:
    SSH_AGENT_PID=2532
    .....
    _=/usr/bin/env
    

    If you source a file, '. <file>' it does not have to be 'executable' to have the commands inside it executed.

    Set variable bert and run the script in a new environment

    $ chmod +x env-demo.sh
    $ env -i bert="hello there" ./env-demo.sh
    Variable bert = hello there                      (bert is set in the new environment)
    Displaying environment variables:
    bert=hello there
    PWD=/home/mark
    SHLVL=1
    _=/usr/bin/env
    

    Unable to source the script when it is used as an arguement to another command. Need to make it executable this time.

    • When a command is not in your path use the full pathname to the command.
    • If it is in the path then just the command name will suffice.
    $ ./find_drivers.sh                      (when in your path)
    $ /usr/not-local/bin/find_drivers.sh     (when not)
    
  • Shell commands
    chaining and looping

    Chain one or more commands together by separating them with a ';'

    $ ls *.txt ; pwd
    joblog.txt  perm-change.txt
    /home/mark
    

    Repeat a command using a loop

    $ let X=0
    $ for wd in `ls *.txt`
    > do
    > let X=X+1
    > echo "("$X") "$wd
    > done
    (1) sec-firewall.txt
    (2) sec-ftp.txt
    (3) sec-gpg.txt
    (4) sec-hardtools.txt
    .....
    

    Chaining and looping

    $ let X=0; for wd in `ls sec-s*.txt`; do let X=X+1; echo "("$X") "$wd; done
    (1) sec-ssh.txt
    (2) sec-ssl-pam.txt
    (3) sec-sysmon.txt
    
  • Command substitution

    Substitute the results of one command for another

    $ var1=$(which bash)                 (Store result of 'which' cmd in var1)
    $ echo $var1
    /bin/bash
    
    $ echo `which bash`                  (Print the result of the 'which' cmd - pointless!)
    /bin/bash
    
    $ echo "1 2 3 4" > file1             (Write to file)
    
    $ echo $(< file1)                    (Read from file)
    1 2 3 4
    
  • Apply commands recursively
    • Some commands have recursive switches, usually '-R' or '-r'.
    • Use a switch if command supports or a recursive command.

    Recurse through current directory and every sub-directory changing ownership of php files

    $ chown -R mark:mark *.php
                                                      (or)
    $ find . -name *.php -exec chown mark:mark {} \;
    
  • Streams, pipes and redirects

    Redirect command's stdout stream to file

    $ echo "fred" > bert
    $ cat bert
    fred
    $ echo "bert" 1> bert ; cat bert
    bert
    

    Append command's stdout stream to file

    $ command >> file
                                        (or)
    $ echo "fred" 1>> bert ; cat bert
    bert
    fred
    

    Redirect command's stderr stream to file

    $ cp not-here.txt not-there.txt 2> bert
    $ more bert
    cp: cannot stat `not-here.txt': No such file or directory
    

    Append both command's stdout and stderr streams to file

    $ chown mark: no-perms.txt >> err.txt 2>&1
    $ more err.txt
    chown: changing ownership of `no-perms.txt': Operation not permitted
    

    File no-perms.txt is owned by root.

    Redirect command's stdout stream to file1, stderr stream to file2

    $ chown -v mark: *.txt > success.txt 2> err.txt
    
    $ more success.txt
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    
    $ more err.txt
    chown: changing ownership of `no-perms.txt': Operation not permitted
    

    Command receives stdin from file

    $ mysql -u root -p < some.sql
    

    Pipe a commands stdout to a commands 's stdin

    $  echo -e "fred\nbert\nsue\nfred\nangela" | sort -u
    angela
    bert
    fred
    sue
    

    Pipe a command's stdout and stderr to be used by a command's as stdin

    $ chown -v mark: *.txt 2>&1 | cat 
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    
  • Copy stdin to stdout and to one or more files
    /usr/bin/tee

    Command usage.

    tee [options] file ...
    
    Common options:
     -a | --append                        Append to the given file(s), do not overwrite
     -i | --ignore-interrupts             Ignore interrupt signals
    

    Redirect stderr to stdout for 'chown', copying stdout to file1, file2

    $ chown -v mark: *.txt 2>&1 | tee file1 file2
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    
    $ more file[12]
    ::::::::::::::
    file1
    ::::::::::::::
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    ::::::::::::::
    file2
    ::::::::::::::
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    

    Append chown's output to file1 and stdout

    $ chown -v mark: *.txt 2>&1 | tee -a -i file2
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    
    $ more file2
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    chown: changing ownership of `no-perms.txt': Operation not permitted
    failed to change ownership of `no-perms.txt' from root:root to mark:mark
    ownership of `perm-change.txt' retained as mark:mark
    ownership of `success.txt' retained as mark:mark
    
  • Supply arguments to a command via stdin
    /usr/bin/xargs
    • Provides a means of supplying arguments to a command via stdin.
    • Typically used when arguments are very long and or need repeated execution. See man pages.
    xargs [options] [file] ...
    
    Common options:
     -n <int>             Use <int> argument(s) from stdin per cmd
    

    Find all files named file3 or file4 that contain 'linux' in them

    $ find . -name 'file[34]' | xargs -n 1 grep -H linux
    ./file4:linux
    ./file3:linux
    

    'grep -H' - display the file name. 'find' could do this on its own, so could 'grep' ... not a very practical example.

    Pass 3 arguments at a time from 'ls' output to stdin of each invocation of echo

    $ ls file* | xargs -n 3 echo
    file1 file2 file3
    file3_aa file3_ab file3_ac
    file4
    
  • Shell command history
    history
    • Bash builtin command.
    • The shell maintains a record of commands entered, the number of commands stored is controlled by the HISTSIZE variable.
    • With no options, display the command history list with line numbers. Lines listed with a '*' have been modified.
    history [n]
    history -c
    history -d offset
    history -anrw [filename]
    history -p arg [arg ...]
    history -s arg [arg ...]
    
    Options:
      n                Lists only the last n lines.
     -c                Clear the history list by deleting all the entries.
     -d offset         Delete the history entry at position offset.
     -a                Append the 'new' history lines (since start of the current
                       bash session) to the history file.
     -n                Read the history lines not already read from the history 
                       file into the current history list.
     -r                Read the contents of the history file and use them as the
                       current history.
     -w                Write the current history to the history file, overwriting
                       the history file's contents.
     -p                Perform history substitution on args, display result on stdout.
     -s                Store args in the history list.
    

    Using history

    $ history
       28  ls
       29  cd ..
       30  ls
       31  mv tplt.css ./css/site.css
       32  ls
       33  rm *.gif *.JPG
       .....
    
    $ echo $HISTSIZE
    500
    
    $ history | head -3          (Show the three LEAST recent commands in history)
       29  cd ..
       30  ls
       31  mv tplt.css ./css/site.css
    
    $ history 3                  (Show the three MOST recent commands in history)
      527  echo $HISTSIZE
      528  history | head -3
      529  history 3
    
    $ history -d 507             (Deletes command at line 507 from history)
    $ !!                         (Run last most recent command - rerun previous command)
    $ !445                       (Run command at line 445 in the history file)
    $ !ifc                       (Run most recent command in history that starts with 'ifc')
    $ !?ifc                      (Run the most recent command in history that contains 'ifc')
    $ ^str1^str2                 (Repeat last command replacing first occurrence of str1 with str2)
    

    Command history editing - key bindings

    Ctrl & p             Display previous line
    Ctrl & n             Display next line
    Ctrl & b             Move back - to the left - one character
    Ctrl & f             Move forwards - to the right - one character
    Ctrl & a             Go to beginning of line
    Ctrl & e             Go to end of line
    Ctrl & l             Clear screen leaving current line at top
    Ctrl & d             Delete a character
    Ctrl & k             Delete text from cursor to end of line
    Ctrl & y             Paste text previously deleted with Ctrl & k or Alt & d
    Ctrl & rtext         Reverse search for 'text'
    Ctrl & stext         Forward search for 'text'
    Alt & d              Delete word
    Alt & <              Display/go to top of history
    Alt & >              Display/go to bottom of history