101 3.1 gnu and unix commands


Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

101 3.1 gnu and unix commands

  1. 1. Junior Level Linux Certification
  2. 2. 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 echo env exec Export pwd set unset man uname history 2
  3. 3. GNU and Unix Commands The shell allows users to enter commands and then interprets these commands into 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
  4. 4. 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 user Options 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.
  5. 5. 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
  6. 6. 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.
  7. 7. GNU and Unix Commands Using the Command Line 7 Editing shell commands with the Readline Library: Key Combination Function Ctrl-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
  8. 8. 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.
  9. 9. GNU and Unix Commands Using the Command Line 9 fc provides 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 /etc 1021 ls -al 1022 vi services 1023 vi hosts 1024 man ls 1025 ls -al s* Ex: Example uses fc utility to list the history events1020-1025.
  10. 10. 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:~ $ whoami Gerald foo:~ $ grep gerald /etc/passwd gerald:x:500:500:G Smith:/home/gerald:/bin/bash It's much easier like using substitution: foo:~ $ grep `whoami` /etc/passwd gerald:x:500:500: G Smith:/home/gerald:/bin/bash foo:~ $ grep $( whoami ) /etc/passwd gerald:x:500:500: G Smith:/home/gerald:/bin/bash Ex: # echo `pwd` Ex:
  11. 11. 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.
  12. 12. GNU and Unix Commands Environment variables 12 Prompt Settings 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).
  13. 13. GNU and Unix Commands Environment variables 13 $PATH When 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 /bin linux:/bin $ ls -la ls -rwxr-xr-x 1 root root 90811 Apr 20 16:32 ls linux:/bin $ cd /usr/bin linux:/usr/bin $ ls -la ls /bin/ls: ls: No such file or directory Ex:
  14. 14. 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 | less foo:~ $ echo PATH foo:~ $ echo $PATH foo:~ $ echo HOME foo:~ $ echo $HOME foo:~ $ echo $HOSTNAME foo:~ $ echo $PS1 foo:~ $ PS1="# " foo:~ $ PS1='u: w $ ' foo:~ $ env | less # search for PS1 by typing /PS1, Enter... foo:~ $ HOME=/var foo:~ $ cd foo:~ $ pwd foo:~ $ HOME=~ Ex:
  15. 15. 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=false SSH_AGENT_PID=511 HOSTNAME=foo.ledge.co.za TERM=xterm SHELL=/bin/bash XDM_MANAGED=/var/run/xdmctl/xdmctl-:0,maysd,mayfn,sched HISTSIZE=1000 GTK_RC_FILES=/etc/gtk/gtkrc:/home/joe/.gtkrc GS_LIB=/home/joe/.kde/share/fonts QTDIR=/usr/lib/qt3-gcc3.4 Ex:
  16. 16. GNU and Unix Commands Environment variables 16 The behaviour of many commands can be customised by setting (and exporting) shell variables as listed on the man pages of the application. foo:~ $ echo PRINTER value is $PRINTER PRINTER value is foo:~ $ lpq lp0 is ready no entries foo:~ $ PRINTER=lp1 foo:~ $ export PRINTER foo:~ $ lpq lp1 is not ready no entries Ex: lpq command shows print queue for the current printer. current printer is set using the PRINTER environment variable.
  17. 17. GNU and Unix Commands Environment variables 17 To remove a variable, use the command unset. foo:~ $ echo SSH_ASKPASS is $SSH_ASKPASS SSH_ASKPASS is /usr/libexec/openssh/gnome-ssh-askpass Ex: foo:~ $ unset SSH_ASKPASS foo:~ $ echo SSH_ASKPASS is $SSH_ASKPASS SSH_ASKPASS is foo:~ $
  18. 18. 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:
  19. 19. 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.
  20. 20. GNU and Unix Commands man pages 20 The primary (and traditional) source of documentation is the manual pages, which can 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).
  21. 21. 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.
  22. 22. commands . The current directory bash What you bash your commands into echo echo env Show environment variables exec Run and don’t return export Add a variable to the export list man Manual pages pwd Print working directory set Show environment settings unset 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
  23. 23. Fim de sessão 23