Windows-to-Linux roadmap: Part 2. Console crash course

Windows-to-Linux roadmap: Part 2. Console crash course

控制台速成

 

Any administrative task can be done from the console in Linux. In many cases, using the console is faster than using a graphical program and may provide additional functionality. Furthermore, any console task can be placed into a script, and thus automated. To really take control of your Linux environment, you will want to learn how to do things from the console. The information here is a guide to get you started at the Linux console if you have a DOS/Windows background.

 

在Linux中任何管理任务可以在控制台中完成。在许多情况下,使用控制台比使用一个图像化程序更快并且可能提供额外的功能。另外,任何控制台任务可以被放于一个脚本,因此而自动化。为了真正的控制你的Linux环境,你想要学习怎样从控制台去做事情。如果你有DOS/Windows背景。这里的信息是一个让你在Linux的控制台有个开始的引导。

 

Accessing a console

If your system boots into text mode (a common configuration for servers to conserve overhead for services), then you are already at a console when you execute a text login. On a typical Linux system, you can get to additional consoles by pressing Ctrl + Alt + (F1 - F6). Each console is a completely different session on the system and can be accessed as different users at the same time.

 

访问一个控制台

如果你的系统启动进入文本模式(一个服务器的普遍配置来为服务保留资源),当你执行一个文本登录那你已经在一个控制台中了,你可以获得额外的控制台通过按 Ctrl + Alt + (F1 - F6). 每个控制台是系统中完全不同的会话,可以被不同的用户在同一时间访问。

 

This multi-console behavior is different from the multiple-desktop in Windows. In Linux, each console can be controlled by a completely different user. For example, you can be logged as root on console 1, and logged in as joeuser on console 2. Both consoles run different programs in their own user space. In the same vein, different users can be logged into a Windows system remotely. In this instance, Linux provides capabilities more like a mainframe than a simple server or workstation.

If you are in a graphical mode, then you can open a terminal to get access to a console screen. The terminal will usually have a button on your desktop's task bar, or you can find it under System Tools in the Program menu. You can also open a terminal from the context menu (right click on the desktop).

这个多控制台行为时不同于windows中的多桌面的,在linux中,每个控制台可以完全的被一个不同的用户所控制。比如,你可以一root在控制台1登录,也可以以joeuser在控制台2登录。两个控制台在他们各自的空间运行不同的程序。相同的情况,不同的用户可以远程登录进入windows系统。在这个实例中,Linux提供了更多的像一个大型机的能力而不是一个简单的服务器或者工作站。
如果你在一个图像化模式中,那么你可以打开终端来得到访问控制台的窗口。这个终端经常在桌面的任务栏上有一个按钮,或者你可以在软件菜单的系统工具下找到,你也可以从上下文菜单(右键菜单)打开一个终端。(在桌面上右击)

Commands

There are many potential commands available from the console. Some of these tools are only truly useful when writing scripts. Here are some of the first ones that you'll probably need. Remember that all commands and options are case sensitive. -R is different from -r, and will probably do different things. Console commands are almost always lowercase.

命令

这有很多潜在的命令可以从控制台使用。有一些这样的工具只有当些脚本时菜真正有用。这有一些泥可能需要。记得所有的命令和脚本是大小写敏感的。-R和-r不同。可能做不同的事情。控制台命令几乎总是小写

cd
Moving around in directories uses the familiar cd command. The main trick is to remember that in Linux the forward-slash (/) is used where you are accustomed to using the back-slash (/). The back-slash is still used, but it specifies that a command should be continued on the next line. This is sometimes done for readability when typing in a particularly long command.

 

使用熟悉的cd命令在目录中移动,这个主要的窍门是记住 在linux中斜杠/被用在你已经习惯使用反斜杠的地方。反斜杠仍然在使用,但是它指定一个命令可能会持续到下一行。当输入一个特别长的命令,有时这样做是为了可读性

 

ls
Listing files in a directory can be done with the ls command. There are several switches you can use to alter the look of the listing:

目录中的文件清单可以使用ls命令来完成,这儿有一些你可以改变清单样子的开关

Listing files

ls -l

Shows a long listing, including files size, date and time, and attributes

显示一个长的列表,包括文件大小,日期与时间,所有的属性

ls -t

Sorts files by time

按照时间对文件排序

ls -S

Sorts files by size

按照文件大小对文件进行排序

ls -r

Combined with one of the sorting switches, reverses the order. ls -lt shows the files with the newest one at the top of the list. ls -lrt shows the files with the newest ones at the bottom.

和排序开关中的一个组合使用,翻转顺序。ls -lt以最新的一个在列表顶端的方式显示文件。ls -lrt以最新的一个在底部来显示文件

ls -h

Human readable. Uses friendly k, M, and G indicators to show file size rather than listing them in bytes.

可读性,使用友好的K,M和G指示符来显示文件大小而不是以字节的方式列出他们 

ls -a

Shows all the files in a directory, even the hidden ones

显示一个目录中的所有的文件,即使是隐藏的

cp
Copy files with the cp command. The command works essentially the same as the DOS copy command. Essential switches:

使用cp名来拷贝文件,这个命令本质上和dos中的copy命令一样。基本开关?

Copying files

cp -R

Copies files recursively; required if you are copying an entire directory

递归的拷贝文件;如果你在拷贝一整个目录需要

cp -f

Forces the copy and overwrites existing files without asking

强迫拷贝并不询问覆盖已经存在的文件

cp -l

Links files instead of copying; see below

链接文件而不是拷贝。看下面

Creating links with the copy command

The cp command can be used to create a quick set of hard links to a file, or to an entire file structure. Use the -l switch to indicate link copying. All directories will be created as directories, but all files will be set up as hard links.

cp -lR /data/accounting/payroll /data/management/hr

The above command will copy the entire directory structure from /data/accounting/payroll and below to /data/management/hr/payroll. All files in the directory structure will be set up as links. This can be used to provide different views of the same files within a file system. This is also a helpful security technique, allowing access to files from a different directory with different access controls.

 

cp命令可以被用来创建一个文件的快速硬链接集,或者一整个文件结构的。使用-l开怪来指示链接拷贝。所有的目录将被创建成目录,但是所有的文件会被设置为硬链接。

cp -lR /data/accounting/payroll/data/management/hr

以上的命令将拷贝整个目录结构从/data/accounting/payroll 和以下到 /data/management/hr/payroll/所有这个目录结构中的文件将被设置为链接。这个可以用来在一个文件系统里为相同的文件提供一个不同的视觉。这个也是个有帮助的安全技术,允许从一个不同的目录以不同的访问控制来访问文件。

 

mv
Move files and rename files with the mv command. It works essentially the same as the DOS move command, except that it will move entire directory structures as well as files.

 

使用mv命令移动文件并重命名文件。它和dos的move命令本质相同,除了他将移动整个目录结构和文件。

 

cat
View files with the cat command. This is the equivalent of the DOS type command. It will dump the contents of a file to another file, to the screen, or to another command. cat is short for concatenate, and can be used to sequence several files together into a larger file.

 

使用cat来查看文件,这是等同于dos的type命令。他将抽取一个文件的内容到另一个文件、屏幕或者另一个命令。cat是concatenate的缩写,也可以 被用来按顺序排放一些文件到一个大文件。

 

more
View information one page at a time with the more command. It works essentially the same as the DOS more command.

 

使用more命令查看一页信息。它和dos中的more命令本质相同

 

 

less
Use less to view a text file with the ability to scroll up and down through the document and search for text patterns.

使用less来查看一个文本文件,具有上下滚动和查找文本模式的功能。

vi
Some might say that vi stands for "virtually impossible." It is a text editor that has a long tradition in the Unix world. vi is not really intuitive, but it is available in almost any Unix-like environment. There is a built-in tutorial for the version installed in Linux, and once you get used to it, you can do some truly incredible things in a few keystrokes. Truly, no editor has managed to replace vi for editing password and configuration files.

 

一些可能说法是vi事实上不可替代。他是一个在unix世界有很长传统的文本编辑器。vi不是真正的???,但是几乎在任何一个像unix环境都可以使用。在linux中内建了一个指导。一旦你开始使用它,你可以通过敲击键盘做一些真正的不可思议的事情,真正地,没有编辑器可以管理替换vi来编辑密码和配置文件。

 

man
View documentation for a command with the man command. Man is short for manual. Documentation tends to be thorough. To learn more about man, type:

man man

查看文档

info
info is like man except it provides hyperlinked text to make browsing documentation easier.

info像man命令,除了它提供超链接文本使得文档浏览更容易。

Which shell?

One critical difference between DOS/Windows and Linux is that the command shell is a layer separated from the operating system. The shell environment affects the features you have, such as editable command lines and scrolling histories. The shell also determines the syntax required to do functions in scripts. In DOS/Windows, there was only one option for scripting, the lowly .BAT file. It did a lot, but required a good deal of creativity on the part of the script writer to do more than basic tasks. In Linux, scripts can contain loops and do more than basic conditional statements, including many things that you expect from a programming language. If you were good at writing .BAT files, shell scripts are going to let you shine.

The default shell is a parameter in each user account. The typical default shell in Linux is /bin/bash, though others are available. The man documentation for each shell is actually very good and goes into detail about shells and how they work. Rather than try to paraphrase that information here, select a shell from the list below and look at its man page.

bash
The bash shell is a free version of the Bourne shell, the first Unix shell, and includes many additional features. Bash has editable command lines, a scrollable command history, and tab completion to help avoid typing long file names.

csh
The C shell uses a "C-like" syntax and has borrowed many features from the Bourne shell, but uses a different set of internal shell commands.

ksh
The Korn shell uses the same syntax as the Bourne shell and has included the user-friendly features of the C shell. ksh is used in many installation scripts and should probably be installed on the system even if it's not your primary shell.

tcsh
The TC shell is an enhanced version of the C shell and is 100% compatible with it.

zsh
The Z shell is an enhanced version of the Korn shell with many features found in the bash shell.

Shell game

You can change your shell at any time by simply executing it from the console. A script can specify the shell it wants to run by putting a shebang (#!) at the top of the file pointing to the desired shell. When the script is executed, it will run in the correct shell, but leave the user's shell environment alone. Here's an example of a line to get a script to run in C shell:

#!/bin/csh

Link me up, Scotty!

One compelling feature in the Linux file system is the file link. A link is a reference to a file, so that you can let files be seen in multiple locations of the file system. However, in Linux, a link can be treated as the original file. A link can be executed, edited, and accessed without having to do anything unusual. As far as other applications on the system are concerned, a link is the original file. When you make edits to a file through the link, you are editing the original. A link is not a copy. There are two kinds of links: a hard link and a symbolic link.

A hard link can only reference files in the same file system. It provides a reference to the file's physical index (also called an inode) in the file's system. Hard links do not break when you move the original file around because they all point to the file's physical data rather than its location in the file structure. A hard-linked file does not require the user to have access rights to the original file and does not show the location of the original, so it has some security advantages. If you delete a file that has been hard linked, the file remains until all references have been deleted as well.

A symbolic link is a pointer to a file's location in the file system. Symbolic links can span file systems and can even point to files in a remote file system. A symbolic link shows the location of the original file and requires a user to have access rights to the original file's location in order to use the link. If the original file is deleted, all of the symbolic links become broken. They will point to a non-existent location in the file system.

Both types of links can be made with the command ln . By default ln will make a hard link. The -s switch will make a symbolic link.

# Create a hard link from MyFile in the current
# directory to /YourDir/MyFile
ln MyFile /YourDir

# Create a symbolic (soft) link from MyFile in
# the current directory to /YourDir/YourFile
ln -s MyFile /YourDir/Yourfile

In the above examples, MyFile, /YourDir/MyFile, and /YourDir/Yourfile are all treated as the same file.


Coming out of your shell

Learning to work from the console is a necessary skill for Linux administration. There are tools to avoid the console, but you will always be more limited by what you can do through a tool. Accessing a console is easy, and accessing command documentation is easy too with the man and info commands.

你可能感兴趣的:(Windows-to-Linux roadmap: Part 2. Console crash course)