Linux newbie shell primer

Discussion in 'all things UNIX' started by Gullible Jones, Nov 10, 2014.

  1. Gullible Jones

    Gullible Jones Registered Member

    May 16, 2013
    This is a primer for Linux novices who want to get started with the command shell, but don't know anything about UNIX shells. It is not a complete guide. I'm just giving you enough to get started - the sort of stuff that would have helped me in the beginning.

    Here goes...

    Non-obvious keybindings
    Ctrl-b (back) moves left, Ctrl-f (forward) moves right.
    Ctrl-left and ctrl-right move left and right in one-word hops. Likewise Alt-b and Alt-f. You should usually use these instead of the arrows alone, they make a huge difference.

    Up moves backward in the history of commands, down moves forward. Likewise Ctrl-p (previous) and Ctrl-n (next).
    Ctrl-a moves to the beginning of the line. Ctrl-e moves to the end.
    Ctrl-k deletes everything between the cursor's current position and the end of the line.

    Ctrl-d deletes the character under the cursor, OR exits the prompt if done on an empty line.
    Ctrl-l gives you a new terminal page. You'll still be able to see the old stuff with the Page-up and Page-down keys.

    Ctrl-r searches for the last occurrence of a string in the command history. Remember this, it is extremely useful; it lets you search through yesterday's commands with just a few keystrokes.
    Tab attempts to autocomplete a command. Remember this too, it will save you a huge amount of time.

    Ctrl-c terminates the currently running program.
    Ctrl-z suspends the currently running program (more on this later).

    Selecting text auto-copies it to the clipboard. Middle click (or Shift-Insert) will paste it. Fancy terminals like gnome-terminal and konsole also accept Ctrl-Shift-c and Ctrl-Shift-v.

    Non-obvious commands
    First of all, '|' is important.
    cmd_one | cmd_two
    uses the output of the command on the left as the input for the one on the right.

    'cd' changes the current working directory, like on Windows. 'cd -' will move to the previous working directory.
    'ls' lists the contents of a directory. It has lots of options.
    'man' reads the manual page of a command. e.g. 'man ls'. Configuration files also usually have man pages.
    'help' shows a help page about the built-in shell commands, the ones that do not have their own binaries.
    'apropos' shows summaries of what different commands do.
    'grep' filters text based on strings, wildcards, or regular expressions. It's very useful, but difficult to use. If you use it, you should read the manual page first.
    'nano' is a text editor that is present on most Linux distros. You probably want 'nano -w' to turn off the problematic word wrapping functionality.
    'stat' shows you detailed information about a file's permissions, modification time, size, etc.

    Most commands can be invoked with '--help' as an option to get a help page.

    With pipelines you can string commands together. e.g.
    apropos directory | less
    will get you a scrollable list of all the commands that do stuff with directories, and summaries of what they're for.

    > redirects output of a command to a file. This is destructive. It will overwrite existing files.
    >> appends the output of a command to the file, instead of overwriting it.
    < redirects input from a file. I barely ever use this.

    The filesystem
    Almost everything is a file. Hardware devices, storage volumes, and various types of system and process information are all represented as files. If you don't know what a file does, don't mess with it (or better yet see if it has a man page).

    Files have an owner and a group, and permissions for owner, group, and others. The permissions are handled as an octal bit mask - I won't explain that fully here, but the upshot is that
    4 = read
    2 = write
    1 = execute (or traverse for directories)
    And the sum of whichever combination of numbers in each "slot" shows what permissions a file has for owner, group, and everyone else respectively. e.g. a typical file in /usr/bin might be owned by root:root with permissions 0755 ('-rwxr-xr-x.' if you look at 'ls -l'). This means read/write/execute permissions for root user, read and execute for root group, and read and execute for everyone else. Note that the '7' is just to be obvious, because root is omnipotent and can bypass read-only permissions on files.

    What's the '0' in the first slot? That's for special permissions:
    4 = setuid (always run as the owner)
    2 = setgid (always run with the rights of the group)
    1 = sticky (no longer used on binaries)

    or, on directories
    4 = always create contents as owned by this user
    2 = always create contents as owned by this group
    1 = only allow files to be deleted by the user that created them (important for world-writeable areas like /tmp)

    If you need to apply special permissions to things, you'll know it. If you don't, you shouldn't use them.

    Some final advice
    When you use nano, hit Ctrl-g and take a look at the help page. This editor is more powerful than it looks.
  2. sthmptn

    sthmptn Registered Member

    Jul 20, 2009
    Good stuff, GJ, I haven't used some of those.

    I know it's not an exhaustive list but one there I've used just before reading this is &&, which you can put between two commands and, if the first completes, it will continue onto the second.

    Example would be: # apt-get update && apt-get upgrade
  3. DedicateNier

    DedicateNier Registered Member

    Mar 7, 2014