101 3.1 gnu and unix commands

23
Junior Level Linux Certification

description

 

Transcript of 101 3.1 gnu and unix commands

Page 1: 101 3.1 gnu and unix commands

Junior Level Linux Certification

Page 2: 101 3.1 gnu and unix commands

Exam Objectives

Key Knowledge Areas

Use single shell commands and one line command sequences to perform basic tasks on the command line. Use and modify the shell environment including defining, referencing and exporting environment variables. Use and edit command history. Invoke commands inside and outside the defined path.

Objective 3: GNU and Unix Commands

Work on the command line Weight: 4

Terms and Utilities

. Bash echoenv exec Exportpwd set unsetman uname history

2

Page 3: 101 3.1 gnu and unix commands

GNU and Unix Commands

The shell allows users to enter commands and then interprets these commandsinto instructions for the Linux operating system.

Linux allows you to use a number of different shells; however, the default shell installed with Linux is the bash shell.

default shell is specified in /etc/passwd file and can be changed there on a per user basis.

Shells

3

To change to a different shell, you simply type the full path along with the command name of the new shell. Ex. /bin/csh

To return to the default login shell, type exit or press CTRL-D.

The default login shell is contained in the environment variable SHELL.Each shell has corresponding resource configuration file - rc file, located in /etc directory It contains global settings for the shell. For the bash shell the file is /etc/bashrc.

To know which default shell is currently configured, type: echo $SHELL

Page 4: 101 3.1 gnu and unix commands

GNU and Unix Commands

The available shells and paths are listed in /etc/shells

Shells

4

Shell Explanation

/bin/bash Bourne-Again Shell that is compatible with the sh shell, which includes features of both the Korn and C shells. This may be a link to the /bin/bash2 file on some distributions.

/bin/sh The Bourne Shell. On many systems this file is linked to /bin/bash.

/bin/ash A System V version of the sh shell.

/bin/bsh This file is linked to /bin/ash.

/bin/bash2 Bourne-Again Shell version 2.

/bin/csh The Berkeley UNIX C shell.

/bin/tcsh An enhanced version of the Berkeley UNIX C shell.

chsh command is used to change the default shell for the userOptions Alternate Function

-s --shells Specifies the default login shell for this user.-l --list Lists the shells specified in the /etc/shells file.-u --help Displays the options for the chsh command.-v --version Displays version information for the shell.

Page 5: 101 3.1 gnu and unix commands

GNU and Unix Commands

Syntax:$ command options arguments

Using the Command Line

5

$ ls # simple commandEx:

$ ls –F # command with options

$ ls -F /etc # execute command on other directory

$ ls -F /etc ; ls -F /usr # first command will complete before next command is started

$ ls -F /etc \ # ignore Enter key and treat all commands as though they are on the same cmdline.

ls -F /usr

Page 6: 101 3.1 gnu and unix commands

GNU and Unix Commands

Using the Command Line

6

Command completion:

bash shell includes a feature called command completion. This enables to type first few letters of the command at the prompt, hit the Tab key, and have the system complete the command.

$ dm <press tab key>Ex:

$ dmesg

• If there is more than one possible match, the system will simply beep. • Pressing Tab key again will display all possible command matches. • Pressing Esc twice performs same action as pressing the Tab key.

Page 7: 101 3.1 gnu and unix commands

GNU and Unix Commands

Using the Command Line

7

Editing shell commands with the Readline Library:

Key Combination FunctionCtrl-b Move back one character.Ctrl-f Move forward one character.Del Delete the character to the left of the cursor.Ctrl-d Delete the character underneath the cursor.Ctrl-a Move to the start of the line.Ctrl-e Move to the end of the line.Esc-f Move forward a word.Esc-b Move backward a word.Ctrl-l Clear the screen, reprinting the current line at the top.Ctrl-k Kill (delete) the text from the current cursor position to the end of the line.Esc-d Kill from the cursor to the end of the current word, or if between words, to the end of the next word.Esc-Del Kill from the cursor the start of the previous word, or if between words, to the start of the previous word.Ctrl-w Kill from the cursor to the previous white space.

This differs from Esc-Del because the boundaries separating words differ.

•Editor can be configured globally using /etc/inputrc file for global changes. •To make changes for specific user edit .inputrc file located in user’s home directory. This file can be used to change key mappings for Readline Library editor, and map keys to commonly used commands. •To view keyboard bindings type the command: bind -v

Page 8: 101 3.1 gnu and unix commands

GNU and Unix Commands

Using the Command Line

8

History file

history file contains list of commands issued at command prompt.

Number of commands stored - Is specified by HISTSIZE environment variable in:/etc/profile or ~/.profile file - Default setting is 1,000 entries.

The command history displays all entries in the history file, which is ~/.bash_history.

HISTCMD variable is used to provide history index number of command currently being run.

HISTFILE variable specifies file used to contain the cmd history – default /home/user/.bash_history.

HISTFILESIZE variable specifies maximum number of lines contained in the HISTFILE.

Page 9: 101 3.1 gnu and unix commands

GNU and Unix Commands

Using the Command Line

9

fcprovides another option for editing commands in history file before running them.fc utility opens the command into default editor, to edit and save it before rerunning the command.fc utility allows to specify number of history events to edit, which enables to edit a range of cmds at once.

# fc –l 1020 1025

1020 cd /etc1021 ls -al1022 vi services1023 vi hosts1024 man ls1025 ls -al s*

Ex: Example uses fc utility to list the history events1020-1025.

Page 10: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

10

Command substitution $(...) and `...`Some commands say interesting things to include as parameters on other commands.command substitution operators allows to include output of a command in another command.

foo:~ $ whoamiGerald

foo:~ $ grep gerald /etc/passwdgerald:x:500:500:G Smith:/home/gerald:/bin/bashIt's much easier like using substitution:

foo:~ $ grep `whoami` /etc/passwdgerald:x:500:500: G Smith:/home/gerald:/bin/bash

foo:~ $ grep $( whoami ) /etc/passwdgerald:x:500:500: G Smith:/home/gerald:/bin/bash

Ex: # echo `pwd`

Ex:

Page 11: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

11

Prompt

The shell prompt can be configured by the user to display a variety of information.

[godmode@zeus godmode]$ _Ex:Godmode:login name; zeus: name of computer; 2nd godmode: current working directory; _: represents cursor.

prompt is set by environmental variable called PS1. To display the current settings use command echo $PS1

System-wide setting of prompt (for all users) is in file /etc/bashrc

[godmode@zeus godmode]$ cat /etc/bashrcEx:

To customize prompt, edit file /etc/bashrc (as root) and insert almost any text inside the quotes.

Page 12: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

12

PromptSettings Used To Configure the Prompt

Setting Meaning

\u User name of the current user.\h The name of the computer running the shell (hostname).\W The base of the name of the current working directory.\w The full name of the current working directory.\$ Displays “$” for normal users and “#” for the root.\! History number of the current command.\# Number of the current command.\d Current date.\t Current time.\s Name of the shell.\n New line.\\ Backslash.\[ Begins a sequence of nonprintable characters.\] Ends a sequence of nonprintable characters.\nnn The ASCII character corresponding to the octal number nnn.$(date) Output from the date command (or any other command for that matter).

Page 13: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

13

$PATHWhen you enter a command, bash searches for executable program in a number of directories. The places bash searches are specified in the PATH environment variable.

foo:~ $ echo $PATH/home/user/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin

Ex:

One of places that bash does not search by default is the current directory – usually. Some distributions do include the current directory in the path (spot the difference).

linux:~ > echo $PATH/bin:/usr/bin:/usr/X11R6/bin:/usr/local/bin:/home/joe/bin:

Ex:

Because of PATH environment variable, you can enter cmds in any working directory.you can use the ls cmd even when you are not in the /bin directory which contains the ls program.

linux:/usr/bin $ cd /binlinux:/bin $ ls -la ls-rwxr-xr-x 1 root root 90811 Apr 20 16:32 lslinux:/bin $ cd /usr/binlinux:/usr/bin $ ls -la ls/bin/ls: ls: No such file or directory

Ex:

Page 14: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

14

The shell environment contains values called environment variables which can be displayed and changed.

To change value of an environment variable, use the syntax: VARIABLENAME='value'

commands related to setting environment variables:

foo:~ $ set | lessfoo:~ $ echo PATHfoo:~ $ echo $PATHfoo:~ $ echo HOMEfoo:~ $ echo $HOMEfoo:~ $ echo $HOSTNAMEfoo:~ $ echo $PS1foo:~ $ PS1="# "foo:~ $ PS1='\u: \w \$ 'foo:~ $ env | less # search for PS1 by typing /PS1, Enter...foo:~ $ HOME=/varfoo:~ $ cdfoo:~ $ pwdfoo:~ $ HOME=~

Ex:

Page 15: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

15

command env displays list of all the exported environment variables. These are the variables that are passed from shell to apps when the applications are executed

foo:~ $ env | head

KDE_MULTIHEAD=falseSSH_AGENT_PID=511HOSTNAME=foo.ledge.co.zaTERM=xtermSHELL=/bin/bashXDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,schedHISTSIZE=1000GTK_RC_FILES=/etc/gtk/gtkrc:/home/joe/.gtkrcGS_LIB=/home/joe/.kde/share/fontsQTDIR=/usr/lib/qt3-gcc3.4

Ex:

Page 16: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

16

The behaviour of many commands can be customised by setting (and exporting) shellvariables as listed on the man pages of the application.

foo:~ $ echo PRINTER value is $PRINTERPRINTER value is

foo:~ $ lpqlp0 is readyno entries

foo:~ $ PRINTER=lp1

foo:~ $ export PRINTER

foo:~ $ lpqlp1 is not readyno entries

Ex: lpq command shows print queue for the current printer. current printer is set using the PRINTER environment variable.

Page 17: 101 3.1 gnu and unix commands

GNU and Unix Commands

Environment variables

17

To remove a variable, use the command unset.

foo:~ $ echo SSH_ASKPASS is $SSH_ASKPASSSSH_ASKPASS is /usr/libexec/openssh/gnome-ssh-askpass

Ex:

foo:~ $ unset SSH_ASKPASS

foo:~ $ echo SSH_ASKPASS is $SSH_ASKPASSSSH_ASKPASS isfoo:~ $

Page 18: 101 3.1 gnu and unix commands

GNU and Unix Commands

Bash session

18

During a login session, bash does a number of special things:

• At beginning of session, file ~/.profile is run automatically. Any special commands placed in this file are run whenever logged

• At end of the session, all commands entered are added to the file ~/.bash_history.

exec /usr/bin/pine # when pine exits, the session is over

If this is put in a user’s ~/.profile, that user will automatically run pine when logging.

contents of ~/.profile can include a exec command to replace the shell

Ex:

Page 19: 101 3.1 gnu and unix commands

GNU and Unix Commands

Recursive commands

19

Recursion in terms of Linux commands refers to applying a command to a all the files in a directory, and all the files in all the subdirectories (and subdirectories of subdirectories).

$ find /etc -type f -exec cat {} \; # find with find, then cat each one

Ex:

Some commands (ls, chown, chmod, cp, rm, grep) support switches – R or – r .commands without recursive mode, combine the cmd with find to achieve your results.

find ... Exec can execute a specific command each time a file is found:

$ cat `find -type f /etc` # cat whatever find says$ find /etc -type f | xargs cat # run cat with parameters from find

The output of find can list file names on the command line:

Using parameter -type f to find ensures that we only consider regular files. find can use other criteria to identify files - name, permissions, modification, date, etc.

Page 20: 101 3.1 gnu and unix commands

GNU and Unix Commands

man pages

20

The primary (and traditional) source of documentation is the manual pages, whichcan be accessed using command: man command or man section command

Manual pages are usually installed when you install a package, so if you do not have a package installed, you probably won't have a manual page for it.

Manual pages have:

•A heading with the name of the command followed by its section number in parentheses•The name of the command and any related commands that are described on the same man page•A synopsis of the options and parameters applicable to the command•A short description of the command•Detailed information on each of the options

There might be other sections on usage, how to report bugs, author information, and a list of related commands. For example, the man page for man tells us that related commands (and their manual sections) are: apropos(1), whatis(1), less(1), groff(1), and man.conf(5).

Page 21: 101 3.1 gnu and unix commands

GNU and Unix Commands

man pages

21

There are 8 common manual page sections:1. User commands (env, ls, echo, mkdir, tty)2. System calls or kernel functions (link, sethostname, mkdir)3. Library routines (acosh, asctime, btree, locale, XML::Parser)4. Device related information (isdn_audio, mouse, tty, zero)5. File format descriptions (keymaps, motd, wvdial.conf)6. Games (note that many games are now graphical and have graphical help outside the man page system)7. Miscellaneous (arp, boot, regex, unix utf8)8. System administration (debugfs, fdisk, fsck, mount, renice, rpm)

Other sections could include: 9.for Linux kernel documentation, n. new documentation,o. old documentation, l. local documentation.

2 important commands related to man are whatis and apropos.

whatis - searches man pages for the name given and displays name info from appropriate manual pages. apropos - does a keyword search of manual pages and lists ones containing the keyword.

Page 22: 101 3.1 gnu and unix commands

commands

. The current directorybash What you bash your commands intoecho echoenv Show environment variablesexec Run and don’t returnexport Add a variable to the export listman Manual pagespwd Print working directoryset Show environment settingsunset Clear an environment variable~/.bash_history The last n commands you typed~/.profile What runs when you login interactively

Test commands for exam

22

GNU and Unix Commands

Page 23: 101 3.1 gnu and unix commands

Fim de sessão

23