WebJunk Hosting and Site Design

Webjunk Introduction to Linux command

Introduction

  1. Terminology

    • Shell: program that interprets and acts on user input from the command line. Linux distros usually include a selection - bash, csh, tsh.
    • Environment: set of defined variables passed to a program when it executes.
    • Profile: set of environment variables and default configurations set up for a user at login
  2. The key files that may be read during login include:

    • /etc/profile
    • /etc/passwd - sets your home directory and startup shell
    • $HOME/.bashrc - assuming that your login is configured to use bash
  3. Useful online manuals are available at: Red Hat
  4. One page Linux manual

Linux - Basics

Shells

A shell is a program that is used by a user to communicate with the operating system kernel. The default shell for MSDOS is 'command.com'. The shell has two main jobs:

Linux has several popular shells including:

Hierarchical file system

The files in a system form a hierarchy as in MSDOS, but unlike MSDOS there is, usually, only one file system on a computer running Linux. The single file system may be distributed across more than one physical disk drive. Within the file system each user:

Each file/directory

The following symbols represent important directories and can be used in commands:

File names

Each Linux file has a filename that can have a maximum of 255 characters (depending on the file system in use). If the file is not in the current directory, the directory in which it is located is also required to give a full file specification. e.g.

      hello.c
      /u/fred/myprog.save

Many characters have a special meaning in Linux and should not be used in filenames. The safest characters are :

Filenames are case sensitive - FRED, Fred and fred are different names.

Logging in

The initial Linux prompt appears as:

      login:

At the prompt type in your account name. Different systems require different actions to achieve the 'login' prompt. After typing your account name at the 'login' prompt, you will be asked to enter a password.

      password:

Type in your password - note that it does not appear on the screen as you type it. If your password is entered correctly, a shell is loaded and a prompt displayed.

      $_

Logging out

The command to leave the system depends on which shell you are using:
bash
exit
C Shell
logout
Any - EOF code
  ^D (Control + D)

^D is the Linux end of file character and will cause the shell to terminate, thus ending the login session.

If you are not sure which shell you are using type:

	echo $SHELL

Command format

Linux commands lines, typed at the shell prompt, have the form:

	command  [Options]  [Arguments]

Arguments are usually files and/or directories. Many commands require a list of one or more files to process. These files are mostly used only as input data and are not changed by the command. If a command requires input data and no files are specified, the command will read from the keyboard.

Options are preceded by a '-'. Most commands have several options which alter the output produced. Options can be listed separately or grouped together:

      ls -l -t
is the same as
      ls -lt

Directories

Commands for creating, removing and changing to directories are very similar to those used in MSDOS:
cd 
change directory to $HOME - the user's login directory
cd path 
make the specified directory current
mkdir path  
create a new directory
rmdir  path 
remove a directory

Shell substitution

A Linux shell interprets * and ? as wild cards in the same way that the MSDOS shell 'command.com' does.

The pattern [...] matches any characters placed inside the brackets

echo is a useful command that displays its arguments on the screen. It is especially valuable for experimenting with the meaning of wild card and other characters. Try:

      echo hello world!
      echo *
      echo .*

Re-direction

Linux supports pipes and input-output re-direction using the same symbols as MSDOS.

  • cmd < file - read input to cmd from file
  • cmd > file - write output from cmd to file, overwrite an existing file
  • cmd >> file - add data to the end of file , if it exists, or create new file
  • cmd1 | cmd2 - pipe, execute both commands, output from cmd1 becomes input to cmd2

Common commands

Some of the more frequently used commands and options are introduced in this section. Refer to 'Commands reference' for more detailed descriptions.

List - ls

A list of files and sub-directories can be obtained using ls:

  • ls
  • ls /usr/bin

A more detailed listing can be obtained with the l option:

      ls  -l

Concatenate - cat

To look at the contents of a (text) file type 'cat filename' e.g.

      cat  .profile

In general cat takes a list of files as input and displays them one after another. If no filenames are provided, cat will read from the standard input, the keyboard, until Ctrl-D (end of file) is entered.

Page - pg

Larger files may disappear off the top of the screen before you can read them. Either use Ctrl-S and Ctrl-Q or use pg

      pg  .profile

The pg command can be used to scroll through the file. At the : prompt type h for help. pg is often used with pipes:

      ls  /usr/bin  |  pg

Copy - cp

To make a copy of a file 'cp oldfile newfile' e.g.

      cp  .profile   prof

Move a file - mv

This command performs the same function as MSDOS ren: 'mv oldname newname' e.g.

      mv  prof new.prof

mv can also be used to move a file from one directory to another.

      mkdir  spare
      mv  new.prof  spare

Remove a file - rm

This command performs the same functions as MSDOS del:

      rm filename

Linux assumes that you know what you are doing. It may not ask for confirmation, even for *, unless the -i (interactive) option is used. Take care.

Exercise

Carry out the following operations and make a note of the commands used:

  1. Open a terminal window
  2. Use ls to create a file called junk
          ls  -l  /usr/bin  >  junk
  3. Make 3 copies of junk called junk1, junk2, junk3
  4. Change the name of junk1 to commands
  5. Delete junk2 and junk3
  6. Examine the contents of commands with pg.
  7. How many options does pg provide at the : prompt?
  8. Create a sub-directory called temp and copy all of the files in your current directory into it.
  9. Make temp the current directory.
  10. Try:
          ls .* 
    what is happening here?
  11. Delete all of the files in temp.
  12. Return to your home directory.
  13. Remove directory temp.
  14. What is the full path of the current directory?
  15. /usr/bin contains the Linux commands. How many commands begin with the letter 'c'?

Searching for files

find

The find command is a very useful tool for searching the file systems for files and carrying out actions on them. It takes the form

      find <start from> <pattern type> <pattern> <action>
The default action is to just list matching files. For example:
      find . -name *.txt
Searches the file system, starting from the current directory, for files ending in '.txt'. This example finds all files in the system that have been modified in the last day:
      find / -mtime 1 
You can avoid a lot of error messages by su'ing to root before trying these.

Searching inside files

grep

The grep command is a very useful tool for patterns within text files. It takes the form

      grep <pattern> <file list> 
For example:
      grep "fred" *.txt 
Searches for the pattern fred in files ending in '.txt'. If the pattern expression is simple, as in this case, the quotes can be ommitted. This example finds all in the directory /etc and sub-directories that contain the pattern "eth0".
      grep -R eth0 * 

Exercise

Try the following:
  1. Open a terminal windows and su to root
  2. What is the command that searches the directory /etc/sysconfig and it's subdirectories for files containing the pattern eth0?
  3. What is the command that lists files in the directory /var/log that have been modified in the last 24 hours?

Basic command list

Command summary:
lslist names of all files in current directory
ls filenameslist only the named files
ls -tlist in time order, most recent first
ls -llist long; more information; also ls -lt
ls -Rlist recursive
ls -alist all files, including names starting with . (normally hidden)
cp file1 file2copy file1 to file2, overwrite file2 if it exists
mv file1 file2move file1 to file2 (rename), overwrite file2 if it exists
rm filenamesremove named files (delete)
cat filenamesprint contents of named files (type)
wc filenamescount lines, words and characters for each file
wc -l filenamescount lines for each file
echo argsprint arguments on the\screen
sort filenamessort files alphabetically by line
tail filenameprint last 10 lines of file
tail -n filenameprint last n lines of file
tail +n filenameprint file starting from line n
cmp file1 file2print location of first difference
diff file1 file2print all differences
cdchange directory to $HOME, login directory
cd pathchange to specified directory
cd ..change to directory above this one
pwdprint working directory, current
mkdir dirnamecreate a sub-directory
rmdir dirnameremove a sub-directory
find ...Search for files
grep pattern filesSearch files for text patterns

Website Copyright© 1995 - 2009 by: Paramount Network Systems. All Rights Reserved.