WatkissOnline.co.uk

Home About E-Books Web Design Unix / Linux Information First Aid Shop Blog

 

« Back to Unix / Linux

Linux Commands

Here's a list of Unix Commands. These are a select few which are useful for users and adminstrators alike. Unless otherwise stated these are based on Linux but should work equally well with most flavours of Unix.


The Basics

  • chdir / cd Changes to a specified directory. This can be either relative to the current directory or a full directory specified from the root directory (/).
    Without any parameter this will change to your default (home) directory.
    "cd .. " will move up one level in the path.
    "cd / " will move to the root (top most) directory.
  • ls Lists the contents of the current directory. A parameter could specify either a path or a mask. Eg.
    "ls /" will list the contents of the root directory "ls *.gif" will list all files ending with ".gif" in the current directory.
    Other parameters can be added such "-l" for a long listing including file sizes and owners;
    "-c" to display in colour;
    "-i" to display the inode;
    "-a" to display all files including hidden files.
  • pwd Displays current working directory
  • mkdir Makes a directory with the specified name. Directory can be specified as a single subdirectory relative to the current or with the full path name. When using a full path name this can only create a single directory so all preceding subdirectories must already exist.
    "-p" will create any missing parent directories.
  • cat This echos the contents of a file to the screen. Eg. "cat something.txt" will display the contents of something.txt onto the screen. Scrolling where neccessary.
  • more Displays text one page at a time. This can be used on it's own to display the contents of a file or using the pipe (see below) to take the output of another command and display it one screen full at a time. Using Space pages through to the next page Using CR will move down a line at a time.
  • less Like more with additional functionality. This allows scrolling up or down a file and allows for search commands to be performed.
  • man Display's manual pages. This is extremely useful for providing further details about a command. Typing man followed by a command will provide help on that command (this works for all standard unix commands and some additional commands). The listing also provides useful key combinations or command line parameters. If you enter man <pagenum> <command> then you can select different pages. The following categories are used:
    1. User Programs
    2. System Calls
    3. Library Functions
    4. Special Files
    5. File Formats
    6. Games
    7. Miscellaneous
  • Pipe '|' The vertical line is called a pipe. It takes the output from the command on the left and "pipes" it through the command on the right. eg.
    "cat | more " will take the output from the cat command (shows contents of a file) and pipes it through the more command (displays multiple screens of text 1 screen at a time). This is a useful combination used for listing the contents of a file 1 page at a time. On UK Keyboards this is normally entered using SHIFT \
  • Redirect > , < The greater than / less than indicators are used for redirecting the standard input / outputs. > redirects the standard output (what is seen on the screen), which can then be put somewhere else (normally a file). < redirects the standard input (keyboard), this is used to automate a process by using a file containing the keystrokes normally typed.
    "command > file.out" Redirects output from command to file.out
    "ftp Executes ftp using ftpcmds.txt as it's stdin (instead of keyboard strokes).
  • Stderr 2> , 2>&1 There is another standard output known as standard error channel. This is often directed at the screen along with the standard output. This can be redirected seperately using 2>. To redirect all messages from the screen (both messages and errors then the command is 2>&1 after redirecting stdout
    "command > file.out 2>&1;"
  • Append >> Redirecting as shown above will overwrite any existing file (unless the noclobber is set see later). By using >> the redirect will append to the file instead of overwriting it. This could be useful for keeping a log e.g.
    "command >>outlog"
  • |tee Tee allows for the stdout to be sent to both the screen and to a file
    "command |tee outfile"
    The output from tee could itself be redirected if wished.
    "command |tee outfile | grep error | tee errorfile | pg" Which will save all output to outfile, however only lines containing the word error would be stored in errorfile and paged on the screen.
    Using option -a would append instead of overwriting the file.

A little more advanced

  • tar Used for combining files into a single file. This was originally designed to copy files to a tape (tape archive), but works equally as well sending the output to a file on disk. Typical usage includes
    tar -xvf filename.tar to extract the contents of the file
    tar -cvf filename.tar * to create an archive containing all files in the current directory and below.
    on most recent versions (typically provided for Linux distributions rather than the proprietary operating systems) then the z option can be used to compress the files using the gzip protocol.
  • gzip / gunzip These two commands are used to compress and uncompress files respectively. The file is replaced by one with the same filename, but with .gz appended to the end. Note that this command does not combine multiple files into a single file - the tar command is needed to provide that functionality.

Useful command combinations

  • ls >temp.txt Runs the command ls rerouting the output into a temporary file temp.txt this will list the conntents of the current directory into a file temp.txt which will be put in the current directory.

Administrator type commands

  • chown Changes the owner and or group associated with a file. The owner / group are seperated by a period. e.g.
    chown username.groupname filename
  • chsh Changes the default shell. To change the shell for a user hangman to only be able to run the hangman game then
    chsh -s /usr/games/bin/hangman hangman
    chsh -l will list the available shells.
    To check the current shells cat /etc/passwd
    which will display the user setup.

 

Shell Settings


Unless specified otherwise these are for the Korn shell. These will probably also work with the bash shell although some have alternative implementations.

Most of the settings could be put in .profile (in users home directory) to set them automatically when logging on.

  • set -o vi Enables command recall. This uses the vi keys. eg. First press ESC followed by
    k Recall last command
    j Recall more recent command
    h left
    l right
    i Insert before cursor
    a Insert after cursor
    x Delete at cursor
  • set -o noclobber Prevents redirects from overwriting files.
  • $DISPLAY Sets which screen the output should be redirected to. Normally this is set to hostname:0.0 To redirect this to another machine depends upon the shell being used.
    For C shell setenv DISPLAY newhostname:0
    For Bourne / Korne shells
    DISPLAY=newhostname:0
    export DISPLAY
  • xhost This will allow / disallow certain hosts to use the display (see above). To allow a machine use +machinename to disallow a machine use -machinename to disallow authority. Used with machinename blank will allow / disallow any host.

 

 

Please Visit

Watkissonline.co.uk Online Baby Shop (UK)

First Aid Quiz Online Shop (UK)

PenguinTutor Online Shop (US)