之前用ubunut大概有一个学期吧,尝试做过网站,中间遇到问题都是依靠搜索引擎,觉得还是有必要系统的学习Unix/Linux。尝试了两章Advanced Programming in the Unix Environment,感觉还是有点吃力,之前有看过《鸟叔的Linux私房菜》觉得不是我的菜。直到发现The Linux Command Line,嗯,TLCL是我的菜。
一些重要的话
Whenever we edit an important configuration file, it is always a good idea create a backup copy of the file first.
Do yourself a favor and add some comments.
vi is designed for typing speed.
The most important determinant of distribution quality is the packaging system and vitality of the distributions support community.
This idea of buffering is used extensively in computers to make them faster.
Why unmounting is important?
Unmounting a device entails writing all the remaining data to the device so that it can safely removed.
When it comes to networking, there is nothing cannot be down with Linux. Linux is used to build all sorts of networking systems and appliances, including firewalls, routers, name serves, NAS(Network Attached Storage)boxes and on and on.
One of the primary tasks of a computer systems administrator is keeping the system data secure.
Simply put, regular expressions are symbolic notations used to identify patterns in text.
一些重要的事实
Modern operating systems are usually multitasking, meaning that they creating the illusion of doing more than one thing at once by rapidly switching from one executing program to another. The Linux kernel manages this through the use of processes. Processes are how Linux organizes the different programs waiting for their turn at the CPU.
When a system starts up, the kernel initials a few of its own activities as processes and launches program called init. init in turn, runs a series of shell scripts (located in /etc) called init script which starts all the system services. Many of these services are implemented as daemon programs, programs that just sit in the background and do their thing without having any user interface.
The fact that a program can launch other program is expressed in the process scheme as a parent process producing a child process.
A program in the background is immune from keyboard input.
By launching a program from the command line you might be able to see error messages that would otherwise be invisible if the program were launched graphically.
We could have also specified the process using a jobspec (for example “%1”) instead of a PID
Processes, like files, have owners, and you must be the owner of the process (or the supervisor) in order to sent it signals with kill.
Shell maintains a body of information during our shell session called the environment. We can use the environment to customize our shell environment.
The shell stores two basic types of data in the environment. There environment variables and shell variables. The set builtin will show both environment variables and shell variables, while printenv will only display later.
In addition to reading the startup files above, non-login shells also inherit the environment from their parent process, usually a login shell.
PATH variables: A colon-separated list of directories that are searched when you enter the name of a executable program.
PATH=$PATH:$HOME/bin
The export command tells the shell to make the contents of PATH available to child processes of this shell.
Whenever we edit a important configure file, it is always a good idea create a backup copy of the file first.
The second important thing to learn about vi is that vi is a modal editor.
x 3x dd dG d$ d0 d^ d20G dW
While we have so far only used words phrases for our search patterns, vi allows the use of regular expressions.
\ : the colon character starts an ex command
% is a short cut meaning from the first line to the last line. Alternatively, the range could have been specified 1,5 or 1,$. If the range of lines is omitted, the operation is only performed on the current line.
s: specify the operation, in this case, substitution.
g: this means “global”, if omitted, only the first instance of the search string on each line is replaced.
To switch from one file to the next use this ex command : “:n”, on the other hand use “:N”
:r foot.txt
The “:r” inserts the specified file after the cursor position line.
Most distributions fall into one of two camps of packaging technologies: the Debian “.deb” camp and the Red Hat “.rpm” camp.
Package management systems usually consists of two types of tools: low-level tools which handle tasks such as installing and removing package files, and high-level tools that perform metadata searching and dependency resolution. Debian-Style: dpkg(low-level), apt-get/aptitude(high-level). Fedora, Red Hat Enterprise Linux, CentOS: rpm(low-level), yum(high-level).
A package can be located based on its name or description.
apt-get update; apt-cache search search-string
The most common package management task is keeping the system up-to-date with the latest packages. The high-level tools can perform this vital task in one single step.
apt-get update; apt-get upgrade
dpkge –status package-name
apt-get show package-name
Linux has amazing capabilities for handling storage devices, whether physical storage, such as hard disks, or network storage, or virtual storage devices like RAID (Redundant Array of Independent Disks) and LVM (Logical Volume Manager)
A mount point is simply a directory somewhere on the file system tree. Nothing special about it. It doesn’t even have to be an empty directory, though if you mount a device on a non-empty directory, you will not be able to see the directorys previous contents until unmount the device.
mount -t iso9660 /dev/hdc /mnt/cdrom
We cannot umount a device if the device is being used by someone or some process.
If you are working on a system that does not automatically mount removable devices, you can use the following technique to determine how the removable device is named when it is attached. First start a real-time view of the /var/log/messages file
sudo tail -f /var/log/messages
Using the tail -f /var/log/messages is a great way to watch what the system is doing in near real-time.
While we usually think of data on your computers as being organized into files, it is also possible to think of the data in “raw” form. However, if we could treat a disk drive as simply a large collection of data blocks, we could perform useful tasks, such as cloning devices.
A properly performing network will exhibit zero percent packet loss. A successful “ping” will indicate that the elements of the network (its interface cards, cabling, routing and gateways) are in generally good working order.
netstate using the “-ie” option we can examine the network interfaces in our system.
When performing causal network diagnostics, the important things to look for are the presence of the word “UP” at the beginning of the fourth line for each interface, indicating that the network interface is enabled, and the presence of a valid IP address in the inet addr field on the second line.
FTP is not secure, because it sends account names and passwords in cleartext. This means that they are not encrypted and anyone sniffing the network can see them. Because of this, almost all FTP done over the Internet is done by anonymous FTP servers. An anonymous FTP server allows anyone to login using the login name “anonymous” and a meaningless password.
Another popular line program for file downloading is wget. It is useful for downloading content from both web and FTP sites.
SSH solves the two basic problems of secure communication with remote host. First, it authenticates that the remote host is who it says it is (thus preventing so-called “man in the middle” attacks), and second, it encrypts all of the communications between the local and remote hosts.
SSH consists of two parts. An SSH server runs on the remote host, listening for incoming connections on port 22, while an SSH client is used on the local system to communicate with the remote server.
sftp works much like the original ftp program that we used earlier; however, instead of transmitting everything in cleartext, it use an SSH encrypted tunnel.
sftp has a important advantage over conventional ftp in that it does not require an FTP server to be running on the remote host. It only requires the SSH server.
If the search requirement is not so simple, locate can be combined with other tools such as grep to design more interesting searches:
locate zip | grep bin
The locate database is created by another program named updatedb. Usually it is run periodically as cron job; that is a task performed at regular intervals by the cron daemon. Most system equipped with locate run updatedb once a day. Since the database is not update continuesly, you will notice that very recent files do not show up when using locate. To overcome this, it’s possible to run the updatedb manually.
While the locate program can find a file based solely on its name , the find program searches a given directory (and its subdirectories) for files based on a variety of attributes.
-newer file: match files and directories whose contents were modified more recently than the specified file. This is very useful when writing shell scripts that perform file backups. Each time you make a backup, update a file (such as log), than use find to determine which files that have changed since last update.
Even with all the tests that find provides, we may still need a better way to describe the logical relationships between tests.
find ~ ( -type f -perm 0600 ) -or ( -type d -not -perm 0700 )
It should go without saying that you should use extreme caution when using the -delete action. Always test the command first by substituting the -print action for -delete to confirm the search results.
In addition to the predefined actions, we can also invoke arbitrary commands. The traditional way of doing this is with the -exec action.
-exec command {} ;
{} is a symbolic representation of the current pathname.
The xargs command performs an interesting function. It accepts input from standard input and converts it into an argument list for a specified command.
Unix-like systems allow embedded spaces (and even newlines) in filenames. This causes problems for programs such as xargs that construct argument list for other programs.
To overcome this, find and xargs allow the optional use of a null character argument separator.
find ~ -iname “*.jpg” –print0 | xargs –null ls-l
Note that unlike ls, find does not produce results in sorted order. Its order is determined by the layout of the layout of the storage device.
The stat command reveals all that the system understands about a file and its attributes.
Data compression is the process of removing redundancy from data.
The bzip2 program uses a different compression algorithm that achieves higher levels of compression at the cost of compression speed.
If you apply compression to file that is already compression, you will actually end up with a large file. This is because all compression techniques involves some overhead that is added to the file to describe the compression.
Archiving is the process of gathering up files and bundling them together into a single large file. Archiving is often done as a part of system backups.
The zip program is both a compression and an archiver.
Unfortunately, the unzip program, does not accept standard input . This prevents zip and unzip from being used together to perform network file copying like tar.
rsync option source destination
One of the real beauties of rsync is that it can be used to copy files over a network. After all, the “r” in rsync stands for “remote”.
The first way is with another system that with rsync installed, along with a remote shell program such as ssh.
The second way that rsync can used to synchronize files over a network is by using an rsync server.
In essence, grep searches text files for the occurrence of a specified regular expression and outputs any line containing a match to standard output.
In addition to literals, regular expressions may also include metacharacters that are used to specify more complex matches.
When we pass regular expressions containing metacharacters on the command line, it is vital that they be enclosed in quotes to prevent the shell from attemping to expand them.
Note that the regular expression ‘^$’ will match blank lines.
We can also match a single character from a specified set of characters by using bracket expressions.
A set may contain any number of characters, and metacharacters lose their special meaning when place with brackets. However, there are two cases in which metacharacters used within brackets. ^ indicate negation; - indicate character range.
The ^ only invokes negation if it is the first character within a bracket expression; otherwise, it loses its special meaning and becomes an ordinary character in the set.
With this setting, POXIS compliant applications will use a dictionary collation order rather than ASCII order. A character range of [A-Z] when interpreted in dictionary order include all of the alphabatic characters except the lowercase “a”.
To partially work around this problem, the POSIX standard includes a number of character classes which provide useful range of characters.
[:alnum:], [:word:], [:blank:], [cntrl]
The LANG variable contains the name of the language and character set used in your locale.
POSIX also splits regular expression implementation into two kinds: basic regular expression (BSE) and extended regular expression (ERE).
Whats the difference between BSE and ERE? Its a matter of metacharacters.
BRE: ^ $ [ ] *
ERE: ( ) { } ? + |
less and vim both share the same method of searching for text. However many of the characters are considered literals in basic expressions. They are only treated as metacharacters when escaped with a backslash.
The zgrep provides a front end for grep, allowing it to read compressed files.
一些命令
ps: simply report a snapshot of current processes
top: totally display Linux processes
bg: place a job in the background
fg: place a job in the foreground
kill: send a signal to a process
killall: kill process by name
shutdown: shutdown or reboot the system
printenv: print part or all of the environment
set: set shell options
export: export environ ment to subsequently executed programs
alias: create a alias for a command
mount: mount a file system
unmount: unmount a file system
fsck: check and repair a file system
fdisk: partition table manipulation
mkfs: create a file system
fdformat: format a floppy disk
dd: write block oriented data directly to a device
genisoimage(mkisofs): create an ISO 9660 image file
wodim(cdrecord): write data to optimal storage media
md5sum: create an MD5 checksum
ping: send an ICMP ECHO_REQUEST to network hosts
traceroute: print the route packets trace to a network network host
netstate: print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
ftp: Internet file transfer program
wget: non-interactive network downloader
ssh: Open SSH SSH client (remote login program)
locate: find files by name
find: search for files in a directories hierarchy
xargs: build and execute command lines from standard input
touch: change file times
stat: display file and file system status
gzip: compress or expand files
bzip2: A block sorting file compressor
tar: tape archiving utility
zip: package and compress files
rsync: remote file and directory synchronization