7. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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 pathname
An absolute pathname uses root of the filesystem as starting location for path search.
Ex: /etc/passwd
Starting from /, descend into etc folder, then locate file named passwd
The key is the leading slash - exactly giving the starting point
Relative pathname
Relative 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
8. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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]
10. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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
11. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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 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.
12. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
Using the Command Line
12
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
13. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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 prefix
often 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...
14. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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
15. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
Using the Command Line
15
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.
18. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
Environment
18
Every piece of running software has it’s own environment
environment 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:/sbin
HOME=/home/bob
TOTAL=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.
20. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
Environment variables
20
Prompt build-in for handling settings
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).
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).
21. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
Environment variables
21
$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:
28. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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.
29. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
GNU and Unix Commands
man pages
29
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.
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).
30. CoreLinuxforRedHatandFedoralearningunderGNUFreeDocumentationLicense-Copyleft(c)AcácioOliveira2012
Everyoneispermittedtocopyanddistributeverbatimcopiesofthislicensedocument,changingisallowed
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.