Ubuntu

本章节关于 The Linux Command Line 读书笔记,语言通俗易懂,适合入门,并且图书是开源性质

Introduce

Freedom is the power to decide what your computer does, and the only way to have this freedom is to know what your computer is doing.Freedom is a computer that is without secrets, one where everything can be known if you care enought to find out.

It's been said that "graphical user interfaces make easy tasks easy, while command line interfaces make difficult tasks possible" and this is still very true today.

Linux is not just a piece of software; it's also a small parg of the larger Unix culture, which has its own languuage and history.

This book is divided into four parts, each covering some aspect of the command line experience:

  • Part 1 - Learning The Shell starts our exploration of the basic language of the command line including such things as the structure of commands, file system naviagtion, command line editing, and finding help and documentation for commands.

  • Part 2 - Configuration And The Environment covers editing configuration files that control the computers oparation from the command line.

  • Part 3 - Command Tasks and Essential Tools explores many of the ordinary tasks that are commonly performed form the command line. Unix-like operating systems, such as Linux, contain many "classic" command line programs that are used to perform powerful operations on data.

  • Part 4 - Writing Shell Scripts introduces shell programming, an admittedly rudimentary, but easy to learn, technique for automating many common computeing tasks. By learning shell programming, you will become familiar with concepts that canbe applied to many other programming languages.

Technically speaking, Linux is the name of the operating systems's kernel, nothing more.

This book is an ongoing project, like many open source software projects.

快速识别整段英文信息:形容词的中心词 + 动词结构 + 形容词的宾语结构,说明性介词结构

Part 1 - Learning the Shell

1 - What Is the Shell?

The shell is a program that takes keyboard commands and passes them to the operating system to carry tou.

  • date

  • cal to display a calendar of the curruent month

  • df to see the current amount of free space on our disk drives

  • free to display the amount of free memory

介词定位术

  • on our Linux system

  • at the prompt

  • in this chapter

2 - Navigation

  • pwd - Print name of current working directory

  • cd - Change directory

    • cd Changes the working directory to your home directory

    • cd - Changes the working directory to the previous working directory

    • cd ~username

  • ls - List directory contents

3 - Exploring the System

  • ls get a list of files and subdirectories contained in the current working directory

  • ls /usr specify the directory to list

  • ls ~ /usr specify multiple direcotories

  • ls -l change the format of the output to reveal more detail

Options and Arguments

This brings us to a very important point about how most commands work. Commands are often followed by one or more options that modify their behavior, and further, by one or more arguments, the items upon which the command acts. So most commands look kind of like this:

the ls command has a large of possible options. The most common are listed below.

A Longer Look at Long Format

Determing a File's Type with file

View File Contents with less - Less Is More

less /etc/passwd to examine the file that defines all teh sysmtem's user accounts.

The less program was designed as an improved replacement of an earlier Unix program call more.

英文名词 an improved replacement < -- > 中文动词 提高替换

Taking a Guided Tour

命令行界面:双击选中,点击滑轮粘贴

/ Where everything begins.

/bin Contains binaries(programs) that must be present for the system to boot and run.

/boot the Linux kernal, initail RAM disk image, and the boot loader.

  • /boot/grub/grub.conf

  • /boot/vmlinuz

/dev device nodes, "Everything is a file"

  • /etc/passwd

  • /etc/crontab

/home

/lib similiar to DLLS

/lost+found for recovery

/media` on modern Linux system, for removable media

/mnt on older Linux system, for removable media

/opt to hold commercial software products

/proc a virtual file system, will give us a picture of how the kernel sees the computer

  • /proc/cpuinfo

/root the home directory for the root account

/sbin vital system tasks for the superuser

/tmp

/usr the directory is likely the largest one

/usr/bin installed by the Linux distribution, hold thousands of programs

/usr/lib

/usr/local

/usr/sbin system administration programs

  • netplan

/usr/share

/usr/share/doc

/var data that is likely to change is stored, such as databases, spool files, user mail.

/var/log log files

In most Unix-like systems it is possible to have a file referenced by multiple names.

  • foo -> foo.2.7

  • libc.so.6 -> libc.2.6.so

4 - Manipulating Files and Directories

  • cp - Copy files and directories

  • mv - Move/rename files and directories

  • mkdir - Create directories

  • rm - Remove files and directories

  • ln - Create hard and symblic links

These five commands are among the most frequently user Linux commands.

Wildcards

Since the shell uses filenames so much, it provides special charaters to help us rapidly specify groups of filenames. These special charaters are called wildcards.

Wildcards can be used with any command that accepts filenames as arguments.

mkdir - Create Directories

mkdir direcotory...

When three periods follow an argument in the description of a command(as above), it means that the argument can be repeated.

rm的options

这节的小练习,多敲几遍

ln -s item symbolic-link

5 - Working with Commands

We will attempt to remove mysterious options and arguments and even create our own commands.

What Exactly Are Commands?

  • an executable program

  • a command built into the shell itself

  • a shell function, related with the environment

  • an alias

type - Display a Command's Type

是 shell builtin, executable program 还是 alias,拉出来看看

which - Display an Executable's Location only works for executable programs

man - Disaplay a Program's Manual Page

whatis - Display One-Line Manual Page Descriptions

help - Get Help for Shell Builtins

Creating Our Own Commands with alias

  • alias foo='cd /usr; ls; cd -'

  • unalias foo

  • alias

6 - Redirection

In this lesson we are going to unleash what may be the coolest feature of the command line. It's called I/O redirection. I/O redirection allows us to change where output goes and where input comes from. Normally, output goes to the screen and input comes form the keyboard, but with I/O redirection, we can change that.

Standard Input, Output, and Error

Many of the programs produce output of some kind.

  • results

  • status and error messages

按照一切都是文件的规则,程序会把正常的输出值 to a special file called standard output and 错误状态信息 to another file called standard error. In addition, many programs take input from a facility called standard input, which is, by default, attached to the keyboard.

Redirecting Standard Input

cat - Concatenate Files

  • The cat command reads one or more files and copies them to standard output.

  • We can use it to display files without paging.

  • Cat is often used to display shourt text files.

Pipelines

Using the pipe operator | (vertical lar), the standard output of one command can be piped into the standard input of another.

7 - Seeing the World as the Shell Sees It

Expansion

Each time we type a command and press the Enter key, bash perform serveral substituions(replacement) upon the text before it carries out our command.

echo - It prints its text arguments on standard output.

8 - Advanced Keyboard Tricks

Command Line Editing

bash uses a library (a shared collection of routines that different programs can use) called Readline to implement command line editing.

Completion - Tab

Using History

the hidden file .bash_history in our home directory kept the list of history commands.

9 - Permissions

Operating systems in the Unix tradition differ from those in the MS-DOS tradition in that they are not only multitasking systems, but also multi-user systems.

多人同时登录必须保证各自操作的安全,不能随意操作别人的文件,不能破坏电脑

Owners, Group Members, and Everybody Else

Reading, Writing, and Executing

chmod - Change File Mode

文件和文件夹略有不同

示例说明

sudo - Execute a Command as Another User

To authenticating using sudo, requires the user's own password.

passwd - Changing Your Password

10 - Processes

Processes are how Linux organizes the different programs waiting for their turn at the CPU.

How a Process Works

init --> init scripts --> system services, namely daemon programs, which just sit in the background

The kernel maintains information about each process to help keep things organized.

View Processes

Part 2 - Configuration and the Environment

11 - The Environment

As we discussed earlier, the shell maintains a body of information during our shell session called the environment.

What is Stored in the Environment?

  • environment variables

  • shell variables, and some programmatic data, namely aliases and shell funtions

How Is The Environment Established?

When we log on to the system, the bash program starts, and reads a serias of configuration scripts called startup files, which define the default environment shared by all users.

  • a login shell session, a virtual console session, username and password

  • a non-login shell session, in the GUI

Modifying the Environment

Since we know where the startup files are and what they contain, we can modify them to customize our environment.

英文当中代词多,因为用的多

Activating Our Changes

The changes we have made to our .bashrc will not take effect until we close our terminal session and start a new one because the .bashrc file is only read at the beginning of a session.

source ~/.bashrc

12 - A Gentle Introduction to vi

Why We Should Learn vi

  • vi is almost always available.

  • vi is lightweiht and fast.

  • We don't want other Linux and Unix users to think we are cowards.

13 - Customizing the Prompt

The Prompt is defined by an environment variable named PS1(short for "prompt string 1")

Adding Color

Most terminal emulator programs respond to certain non-printing character sequences to control such things as character attributes(such as color, bold text, and the dreaded blinking text) adn cursor position.

Part 3 - Common Tasks and Essential Tools

14 - Package Management

Most distributions fall into one of two camps of packaging technologies: the Debian .deb camp and the Red Hat .rmp camp

If a package requires a shared resource such as a shared library, it is said to have a dependency.

Package management systems usually consist of two types of tools.

  • Low-level tools which handles tasks such as installing and removing package files

  • High-level tools that perform metadata searching and dependency resolution

Finding a Package in a Repository

Installing a Package from a Repository

Installing a Package from a Package File

If a package file has been downloaded from a source other than a repository, it can be installed directly(though without dependency resolution) using a low-level tool.

Removing a Package

Updating Packages from a Repository

Updating a Package from a Package File

Listing Installed Packages

Determining Whether a Package is Installed

display information about an installed package

dpkg --status emacs or sudo apt-cache show tmux

Finding Which Package Installed a File

16 - Networking

ping - Send an ICMP ECHO_REQUEST to network hosts

Most network devices recving this packet will reply to it, allowing the network connection to be verified.

performance statistics

traceroute - Print the route packets trace to a network host

ip - Show/manipulate routing, devices, policy routing and tunnels

The ip program is a multi-purpose network configuration tool that makes use of the full range networking of features available in mordern Linux kernels.

ip [OPTIONS] OBJECT { COMMAND | help } 如 ip address

The first, called lo, is the loopback interface, a virtual interface that the system uses to "talk to itself" and the second, called eth0, is the Ethernet interface.

17 - Search for Files

locate - Find Files the Easy Way

  • updatedb 定时任务统计文件名

find - Find Files the Hard Way

18 - Archiving and Backup

Throughout the history of computing, there has been a struggle to get the most data into the smallest available space, whether that space be memory, storage devices, or network bandwidth.

compression algorithoms: lossless and lossy

gzip - compress one or more files 会把源文件替换成压缩版本

  • 无压缩空间的的文件,避免再次压缩,文件会变大,因会增加额外信息 gzip picture.zip

tar - short for tape archive 打包与解包

  • .tar for a 'plain' tar archive

  • .tgz for a gzipped archive

tar --help | less

  • 其中 f 指定文件 archive.tar

  • c 关联 create

  • x 关联 extract

  • a slightly odd way of expressing options, 可不加 leading dash

zip - both a compression tool and an archiver

rsync - synchronizing files and directories

23 - Compiling Programs

Why compile software?

  • Availability. Linux 发布版包括了一些常用软件,但是不是全部. In some case, need to compiling

  • Timeliness. 尝试新版本

What is Compiling?

Simply put, compiling is the process of translating source code (the human-readable description of a program written by a programmer) into the native language of the computer's processor.

  • machine language

  • assembly language --> assembler --> machine language

  • high-level programming language --> compiler --> 有些会编译成 汇编语言

  • linking, libraries, executable program file

  • A program called linker is used to form the connections between the output of the compiler and the libraries that the compiled program requires.

Are All Programs Compiled?

  • 编译型语言

  • 解释性语言

Compiling a C Program

  • The file Makefile describes the relationships and dependencies among the components that comprise the finished program.

  • 重要文件 可研究 README INSTALL

Part 4 - Writing Shell Scripts

24 - Writing Your First Script

What are Shell Scripts?

In the simplest terms, a shell script is a file containing a series of commands. The shell reads this file and carries the commands as though they have been entered directly on the command line.

How to Write a Shell Script

  • Write a script

  • Make the script executable

  • Put the script somewhere the shell can find it

编写脚本 vim hello

  • 第一行 The #! charater sequence is, in fact, a special constuct called a shebang. The shebang is used to tell the kernel the name of the interpreter that should be used to execute the script that follows.

  • 第二行 # 注释

  • 第三行 同命令行模式下 echo 'Hello World!'

修改为可执行,并调整执行路径

27 - Flow Control: Branching with if

The if statement has the following syntax:

Exit Status

Commands(including the scripts and shell functions we write) issue a value to the system when they terminate, called an exit status.This value, which is an integer in the range of 0 to 255, indicates the success of failure of the command's execution. By convention, a value of zero indicates success and any other value indicates failure. The shell provides a parameter that we can use to examine the exit status. Here we see it in action:

test

By far, the command used most frequently with if is test. The test command performs a variety of checks and comparisions. It has two equvalent forms. The first, shown here:

test expression

And the second, more popular form, shown here:

[ expression ]

File Expressions

  • "$FILE" 注意加引号

  • exit defaults to the exit status of the last command executed

String Expressions

Integer Expressions

A More Modern Version of test

Modern versions of bash include a compound command that acts an enhanced replacement for test.

It uses the following syntax:

[[ expression ]]

  • 兼容 [ expressoin ]

  • 支持正则 string1 =~ regex

(( )) - Designed for Integers

28 - Reading Keyboard Input

read - Read Values from Standard Input

29 - Flow Control: Looping with while/until

while

Part 5 - Practice

设置时区

Last updated