В рамках подготовки к сдаче экзамена 101 уровня Junior Level Linux Professional LPIC-1 публикую краткие описания команд и фактов из подготовительных материалов IBM к этому экзамену.
Using the command line
Shells also use three standard I/O streams:
• stdin is the standard input stream, which provides input to commands.
• stdout is the standard output stream, which displays output from
commands.
• stderr is the standard error stream, which displays error output from
commands.
echo command prints (or echos) its arguments to the terminal
all the extra spaces were compressed down to single
spaces in the output. To avoid this, you need to quote strings, using either double
quotes (") or single quotes (').
Normally echo will append a trailing
new line character to the output. Use the -n option to suppress this. Use the -e
option to enable certain backslash escaped characters to have special meaning.
When the backslash
character (\) is not quoted, it serves as an escape to signal bash itself to preserve
the literal meaning of the following character.
The simplest command sequence is just two commands separated by a semicolon
(;). Each command is executed in sequence. In any programmable environment,
commands return an indication of success or failure; Linux commands usually return
a zero value for success and a non-zero in the event of failure. You can introduce
some conditional processing into your list using the && and || control operators. If
you separate two commands with the control operator && then the second is
executed if and only if the first returns an exit value of zero. If you separate the
commands with ||, then the second one is executed only if the first one returns a
non-zero exit code.
You can terminate a shell using the exit command.
Some common bash environment variables
USER The name of the logged-in user
UID The numeric user id of the logged-in user
HOME The user's home directory
PWD The current working directory
SHELL The name of the shell
$ The process id (or PIDof the running bash shell (or other)
process
PPID The process id of the process that started this process (that is, the id
of the parent process)
? The exit code of the last command
[ian@echidna ian]$ echo $USER $UID
ian 500
[ian@echidna ian]$ echo $SHELL $HOME $PWD
/bin/bash /home/ian /home/ian
use the export command to export a variable name.
Sometimes you need to use variables in expressions where the meaning is ambiguous. In such cases, you can use curly braces to delineate a variable name
[ian@echidna ian]$ echo "-$HOME/abc-"
-/home/ian/abc-
The env command without any options or parameters displays the current environment variables.
use the unset command to unset a variable and remove it from the shell variable list
use the set command to control many facets of the way bash (or other shells) work
use the exec command to run another program that replaces the current shell.
history
Displays the entire history
history N
Displays the last N lines of your history
history -d N
Deletes line N form your history; you might do this if the line contains a
password, for example
!!
Your most recent command
!N
The Nth history command
!-N
The command that is N commands back in the history (!-1 is equivalent to !!)
!#
The current command you are typing
!string
The most recent command that starts with string
Emacs keystroke combinations are usually expressed as C-x or M-x,
where x is a regular key and C and M are the Control and Meta keys, respectively.
On a typical PC system, the Ctrl key serves as the Emacs Control key, and the Alt
key serves as the Meta key.
If you use cd with no parameters, the change
will be to your home directory. A single hyphen (-) as a parameter means to change
to the previous working directory. Your home directory is stored in the HOME
environment variable, and the previous directory is stored in the OLDPWD variable,
so cd alone is equivalent to cd $HOME and cd - is equivalent to cd $OLDPWD.
Usually we say change directory instead of the full change current working directory.
The primary (and traditional) source of documentation is the manual pages, which
you can access using the man command.
The common manual sections, with some example contents
are:
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)
Two important commands related to man are whatis and apropos. The whatis
command searches man pages for the name you give and displays the name
information from the appropriate manual pages. The apropos command does a
keyword search of manual pages and lists ones containing your keyword.
If you wish to print the page, specify the -t option to format the page for printing using the groff or troff program.
In addition to manual pages accessible from a command line, the Free Software
Foundation has created a number of info files that are processed with the info
program.
Section 3. Text streams and filters
In order to use the output of a command, command1, as input to a filter, command2, you connect the commands using the pipe operator (|)
output redirection operator (>). output does not display on the terminal because it has been
redirected to the file.
Use the cat (short for catenate) command to display the contents of a file on stdout.
Occasionally, you might want to display a file in reverse order. Naturally, there is a
text filter for that too, called tac (reverse of cat).
GNU text utilities include an od (or Octal Dump)
The split command will do this in such a way that the cat command can be used to recreate the file easily. By default, the files resulting from the split command have a prefix in their name of 'x' followed by
a suffix of 'aa', 'ab', 'ac', ..., 'ba', 'bb', etc.
The wc command displays the number of lines, words, and bytes in a file.
Options allow you to control the output from wc or to display other information such as maximum line length.
Two commands allow you to display either the first part (head) or last part (tail).
Another common use of tail is to follow a file using the -f option, usually with a line count of 1.
Sometimes you may want to swap tabs for spaces or vice versa. The expand and
unexpand commands do this. The -t option to both commands allows you to set
the tab stops.
Use the tr command, which translates characters in one set (set1) to corresponding characters in another set (set2).
The nl command numbers lines, which can be convenient when printing files.
The pr command is used to format files for printing. The default header includes the
file name and file creation date and time, along with a page number and two lines of
blank footer.
Useful command for formatting text is the fmt command, which formats text
so it fits within margins.
The sort command sorts the input using the collating sequence for the locale
(LC_COLLATE) of the system The sort command can also merge already sorted
files and check whether a file is sorted or not.
The uniq command normally operates on sorted files, but remove consecutive identical
lines from any file, whether sorted or not.
the cut command, which extracts fields from text files.
The paste command pastes lines from two or more files side-by-side, similar to the way that the pr command merges files using its -m option.
join, which joins files based on a matching field. The files should be sorted on the join field
Sed is the stream editor. Sed is extremely powerful, and the tasks it can accomplish are limited only by your imagination. sed can work as a
filter or take its input from a file. Output is to the standard output stream. Sed loads
lines from the input into the pattern space, applies sed editing commands to the
contents of the pattern space, and then writes the pattern space to standard output.
Section 4. Basic file management
On a storage device, a file or directory is contained in a collection of blocks.
Information about a file is contained in an inode which records information such as
the owner, when the file was last accessed, how large it is, whether it is a directory
or not, and who can read or write it. The inode number is also known as the file
serial number and is unique within a particular filesystem
use ls -ld and also how to list entries for multiple files or directories.
ls -t will sort by modification time (newest to oldest) while ls
-lS will produce a long listing sorted by size (largest to smallest). Adding -r will
reverse the sort order. for example, use ls -lrt to produce a long listing sorted
from oldest to newest.
cp
is used to make a copy of one or more files. You must give at least two names,
one (or more source names and one target name. If you specify two file names,
then the first is copied to the second. Either source or target file name may
include a path specification. If you specify a directory as the last name, then
you may specify multiple files to be copied into it. All files will be copied from
their existing locations and the copies will have the same file names as the as
the original files. Note that there is no default assumption of the target being
the current directory as in DOS and Windows operating systems.
mv
is used to move or rename one or more files or directories. In general, the
names you may use follow the same rules as for copying with cp; you can
rename a single file or move a set of files into a new directory. Since the name
is only a directory entry that links to an inode, it should be no surprise that the
inode number does not change unless the file is moved to another filesystem,
in which case moving it behaves more like a copy followed by deleting the
original.
rm
is used to remove one or more files. We will see how to remove directories
shortly.
-f or --force
will cause cp to attempt to remove an existing target file even if it is not
writable.
-i or --interactive
will ask for confirmation before attempting to replace and existing file
-b or --backup
will make a backup of any files that would be replaced.
rm command also accepts the -i (interactive) and -f (force options.
Once you remove a file using rm, the filesystem no longer has access to it.
The -p option may be used to preserve selected attributes.
The mkdir, like the commands we have just been reviewing, will handle
multiple directory creation requests in one pass
Removing directories using the rmdir command is the opposite of creating them.
Again, there is a -p option to remove parents as well.
You can use the -r (or -R or --recursive) option to cause the cp command to
descend into source directories and copy the contents recursively.
A string containing any of the characters '?', '*' or '[', is a wildcard pattern. Globbing is
the process by which the shell (or possibly another program) expands these patterns
into a list of pathnames matching the pattern. The matching is done as follows.
?
matches any single character.
*
matches any string, including an empty string.
[
introduces a character class. A character class is a non-empty string,
terminated by a ']'. A match means matching any single character enclosed by
the brackets.
• The '*' and '?' characters match themselves. If you use these in filenames,
you will need to be really careful about appropriate quoting or escaping.
• Since the string must be non-empty and terminated by ']', you must put ']'
first in the string if you want to match it.
• The '-' character between two others represents a range which includes
the two other characters and all between in the collating sequence. For
example, [0-9a-fA-F] represents any upper or lower case hexadecimal
digit. You can match a '-' by putting it either first or last within a range.
• The '!' character specified as the first character of a range complements
the range so that it matches any character except the remaining
characters. For example [!0-9] means any character except the digits 0
through 9. A '!' in any position other than the first matches itself.
Remember that '!' is also used with the shell history function, so you need
to be careful to properly escape it.
the touch command which can update file access and modification times or create empty files.
find command which is used to find files in one or more directory trees, based on criteria such as name, timestamp, or size.
The -exec option can be used for as many purposes as your imagination can
dream up. For example:
find . -empty -exec rm '{}' \;
removes all the empty files in a directory tree, while
find . -name "*.htm" -exec mv '{}' '{}l' \;
renames all .htm file to .html files.
Section 5. Streams, pipes, and redirects
There are two ways to redirect output:
n>
redirects output from file descriptor n to a file. You must have write authority to
the file. If the file does not exist, it is created. If it does exist, the existing
contents are usually lost without any warning.
n>>
also redirects output from file descriptor n to a file. Again, you must have write
authority to the file. If the file does not exist, it is created. If it does exist, the
output is appended to the existing file.
Use &> or &>> to redirect both standard output and standard
error to the same place.
At other times you may want to ignore either standard output or standard error
entirely. To do this, redirect the appropriate stream to /dev/null.
we can redirect the stdout and stderr streams, so too we can redirect stdin
from a file, using the < operator.
Shells, including bash, also have the concept of a here-document, which is another
form of input redirection. This uses the << style="font-weight: bold;">| (pipe) operator between two commands to direct the stdout of the first to the stdin of the second.
The xargs command works a little like the -exec option of find, but there are some
important differences
Section 6. Create, monitor, and kill processes
the ps command which displayed process status and we saw that processes have a
Process ID (PID) and a Parent Process id (PPID)
What you need to do is place the job in the background; a bg command takes the same type of job specification as the fg command and does exactly that.
job 2 has a plus sign (+) beside the job number, indicating that it is the
current job.
The nohup command is used to start a command that will ignore hangup signals
and will append stdout and stderr to a file.
If we use the jobs command with the -p option, the
output is simply the PID of the process group leader for each job.
If you run ps several times in a row, to see what is changing, you probably need the
top command instead.
The general way to send a signal is with the kill command.
Section 7. Process execution priorities
The nice command displays our default priority.
If you happen to start a process and realize that it should run at a different priority, there is a way to change it after it has started, using the renice command.
Section 8. Searching with regular expressions
A regular expression (also called a "regex" or "regexp") is a way of describing a text
string or pattern so that a program can match the pattern against arbitrary text
strings, providing an extremely powerful search capability.
Most characters match themselves, and most metacharacters must
be escaped using a backslash (\).
+
The + operator is like the * operator, except that it matches one or more
occurrences of the preceding regular expression. It must be escaped for basic
expressions.
?
The ? indicates that the preceding expression is optional, so it represents zero
or one occurrences of it. This is not the same as the ? used in globbing.
.
The . (dot) is a metacharacter that stands for any character. One of the most
commonly used patterns is .*, which matches an arbitrary length string
containing any characters (or no characters at all). Needless to say, you will
find this used as part of a longer expression. Compare a single dot with the ?
used in globbing and .* with the * used in globbing.
The ^ (caret) matches the beginning of a line while the $ (dollar sign) matches the
end of line. So ^..b matches any two characters at the beginning of a line followed
by a b, while ar$ matches any line ending in ar. The regular expression ^$ matches
an empty line.
Range expression
A range expression is two characters separated by a - (hyphen), such as 0-9
for digits, or 0-9a-fA-F for hexadecimal digits. Note that ranges are
locale-dependent.
Named classes
Several named classes provide a convenient shorthand for commonly used
classes. Named classes open with [: and close with :]. Some examples:
[:alnum:]
Alphanumeric characters
[:blank:]
Space and tab characters
[:digit:]
The digits 0 through 9 (equivalent to 0-9)
`[:upper:] and [:lower:]
Upper and lower case letters respectively.
^ (negation)
When used as the first character in a square brackets, the ^ (caret) negates the
sense of the remaining characters so the match
Section 9. File editing with vi
vi modes
The vi editor has two modes of operation:
Command mode
In command mode, you move around the file and perform editing operations
such as searching for text, deleting text, changing text, and so on. You usually
start in command mode.
Insert mode
In insert mode, you type new text into the file at the insertion point. To return to
command mode, press the Esc (Escape) key.
:q!
Quit editing the file and abandon all changes. This is a very common idiom for
getting out of trouble.
:w!
Write the file (whether modified or not). Attempt to overwrite existing files or
read-only or other unwriteable files. You may give a filename as a parameter,
and that file will be written instead of the one your started with. It's generally
safer to omit the ! unless you know what you're doing here.
ZZ
Write the file if it has been modified. Then exit. This is a very common idiom for
normal vi exit.
:e!
Edit the current disk copy of the file. This will reload the file, abandoning
changes you have made. You may also use this if the disk copy has changed
for some other reason and you want the latest version.
:!
Run a shell command. Type the command and press Enter. When the
command completes, you will see the output and a prompt to return to vi
editing.
Moving around
The following commands help you move around in a file:
h
Move left one character on the current line
j
Move down to the next line
k
Move up to the previous line
l
Move right one character on the current line
w
Move to the next word on the current line
e
Move to the next end of word on the current line
b
Move to the previous beginning of the word on the current line
Ctrl-f
Scroll forward one page
Ctrl-b
Scroll backward one page
Moving to lines
The following commands help you move to specific lines in your file:
G
Moves to a specific line in your file. For example, 3G moves to line 3. With no
parameter, G moves to the last line of the file.
H
Moves relative to the top line on the screen. For example, 3H moves to the line
currently 3rd from the top of your screen.
L
Is like H, except that movement is relative to the last line on screen, Thus, 2L
moves to the second to last line on your screen.
Searching
You can search for text in your file using regular expressions:
/
Use / followed by a regular expression to search forward in your file.
?
Use ? followed by a regular expression to search backward in your file.
n
Use n to repeat the last search in either direction.
i
Enter insert mode before the character at the current position. Type your text
and press Esc to return to command mode. Use I to insert at the beginning of
the current line.
a
Enter insert mode after the character at the current position. Type your text and
press Esc to return to command mode. Use A to insert at the end of the current
line.
c
Use c to change the current character and enter insert mode to type
replacement characters.
o
Open a new line for text insertion below the current line. Use O to open a line
above the current line.
cw
Delete the remainder of the current word and enter insert mode to replace it.
Use a repetition count to replace multiple words. Use c$ to replace to end of
line.
dw
As for cw (and c$) above, except that insert mode is not entered.
dd
Delete the current line. Use a repetition count to delete multiple lines.
x
Delete the character at the cursor position. Use a repetition count to delete
multiple characters.
p
Put the last deleted text after the current character. Use P to put it before the
current character.
xp
This combination of x and p is a useful idiom. This swaps the character at the
cursor position with the one on its right.
2 комментария:
Amiable post and this post helped me alot in my college assignement. Thank you as your information.
Well I assent to but I contemplate the brief should have more info then it has.
Отправить комментарий