101 3.1 gnu and unix commands v4

33
Core Linux for Red Hat and Fedora learning under GNU Free Documentation License - Copyleft (c) Acácio Oliveira 2012 Everyone is permitted to copy and distribute verbatim copies of this license document, changing is allowed Linux Essenciais and System Administration

Upload: acacio-oliveira

Post on 13-Jul-2015

89 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Linux Essenciais and System Administration

Page 2: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

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.

Unix Commands

Work on the command line

Terms and Utilities

. Bash echoenv exec Exportpwd set unsetman uname history

2

Page 3: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Syntax:$ command options arguments

Using the Command Line

3

$ 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 4: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Basic Commands

Using the Command Line

4

$ who #Lists currently logged in users

$ uptime #Statistics about machine usage and run time

$ echo #Prints the given arguments to the screen

$ date #Print current date and time

$ exit #Terminate current shell session

$ reset #Reset terminal state to default settings

Ex:

Page 5: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Using the Command Line

5

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 6: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Command Line Working

6

1. Hierarchie Organization

•Data is stored in files•Files are grouped and organized in Directories, creating a tree structure•Filesystem begins at root, represented as: /•The Standard Hierarchy provides basic organization

2. Working Directory•Operations within shell generally gather input from files and output information to files, so the shell tracks a “working directory” to ease file specifications, and have a default location to output files if one is not provided.

$ pwd # Print Working DirectoryEx:

$ cd # Change [working] Directory

Page 7: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Command Line Working

7

3. Pathnames

•A pathname specifies exact location of a file or directory within the filesystem. There are two types of pathnames: absolute and relative

Absolute pathnameAn absolute pathname uses root of the filesystem as starting location for path search.Ex: /etc/passwdStarting from /, descend into etc folder, then locate file named passwdThe key is the leading slash - exactly giving the starting point

Relative pathnameRelative pathnames only specify a file’s location with respect to a working directory. Path is relative to current working directory. Relative pathnames never start with /.Ex: memo/january.txt

Page 8: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Command Line Working

8

4. Pathname Wildcards

Wildcards are a set of metacharacters which provide a shorthand notation for specifying large groups of files. There are 3 basic pathname wildcards:

* Match 0 or more characters. Any characters. Ex: * ; a* ; *.txt

? Matches exactly 1 character. Can be any character, but there must be exactly 1.Ex: file?.txt ; log-???? ; ????*

[set] Match exactly 1 character, character must be from the set. Great flexibility in specifying the set.Useful when filenames are following a specific pattern.

Ex: log-2009-1.[012]-* ; [a-zA-Z]*

•Each desired character can be directly typed into the set: [012345]

•Ranges are acceptable. Starting point must be “less” than ending point. Starting/ending case must match for letters: [0-5] ; [d-h] ; [N-Z]

•Can Mix and match: [0-9a-zA-Z].

•If a hyphen is needed to be part of the set, specify it first: [-acg0-4]

•You can specify an “anti” set. Anything listed in set will not match: start set with !:[!0-9]

Page 9: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

In order to master the shell, you have to understand it’s inner workingsconcept of The Big Loop:

Shells

9

1. Print prompt, await user input

2. Parse and verify input; on error, loopSyntax checking, command identification, metacharacter substitutions and operations

3. Perform requested operation ( execute command, built-in )

4. Loop

Page 10: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

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

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

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

Shells

10

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

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.Related information to environment goes to /etc/profile

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

Page 11: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

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

Shells

11

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 12: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Using the Command Line

12

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 13: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

<command> [options] [arguments]

•Everything is separated with white space

•Options are just a special interpretation of arguments, generally identified with a prefixed hyphen

•POSIX options (long options) use double hyphen prefixoften spell out option with a word rather than just a letter ( --verbose instead of -v )

Command Line Syntax

13

- Arguments are separated with whitespace- sometimes whitespace needs to be part of the argument itself (ex: spaces in filenames)

command filename with spaces Without any guidance, the shell will interpret this input as a command with various arguments.

Quoting is the easiest way to guide the shell in this matter.

There are two forms...

Page 14: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Single quotes: command ‘filename with spaces’

-Quotes let shell know where an argument starts and stops (quotes not included);-It doesn’t bother with what’s between markers - its interpreted strictly as data; this line would be interpreted as a command with one argument: filename with spaces

Command Quoting

14

Double quotes: command “filename with spaces” - equals single syntax, but interpret differently:

-Quotes let the shell know where an argument starts and stops, -but data between is loosely examined for metacharacters.this line would also be interpreted as a command with one argument: filename with spaces

Metacharacter any character that has more than one meaning or interpretation. Ex: single and double quotes. -In normal context, they denote endpoints for arguments, not actual quote characters;

-But if you need a quote in your argument value (ex: filename smith’s) use the \backslash: \ escape metacharacter command (ex: filename smith\’s) tells shell to interpret character following backslash as normal character, not a metacharacter This allows you to use metacharacters as regular characters

Page 15: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Using the Command Line

15

History filehistory 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 16: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Using the Command Line

16

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 17: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

17

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 18: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment

18

Every piece of running software has it’s own environmentenvironment is a collection of KEY->value pairs

1.The KEY is [traditionally capitalized] letters, numbers and symbols to uniquely identify the variable 2.The value is a string

Ex: PATH=/usr/local/bin:/usr/bin:/bin:/sbinHOME=/home/bobTOTAL=348

To create a new variable (or change an existing one):TOTAL=100 Type the name of the variable, an equals sign, and the value. Quoting if needed.

Once a variable is created, view it’s value with the $ metacharacter.

-The easiest way is to use echo: echo $TOTAL equals echo 100-$ metacharacter asks shell to look for value of named variable, and replace everything with that value.

Page 19: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

19

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 20: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

20

Prompt build-in for handling settingsSettings Used To Configure the Prompt

Setting Meaning

\u User name of the current user.\h The name of the computer running the shell (hostname).\set.\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 21: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

21

$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 22: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

22

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 in env by typing /PS1foo:~ $ HOME=/varfoo:~ $ cdfoo:~ $ pwdfoo:~ $ HOME=~

Ex:

Page 23: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

23

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 24: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment

24

Environment variables are local to the containing processIts possible to mark variables “exported” - allows to be passed down to sub-processes (child processes)

-Once a variable is created, to mark it exported:export TOTAL (no $ metacharacter)

-Stop exporting: export -n TOTAL

set: Displays all environment variables and valuesenv: Displays exported environment variables and values

To remove a variable completely:unset TOTAL

Page 25: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

25

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 26: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Environment variables

26

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 27: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Bash session

27

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 executable command to replace the shell

Ex:

Page 28: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

Recursive commands

28

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 29: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

man pages

29

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.

Manpages can be written to cover any topic, but generally are available for commands, libraries, function calls, kernel modules and configuration files.

Manual pages have:

•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•Synopsis of the options and parameters applicable to the command•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. Ex: 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 30: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

man pages

30

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 31: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

GNU and Unix Commands

infopages

31

There is some movement to convert the aging manpage system into a newer format, the infopage system.

Info system provides a more advanced interface, supporting links, split windows and more.

Accessing infopages is the same as man:

info <topic>Once within the info system, type ? for help on the interface

Page 32: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

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

32

GNU and Unix Commands

Page 33: 101 3.1 gnu and unix commands v4

Core

Lin

ux fo

r Re

d H

at a

nd F

edor

a le

arni

ng u

nder

GN

U F

ree

Doc

umen

tatio

n Li

cens

e -

Copy

left

(c) A

cáci

o O

livei

ra 2

012

Ev

eryo

ne

is p

erm

itte

d to

co

py

and

dis

trib

ute

verb

atim

co

pie

s o

f th

is li

cen

se d

ocu

me

nt,

cha

ngin

g is

allo

wed

Fim de sessão

33