Chinaunix首页 | 论坛 | 博客
  • 博客访问: 717624
  • 博文数量: 235
  • 博客积分: 4309
  • 博客等级: 中校
  • 技术积分: 2325
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-17 11:25
个人简介

If you don\\\\\\\\\\\\\\\'t wanna do it, you find an EXCUSE; if you do, you\\\\\\\\\\\\\\\'ll find a WAY :-)

文章分类

全部博文(235)

文章存档

2014年(3)

2013年(2)

2012年(31)

2011年(199)

分类: LINUX

2011-02-17 18:24:37

一:修改sudoers配置文件

很多命令需要root权限, 启用sudo 避免繁琐的切换root(将用户添加到管理员组 同时禁用root 保证安全性)

1.配置 sudo  编辑 sudoers 文件( vi /etc/sudoers)
移到这一行下面:
root    ALL=(ALL)       ALL
添加 
username  ALL=(ALL)      !/usr/bin/passwd, /usr/bin/passwd [A-Za-z]*, \ !/usr/bin/passwd root(替换 username 为你登陆时使用的用户名)

如果你想每次使用 sudo 命令的时候都提示你输入根密码,把下面的#去掉:(#是注释)
# %wheel        ALL=(ALL)       ALL

如果你不想每次使用 sudo 命令的时候都提示你输入跟密码,把下面的#去掉:
# %wheel        ALL=(ALL)       NOPASSWD: ALL

接着按 :wq 键保存退出。TIPS:修改只读文件 保存并退出 (:wq!)即可!

2.添加你的用户名到 wheel 组

[user@rhel5 ~]$ su -c 'gpasswd -a username wheel'   (
-c command:(变为新用户,执行一条“命令”后再变回原用户)

3.测试你是否正确配置了 sudo ,只需要在普通用户权限下输入

[user@rhel5 ~]# sudo whoami →→ 如果一切正常,命令会返回 “root” 这个字。

PS:因为出于安全性考虑 ;Fedora/RHEL/CENTOS 执行命令都需要键入完整路径
Attention:键入完整路径执行命令,防止已经流入搜索路径下的su等程序 企图获得口令的可能!

因此我们看到了足够的安全性,redhat、centos、fedora。(或许这给您带来了麻烦!)

——————————————————————————————————————

二:常用技巧

[root@rhel5 ~]# nautilus Desktop →→ 使用图形管理器打开文件夹

[root@rhel5 ~]# lsb_release -a →→ 首先来看看当前系统的版本

蓝色代表目录
绿色代表可执行文件
红色表示压缩文件
浅蓝色表示链接文件
灰色表示其他文件
红色闪烁表示链接的文件有问题了
黄色表示设备文件

命令的组成:Command [options] [arguments]      /*命令的组成包裹三部分*/

 The Seven Fundamental Filetypes (基本的 7 种档案类型)
-         regular file
d        directory
l         symbolic link
b        block special file
c        character special file
p        named pipe
s        socket

——————————————————————————————————————

【Don't try to memorize everything!】

1.whatis

[root@rhel5 ~]#whatis ls  →→ 查询到指令的简短描述说明
ls                 list directory contents

2.--help
[root@rhel5 ~]#date --help  →→ 查询到指令的说明摘要和参数列表

TIPS:少数指令不支持 "--help",可用man指令
"[ ]"are optional 
"<>"are variables
 "x|y|z" x or y or z 可不使用,否则选其一
-abc mean "any mix of -a,-b or -c" //-a or -b or -c、-ab or -bc or -ac、-abc


3.man

man [] (号称:man page)
Manual sections:
1 User commands  4 Special files 7 Miscellaneous
2 System calls        5 File formats  8 Administrative commands 
3 Library calls        6 Games

[root@rhel5 ~]# man passwd →→ 默认进入Chapter 1
[root@rhel5 ~]# man -k passwd  →→ 找到符合关键字的指南
TIPS:
/  Search for text
n/N        Next/Previous match
q            Quit viewing page

info (号称:info page,比 man 提供更加详细的说明。)

4.info
[root@rhel5 ~]#info ls

TIPS:
Tab        Move the next link
Enter     Follow link the cursor is on
n/p/u      View next/previous/up-one node//切换小节节点
s[] Search for text
q            Quit viewing page
[root@rhel5 ~]#ls /usr/share/doc/*查看说明文档*/

——————————————————————————————————————

Some Important Directories】:
/root、/home/
/bin、/usr/bin、/usr/local/bin
/sbin、/usr/sbin、/usr/local/sbin
/media  and /mnt 
/media/floppy、/media/cdrom


【Other Important Directories】:
/etc         -System config files
/tmp        -Temporary files
/boot       -Kernel and bootloader
/dev        -Device
/usr         -Programs
/lost + found
/var and /srv    -Server data 
/proc            -System information


【The lib Directories】:
/lib、/usr/lib、/usr/local/lib


【Understanding the Red Hat Linux File System Structure】:
According to the FHS, the / directory must contain, or have
links to, the following directories:


bin — This directory contains command files for use by the
system administrator or other users. The bin directory can
not contain subdirectories.


boot — On Red Hat systems, this is the directory
containing the kernel, the core of the operating system. Also
in this directory are files related to booting the system, such
as the bootloader.


dev — This directory contains files with information about
devices, either hardware or software devices, on the system.


etc — This directory and its subdirectories contain most of
the system configuration files. If you have the X Window
System installed on your system, the X11 subdirectory is
located here. Networking related files are in the subdirectory
sysconfig.
Another subdirectory of etc is the skel directory, which is
used to create files in users’ home directories when the users
are created.


home — This directory contains the directories of users on
the system. Subdirectories of home will be named for the
user to whom they belong.


lib — The shared system files and kernel modules are
contained in this directory and its subdirectories.


mnt — This directory is the location of the mount point for
temporary file systems, such as a floppy or CD.


opt — This directory and its subdirectories are often used
to hold applications installed on the system.


proc — Information about system processes is included in
this directory.


root — This is the home directory of the root user. Don’t
confuse this with the / directory, which has the same name.


sbin — Contained in this directory are system binaries
used by the system administrator or the root user.


tmp — This directory contains temporary files used by the
system.


usr — This directory is often mounted on its own partition.
It contains shareable, read-only data. Subdirectories can be
used for applications,
typically under /usr/local.


var — Subdirectories and files under var contain variable
information, such as system logs and print queues


【Some important /proc files are】:
/proc/cpuinfo — contains info about the CPU
/proc/interrupts — tells you what interrupts are in use
/proc/scsi — tells you what kind of SCSI adapter is installed
/proc/parport — contains info about the parallel ports on
your system
/proc/tty — contains info about ttys that are available and in
use
/proc/apm — contains advanced power management
information
/proc/bus — a directory that contains bus-specific
information
/proc/devices — lists available character and block devices
/proc/dma — lists used DMS channels
/proc/filesystems — lists supported file systems
/proc/fs — contains file system parameters
/proc/ide — directory that contains information about the IDE
subsystem
/proc/ioports — contains information about system I/O port
usage
/proc/modules — contains a list of currently loaded modules
/proc/net — contains networking information
/proc/uptime — contains the system uptime
/proc/version — contains the system version

——————————————————————————————————————

【Absolute Pathnames && Relative Pathnames】
/home/user2                     ../user2

1.cd
[root@rhel5 ~]# cd /root/doc  →→ 绝对路径
[root@rhel5 ~]# cd     →→ 返回用户主目录
[root@rhel5 ~]# cd ..  →→ 返回上一集目录*/
[root@rhel5
 ~]# cd -   →→ 返回上一次使用的目录*/

2.ls

[root@rhel5 ~]#ls -l | lpr  →→ 传给打印机 打印出来
[root@rhel5 ~]# ls -a  →→ 以 "." 开头的档案或目录是隐含目录
[root@rhel5 ~]# ls -A  →→ 显示除"."和". ."外的所有文件
[root@rhel5 ~]# ls -l /usr/  →→ 查看 "/usr目录内" 详细参数

[root@rhel5 ~]# ls -ld /usr →→ 查看该目录本身的属性,不包括其中的文件信息

[root@rhel5 ~]# ls -lF  →→ 分类显示(--classify)

[root@rhel5 ~]# ls -lS  →→ sort by file size

[root@rhel5 ~]# ls -lt  →→ sort by modification time

[root@rhel5 ~]# ls -li →→ 显示inode

[root@rhel5 ~]# ls -lh  →→ print size in human readable

 

3.cp
[root@rhel5 ~]# cp redhat.txt ./Dktop  →→ 复制文件到指定路径
[root@rhel5 ~]# cp -r ubuntu ./Desktop  →→ 复制目录需要加上 "-r"
[root@rhel5 ~]# cp -p linux.txt ./Desktop  →→复制文件并保存原有时间戳记

[root@rhel5 ~]# cp -a ubuntu ./Desktop  →→ 复制目录并保存原有时间戳记,使用 "-a" 代替 "-r -p"

[root@rhel5 ~]# cp 2009 2010 ./Desktop →→ 复制多个文件

4.mv
[root@rhel5 ~]# mv 2009 doc/2010 →→ 重命名目录或文件
[root@rhel5 ~]# mv  2009 doc/2010 →→ 移动并重命名文件
[root@rhel5 ~]# mv doc/2009 ./Desktop →→移动文件
[root@rhel5 ~]# mv doc/2009 doc/2010 ./Desktop →→移动多个文件

5.rm

[root@rhel5 ~]# rm -f doc/2009 →→删除档案 加"-f" 可以免去询问提示
[root@rhel5 ~]# rm -r ubuntu  →→删除目录 加 "-r"

6.mkdir / rmdir

[root@rhel5 ~]# mkdir doc/dir1 →→创建目录

[root@rhel5 ~]# rmdir doc/dir1 →→只能用于删除空目录,删除非空目录使用 "rm  -r"

7.File Globbing (文件通配符)
*             -matches zero or more characters
?            -matches any single character
[a-z]      -matches a range of characters
[!a-z]    -matches all except the range
Tab       -可用于补齐指令(敲击两次列出所有相关指令)

8.file

[root@rhel5 ~]# file doc/doc1 →→ 查看文件是哪种类型的文件

9.history

[root@rhel5 ~]# history →→查看以前执行过的指令,包括执行的序号 + 指令
185 clear
186 cd ..
187 history
188 passwd --status user2
[root@rhel5 ~]# !188 →→执行188 执行过的指令

[root@rhel5 ~]# ^2^1 →→将上个指令的 "2" 改为 "1"

[root@rhel5 ~]# ping 192.168.0.196 →→ping指令

[root@rhel5 ~]# ^196^1 →→将上个指令的 "196" 改为 "1"

[root@rhel5 ~]# ping 192.168.0.1

 

10.touch

[root@rhel5 ~]# touch doc/test1.txt →→创建文件
[root@rhel5 ~]# touch {a,b} →→ "{}" 用于排列组合

-rw-r--r-- 1 root root 0 Oct 01 17:16 a
-rw-r--r-- 1 root root 0 Oct 01 17:16 b

[root@rhel5 ~]# touch a{a,b} →→ "{}" 用于排列组合
-rw-r--r--1 root root 0 Oct 01 17:16 a
-rw-r--r--1 root root 0 Oct 01 17:16 aa
-rw-r--r--1 root root 0 Oct 01 17:16 ab
-rw-r--r--1 root root 0 Oct 01 17:16 b

[root@rhel5 ~]# touch {a,b}.{1,2} →→ "{}" 用于排列组合
-rw-r--r--1 root root 0 Oct 01 17:17 a.1
-rw-r--r--1 root root 0 Oct 01 17:17 a.2
-rw-r--r--1 root root 0 Oct 01 17:17 b.1
-rw-r--r--1 root root 0 Oct 01 17:17 b.2

11.hostname

[root@rhel5 ~]# hostname →→显示本机名称

11.1修改network配置文件

[root@rhel5 ~]# vi /etc/sysconfig/network

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=rhel5 →→修改主机名称reboot 生效

12.echo & backquotes
[root@rhel5 ~]# echo "Hostname:" →→回显字符串*/
[root@rhel5 ~]# echo "Hostname:`hostname`" →→ 倒引号" ` "倒引号用于包裹指令以优先执行 
[root@rhel5 ~]# echo "Hostname:$(hostname)" →→ 倒引号" ` " 和"$()" 实现同样的效果 
Hostname:localhost.localdomain

12.1【Variables 进行数学运算】
[root@rhel5 ~]# a=3 →→ 设定变量的值 例如:" a = 3 "即可
[root@rhel5 ~]# echo $a →→显示出变量 "a" 的值
[root@rhel5 ~]# b=5 →→ 设定变量的值 
[root@rhel5 ~]# echo $[ $a + $b] →→进行加法计算
[root@rhel5 ~]# echo $[ $a -  $b] →→进行减法计算
[root@rhel5 ~]# echo $[ $a * $b] →→进行乘法计算
[root@rhel5 ~]# echo $[ $a / $b] →→进行除法计算
[root@rhel5 ~]# echo $[ $a % $b] →→进行取余计算
[root@rhel5 ~]# echo $[ $a ** $b] →→进行平方计算

12.2.backslash
[root@rhel5 ~]# echo Your cost is \$5.00 →→ backslash("\" 反斜杠) 进行转译 
[root@rhel5 ~]# ls \  →→ 命令最后跟 "\"  
> -l  →→如果输入的指令很长就可以使用 "\" 以便可以接着输入指令

12.3.Double Quotes 、Single Quotes、backslash

[root@rhel5 ~]# echo \*\* 00000 \*\*  →→ 进行转译显示出来 
[root@rhel5 ~]# echo "** 00000 **"  →→ 使用双引号 (Double Quotes)
[root@rhel5 ~]# echo '** 00000 **'  →→ 使用单引号 (Single Quotes)
TIPS:
Single Quotes inhibit all expansion(可以禁止所有字元功能)
Double Quotes inhibit all expansion except:
$(dollar sign)
\(backslash)
`(backquotes)
!(exclamation point)

 【echo 命令】:
echo是一个非常简单、直接的linux命令:
    *将argument送出至标准输出(STDOUT),通常就是在显示器(monitor)上输出。
 
为了更好的理解,不如先让我们跑一下 echo 命令好了:
 
$ echo

$

你会发现只有一个空白行,然后又回到 shell prompt 上了。这是因为 echo 在预设上,在显示完argument之后,还会送出一个换行符号(new-line charactor)。上面的命令没有任何的argument,结果就只剩下一个换行符号了。若要取消这个换行符号,可以利用echo的 -n option
 
$ echo -n
$
 
不妨让我们回到 command line 的概念上来讨论上例的 echo 命令好了:
    command line 只有 command-name(echo)以及option(-n),并没有任何argument。要想看看 echo的argument ,那还不简单!接下来,可以试试如下的输入:

$ echo first line
first line
$ echo -n first line
first line $
 
在上两个 echo 命令中,你会发现 argument 的部分显示在你的屏幕,而换行符号则视 -n option的有无而别。很明显的,第二个echo由于换行符号被取消了,接下来的shell prompt就接在输出结果同一行了。
事实上,echo除了 -n options 之外,常用选项还有:
    -e:启用反斜线控制字符的转换
    -E:关闭反斜线控制字符的转换(预设如此)
    -n:取消行末之换行符号(与 -e 选项下的 \c 字符同意)
 
關於 echo 命令所支援的反斜線控制字符如下表:
        \a:ALERT / BELL (从系统喇叭发送出声音)
        \b:BACKSPACE ,也就是向左刪除
        \c:取消行末的行换符号
        \E:ESCAPE,逃离键
        \f:FORMFEED,换页字符
        \n:NEWLINE,換行字符
        \r:RETURN,回车键
        \t:TAB,表格跳位鍵
        \v:VERTICAL TAB,垂直表格跳位鍵
        \n:ASCII 八进制编码(以 x 为十六进制)
        \\:反斜线本身
        
或许,我们可以透过实例来了解 echo 的选项及控制字符:

例一:
$ echo -e "a\tb\tc\nd\te\tf"
a       b       c
d       e       f
 
上例运用 \t 来区隔 abc 还有 def ,以及用 \n 将 def 换至下一行。

例二:
$ echo -e "\141\011\142\011\143\012\144\011\145\011\146"
a       b       c
d       e       f

与例一的结果一样,只是使用了八进制编码来表示。

例三:
$ echo -e "\x61\x09\x62\x09\x63\x0a\x64\x09\x65\x09\x66"
a       b       c
d       e       f
 
与例二差不多,只是换用了十六进制编码。


例四:
$ echo -ne "a\tb\tc\nd\te\bf\a"
a       b       c
d       f $
 
因为 e 字母后面是删除键(\b),因此输入结果就没有e了。
在结束时听到一声铃响,那是\a的杰作!
由于同时使用了 -n 选项,因此 shell prompt 紧接在第二行之后。
若不用 -n 的话,那在\a后再加个\c,也是同样的效果。
 
事实上,在日后的 shell 操作及 shell script 设计上,echo 命令是最常用的命令之一。
比方说,用 echo 来检查变量值:
$ A=B
$ echo $A
B
$ echo $?
0

13.命令补全和历史命令搜索功能

[root@rhel5 ~]# CTRL+R →→进行历史指令的搜寻功能,非常方便。
使用Esc-. (先按Esc键,再按 .键)或者Alt+.(Alt 键和.键一齐按下) 进行最后一个参数的补全功能
例如:
[root@rhel5 ~]# ping 192.168.0.196 →→
[root@rhel5 ~]# telnet  →→这个时候使用Esc-. 或者Alt+. 就可以在后面补上"192.168.0.196"


14.Gnome Terminal
           -Open a new tab
   -Next/Prev tab
                -Change to go to "N" tab(指定切换到哪个Terminal 例如:Alt+3)
    -Copy/Paste
      -Close a tab(关闭当前Terminal)

15.【切换指令编辑模式】
[root@rhel5 ~]# set -o →→ 查看指令编辑模式 
[root@rhel5 ~]# set -o vi →→可以切换到vi编辑模式,"-"表示disable "o"表示off 
[root@rhel5 ~]# set +o vi →→ "+"表示enable "o"表示off 

16.Pipes " | "

将处理完的程式导入到另一个程式继续做处理
[root@rhel5 ~]#ls -l /etc | less →→ " | " 将一个指令的输出变成另一个指令的输入

17.sort 
[root@rhel5 ~]#sort -ruf sort.1

-r        -倒序排列
-f        -忽略大小写
-u       -去掉重复的
-t        -设置间隔符号
-n       -使用数值进行排列
-k       -设定按照哪一栏做排列
-k       -设定两栏,当第一栏有重复时按照第二栏做排列

[root@rhel5 ~]#cat /etc/passwd →→ 该文件都是以冒号 ":"进行间隔。
[root@rhel5 ~]#sort -t: -k3 /etc/passwd →→以第三行做排列
[root@rhel5 ~]#sort -t: -k3 -n /etc/passwd →→以第三行做排列,并使用"-n" 以数字进行排列。

18.cut

有选择的查看文件
[root@rhel5 ~]#cut -f3 linux.tab →→ 显示第三列 
[root@rhel5 ~]#cut -f2 -d, cut.csv →→ 显示第二列并以","作为间隔符号 
[root@rhel5 ~]#cut -c4-8 cut.csv →→显示每行的第 4个字源到第8个字源的资料

[root@rhel5 ~]#cut -f1 -d: /etc/passwd | sort -r | less  →→ TIPS:
将 cut 出来的结果 导到 sort指令做排序 接着 导到 less 指令 一页一页的显示出来。

19.Paste & cat
[root@rhel5 ~]#paste file1 file2 file3 > file4 →→ paste 指令将多个文件的内容导出到一个新的文件里 
[root@rhel5 ~]#cat file1 file2 file3 > file4 →→ cat 指令将多个文件的内容导出到一个新的文件里

20.WC

用于统计:行数、单词数、字符数

[root@rhel5 ~]#wc README →→统计:行数、单词数、字符数
207 1923 13011 README 

[root@rhel5 ~]#wc -l README →→只统计:行数
207 README
[root@rhel5 ~]#wc -w README →→只统计:字数
1923 README
[root@rhel5 ~]#wc -c README →→只统计:字源数
13011 README

(常用技巧)
[root@rhel5 ~]#cat /etc/passwd | wc -l  →→ 首先使用cat 指令读出文件, 再交给 wc -l  统计出行数;
TIPS:由于 passwd 按行保存账户信息, 所以总共有58行就代表有58 个 账户 !

21.Head

作用:因为说明总是摆在文件最前面, 使用此命令方便 查看文件用途

[root@rhel5 ~]#head file1 →→默认显示前10行
[root@rhel5 ~]#head -n 15 file1 →→ " -n " 显示前15行
[root@rhel5 ~]#head --lines 17 file1 →→ " --line " 显示前17行
[root@rhel5 ~]#head -12 file1 →→直接加行数也可以:显示前12行

22.Tail

作用:用于查看档案文件 新增加了什么资料信息

[root@rhel5 ~]#tail file1 →→默认显示最后10行
[root@rhel5 ~]#tail -n 15 file1 →→ " -n " 显示最后15行
[root@rhel5 ~]#tail --lines 17 file1 →→ " --line " 显示最后17行
[root@rhel5 ~]#tail -12 file1 →→直接加行数也可以:显示最后12行

Use -f to follow the end of a text file as it changes(Used to watch log files):
[root@rhel5 ~]#tail -f /var/log/messages  →→查看更新的日志文件部分
[root@rhel5 ~]#service network restart  →→重启网卡

 

23. tee

存档工作
[root@rhel5 ~]#cut -f1 -d: /etc/passwd | sort | less  →→ 将cut 后的资料 导到sort 指令进行排序, 最后一页一页的显示出来。

[root@rhel5 ~]#cut -f1 -d: /etc/passwd | tee cut.file | sort | tee sort.file | less →→ 使用tee存档。

 

24.Uniq

Unique, 去除相邻几行内重复的文字
[root@rhel5 ~]#uniq -c file2 →→计算出重复的行数
[root@rhel5 ~]#uniq file2  →→ 去除相邻几行内重复的文字

[root@rhel5 ~]#less file2  →→file2 文件含有重复的行
abc:x:501:501::/home/abc:/bin/bAsh
xyz:x:500:500::/home/xyz:/bin/Bash
xyz:x:500:500::/home/xyz:/bin/Bash
xyz:x:500:500::/home/xyz:/bin/Bash
upload:x:502:502::/class:/bin/bash
xyz:x:500:500::/home/xyz:/bin/Bash
(常用技巧)
[root@rhel5 ~]#sort file2 | uniq  →→ 首先排序, 去除掉文件内所有重复的行 结果如下:
abc:x:501:501::/home/abc:/bin/bAsh
upload:x:502:502::/class:/bin/bash
xyz:x:500:500::/home/xyz:/bin/Bash

[root@rhel5 ~]#sort -u file2 →→上面的指令相当于该指令:sort -u 
abc:x:501:501::/home/abc:/bin/bAsh
upload:x:502:502::/class:/bin/bash
xyz:x:500:500::/home/xyz:/bin/Bash

25.TR
导入功能使用 "<"符号
[root@rhel5 ~]# tr 'A-Z' 'a-z' < .bash_profile →→  "tr" 表示:translate or delete characters ,将文件.bash_profile 大写改成小写并导入
[root@rhel5 ~]# cat -A file5 | tr '$' '\r' > file6.txt  →→ -A 表示将段落符号都显示出来

26.diff

比对档案文件的区别
< indicates line in first file
> indicates line in second file

[root@rhel5 ~]#diff ABC abc
1c1 (  说明:第一行不一样  )
< A  →→表示:A在第一个文件里
- - - 
> a  →→表示:a在第二个文件里
4c4 (  说明:第四行不一样  )
< D  →→表示:D在第一个文件里
- - -
> d  →→表示:d在第二个文件里

26.1. sdiff

比对档案文件的区别
[root@rhel5 ~]# sdiff ABC abc  →→ " | "以pipe 符号标识出来不同的地方 
A  |   a
B      B
C      C
D  |   d
E      E
F      F
G      G


27.Aspell & look

检查拼写错误

[root@rhel5 ~]#look  redha   →→ 直接检查单词的拼写 
[root@rhel5 ~]#aspell  check Readme  →→ 检查文件拼写错误、通过输入数字来更改单词 
[root@rhel5 ~]#less /usr/share/dict/words  →→ 检查所用到的字典库 所在地 
[root@rhel5 ~]#cat  /usr/share/dict/words | wc -l  →→ 统计 字典库的单词总数
483522  (  更新字典库)


28.Expand
[root@rhel5 ~]# cat -A file1  →→ 输出所有字源 包裹 空格等键位 
1^Ia^Ix$
2^Ib^Iy$
3^Ic^Iz$
[root@rhel5 ~]# expand file1 > file2.txt  →→ 将文件中 Tab 键转换为 Space 键 进行格式化并导出

 

29.Fmt

将文本格式化为段落
[root@rhel5 ~]# fmt -u -w50 file5  →→ " -u " 制式化空白规格, " -w "规定每行最长的宽度

 

30. Mail 
Sending mail   -s
[root@rhel5 ~]# mail -s "1st mail" user2 →→  "1st mail"表示:邮件标题;
"user2"表示:收件人(可使用Local Username或者user@gmail.com) 。
Hello User2,This is your first mail !!   →→ 邮件正文部分 
Goodby     →→ 邮件正文部分 
.     →→ "."用以结束 
Cc:     →→提示转发给其他人,Carbon copy 
[root@rhel5 ~]# mail -s "2nd mail" user2 < test.mail  →→如果test.mail是一封写好的邮件正文,则可以使用该指令导入并寄出。

[root@rhel5 ~]# ls -l | mail -s "3rd mail" user2  →→ 将 ls查到的讯息导出到 mail指令进行处理。

Reading mail
[root@rhel5 ~]# mail  →→查看收到哪些邮件?
Mail version 8.1 6/6/93. Type ? for help.
"/var/spool/mail/user2": 2 messages 2 new
>N 1 root@localhost.local  Tue Jun 15 15:18 17/697 "1st mail"
  N 2 root@localhost.local  Tue Jun 15 15:18 16/672 "2nd mail"
& 2    →→输入 " 2 "表示查看编号是 2 的邮件,亦即第二封邮件。 
& x    →→无任何提示 和 操作! 
& q    →→会将已读邮件存入垃圾桶。
Saved 1 message in mbox  →→mbox 类似于垃圾箱
Held 1 message in /var/spool/mail/user2

[root@rhel5 ~]# cat /var/spool/mail/user2  →→ 用以查看自己收到的邮件(此路径就是user2的mailbox)
[root@rhel5 ~]# cat ~user2/mbox  →→在这里可以找到已阅读的邮件。

 

31.xargs 
[root@rhel5 ~]# cat delete_file.txt  →→ 文件中包含如下三个名称。 
f1.txt
f2.txt
f3.txt
[root@rhel5 ~]# cat delete_file.txt | xargs rm -f  →→ 这条命令相当于:
rm -f f1.txt 
rm -f f2.txt 
rm -f f3.txt

32. Pr

按照打印机的格式将文本格式化
[root@rhel5 ~]#pr -5 /usr/share/dict/words | more  →→ 格式化为5 栏 显示出来

[root@rhel5 ~]#pr -h"This is header" -l20 -5 /usr/share/dict/words | lpr  →→ 
" -h " (header)设置显示的表头, " -l20 " 每页总共20行,格式化为5 栏 , 最后使用 lpr 指令打印出来

——————————————————————————————————————

三:VIM

VIM USER MANUAL →→ 

grep: 简介提要grep (global search regular expression(RE) and print out the line,

全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具。

 1. Screen Repositioning

快速定位技巧
H        go to first line on screen(high)
M       go to middle  line on screen(middle)
L        go to last line on screen(low)

z       make current line first line on screen(使光标所在行变成屏幕第一行)
z -                  make current line last line on screen(使光标所在行变成屏幕最后一行)

 

2.Filtering
输入" !! " :
:.! ls -l  →→编辑过程中插入某个文件内容
:.! date  →→编辑过程中插入时间戳记
输入!} :
:.,.+12 sort →→用于排序
:.,.+12 fmt -w60 →→用于设定段落的宽度为:60, 超过后会自动换行。

:r file1               -read the file1 into current file
:1,20w file2       -write lines 1 through 20 to the file2  →→" w "表示:write

:!command       -用于执行一个外部命令 command。 
:!dir                   -用于显示当前目录的内容。 

:!rm FILENAME -用于刪除名为 FILENAME 的文件。

:w FILENAME    -可将当前 VIM 中正在编辑的文件保存到名为 FILENAME的文件中。
:1,$w file3         -write entire file to the file3   →→" w "表示:write
:1,20w  >> file4 -append lines 1 through 20 to the file4  →→" w "表示:write

 

3.开启并管理多个文件

[root@rhel5 ~]#vi test1 test2 test3   →→同时用 VI 打开三个档案文件
:n           move to the next file   →→切换到下一个档案文件
:rew      rewind to the first file   →→切换到上一个档案文件
:n#        jump to the previous file,thus toggling between two files→→在两个档案文件之间互相切换

 

4.VI 编辑器的设定选项

:set  →→显示重要的设定选项 
:set all  →→显示全部的设定选项

:set ignorecase(ic)  →→设定忽略大小写
:se noic   →→设定取消忽略大小写
:set number  →→设定显示行号
:se nonu   →→设定取消显示行号

 

5. Command Mode、Insert Mode、Extended Mode
w           -word ahead  →→跳转到下一个单词
b           -word back  →→跳转到上一个单词
(            -sentence back  →→跳转到上一个句子
)            -sentence forward  →→跳转到下一个句子
{            -paragraph above  →→跳转到上一段
}            -paragraph below  →→跳转到下一段

 

6. Entering Insert Mode
a          -append after the cursor
i           -insert before the cursor
A          -append to end of line
I           -insert at beginning of line
o          -open a line below
O         -open a line above

 

7.Change、Delete、Yank(Copy)
(Name)      Change      Delete     Yank(Copy)
Line           cc               dd             yy
Letter        cl                dl              yl
Word         cw              dw            yw

 

8.Use p or P put(Paste) changed,deleted or copied data
For  line oriented data:
 p             -puts the data below the current line
 P             -puts the data above the current line
For character oriented data:
 p            -puts the data after the current cursor
 P            -puts the data before the current cursor

 

9.Undoing Changes
u                   -undo most recent change
     -redo last "undone" change  →→  " r " = (redo) 
U                  -undo all changes to the current line


10.Searching for Text
/text             -search downwards for "text"
?text            -search upwards for "text"
n                   -continue search in the same direction
N                   -continue search in the opposite direction

 

11.Tricks
dtc               -delete from cursor to the letter c
rc                 -replace a character with c
x                   -delete a character
5dd              -delete 5 lines
5yy               -copied 5 lines
5x                -delete 5 characters
R                 -replace character-for-character until


12.Extended Mode
:w               -Saving(Writing)
:q               -Quitting
:wq            -Saving and Quitting
:w!              -Forcing save
q!               -Forcing quit
:wq!           -Forcing save and quit


13.Ctrl-g and Shift-G

Ctrl-g 用于显示当前光标所在位置和文件状态信息。

Shift-G 用于将光标跳转至文件最后一行。先敲入一个行号然后 按Shift-G 则是将光标移动至该
行号代表的行。


14. Replace

 如果光标当前位置是括号(、)、[、]、{、},按 % 可以将光标移动到配对的括号上。

在一行內替换头一个字符串 old 为新的字符串 new,请输入  :s/old/new
   
在一行內替换所有的字符串 old 为新的字符串 new,请输入  :s/old/new/g
    
在两行內替换所有的字符串 old 为新的字符串 new,请输入  :#,#s/old/new/g
     
在文件內替换所有的字符串 old 为新的字符串 new,请输入  :%s/old/new/g
     
进行全文替换时询问用户确认每个替换需添加 c 选项,请输入 :%s/old/new/gc 

——————————————————————————————————————

 四:进阶技巧

1.Tar

1.1. 打包档案文件
[root@rhel5 ~]# tar cvf ~/doc.tar doc  →→ 打包 doc目录里全部档案文件到doc.tar

1.2. 查看归档文件

[root@rhel5 ~]# tar tf doc.tar  →→ 查看归档文件里档案清单 
[root@rhel5 ~]# tar tvf doc.tar  →→ 查看归档文件里详细的档案清单

1.3. 打包并压缩档案文件
z -for gzip  compression
j -for bzip2  compression

[root@rhel5 ~]# tar cvfj  doc.tar.bz2 doc →→ 建立一个归档文件 并且 使用bzip2 进行压缩 
[root@rhel5 ~]# tar cvfz  doc.tar.gz doc →→ 建立一个归档文件 并且 使用gzip 进行压缩

1.4. 解压归档文件
[root@rhel5 ~]# tar xvf doc.tar  →→ 解压归档文件 

[root@rhel5 ~]# tar xzvf doc.tar.gz  -C test/ →→ 解压gz归档文件到指定目录 

[root@rhel5 ~]# tar xzvf doc.tar.gz hello.txt  →→ 解压gz归档文件中的指定文件 


1.5. 追加文件

[root@rhel5 ~]# tar -rf  doc.tar ubuntu.png  →→ 向归档文件中追加文件


1.6.1 zip

[root@rhel5 ~]# zip -P 123 hello.zip hello.txt→→使用 zip 进行压缩, 密码:123

[root@rhel5 ~]# unzip -P 123 hello.zip →→使用 unzip 进行解压缩, 密码:123

[root@rhel5 ~]# unzip -l iptables.zip →→使用 unzip 查看压缩包内文件

[root@rhel5 ~]# zip -d iptables.zip bbs.txt →→使用 zip 删除压缩包内指定文件

1.6.2 rar

[root@rhel5 ~]# 由于RAR 不是免费产品, 所以鄙人没有使用它。


2.gzip, gunzip

一般配合Tar命令使用
[root@rhel5 ~]# gzip ./doc/about.txt  →→ 使用 gzip 进行压缩, 并且不保留源文件(亦即:将源文件更改为压缩档) 
[root@rhel5 ~]# gunzip ./doc/about.gz  →→ 使用 gzip 进行解压缩 
[root@rhel5 ~]# gzip -9 -vc ./doc/about.txt > ./doc/about.gz  →→ 使用 gzip 进行压缩(使用最高压缩比), 并且保留源文件
[root@rhel5 ~]# bzip2 -9 -vc ./doc/about.txt > ./doc/about.bz2  →→ 使用 bzip2 进行压缩(使用最高压缩比), 并且保留源文件


3. bzip2, bunzip2 (压缩比 较之gzip 会稍高)
用法可类比gzip,gunzip


4. Symbolic Links

鄙人认为类似于Windows的快捷方式
ln -s filename [linkname]
[root@rhel5 ~]# ln -s doc/doc1 Desktop/doc1  →→  在目标目录建立一个源档案的符号链接,与源文件对应不同的 "Inode-no"。


5. Hard Links
ln  filename [linkname]

Hard Link 只能针对档案建立, 无法对目录进行建立。

TIPS:"Hard link" 的 " Link count " 属性值为:  "2"


[root@rhel5 ~]# ln doc/doc1 backup →→ 建立" hardlink "   
[root@rhel5 ~]# ls -li doc   →→ " Link count " 属性值为:  "1",  "Inode-no"值为:688469 
total 108
688469 -rw-r--r-- 1 root root 0 Oct 01 23:16 delete_file.txt

[root@rhel5 ~]# ls -li backup  →→ " Link count " 属性值为:  "2" , "Inode-no"值同样是:688469 
total 8
688469 -rw-r--r-- 2 root root 0 Oct 01 23:16 delete_file.txt


6. alias
[root@rhel5 ~]#alias  →→ 列出所有别名 
[root@rhel5 ~]#alias lf='ls -laF'  →→ 创建别名 
[root@rhel5 ~]#alias lf  →→  查看 "lf" 的别名


 7.Set & Variables

[root@rhel5 ~]#set | more  →→  分页显示所有变量 
[root@rhel5 ~]#set -o noclobber  →→ 保护已存在的档案不会被覆盖掉  
[root@rhel5 ~]#export A  = 8  →→ 建立变量并设定变量为环境变量 
[root@rhel5 ~]#unset  A  →→删除变量, 非root 用户只能删除自己的变量


7.1. Common Environment Variables
HOME             -Path to user's home directory

LANG              -Identification of default language  programs should be use;
 example: en_US.UTF-8 for U.S.English
PWD               -User's current  working directory

TERM             -environment variable defines the terminal type(图形界面登录 预设:TERM=xterm)

reset              -command(not variable) used to reset a terminal should the screen become corrupted(屏幕乱码时,此命令重设屏幕画面)

PATH              -colon seperated list of locations where commands can be found

which/whereis   -command(not variable)  showing location in the PATH of an executable


7.2. 使用技巧

7.2. 1. echo & backquotes
[root@rhel5 ~]# echo "Hostname:" →→回显字符串*/
[root@rhel5 ~]# echo "Hostname:`hostname`" →→ 倒引号" ` "倒引号用于包裹指令以优先执行 
[root@rhel5 ~]# echo "Hostname:$(hostname)" →→ 倒引号" ` " 和"$()" 实现同样的效果 
Hostname:localhost.localdomain

7.2. 2. backslash
[root@rhel5 ~]# echo Your cost is \$5.00 →→ backslash("\" 反斜杠) 进行转译 
[root@rhel5 ~]# ls \  →→ 命令最后跟 "\"  
> -l  →→如果输入的指令很长就可以使用 "\" 以便可以接着输入指令

7.2. 3.Double Quotes 、Single Quotes、backslash

[root@rhel5 ~]# echo \*\* 00000 \*\*  →→ 进行转译显示出来 
[root@rhel5 ~]# echo "** 00000 **"  →→ 使用双引号 (Double Quotes)
[root@rhel5 ~]# echo '** 00000 **'  →→ 使用单引号 (Single Quotes)
TIPS:
Single Quotes inhibit all expansion(可以禁止所有字元功能)
Double Quotes inhibit all expansion except:
$(dollar sign)
\(backslash)
`(backquotes)
!(exclamation point)


 7.3. Inodes (index node) --- (存取数据全过程)
系统首先找到 "Inode-no" ,再根据"Inode-no"  对应到 "Inode-table",
接着由table 中pointer 值找到 档案存放在哪几个 " block "(存取数据的最小单位) 里面。

 ——————————————————————————————————————

五:标准输入和输出

【Standard Input and Output】:
Standard input       -keyboard is default      -0
Standard output    -terminal windows is default  -1
Standard error       -terminal windows is default  -2

『 查看标准的输入、输出和错误目录』:

[root@rhel5 ~]#ls -l /dev/std*   
lrwxrwxrwx 1 root root 15 Oct 10 22:22 /dev/stderr ->/proc/self/fd/2
lrwxrwxrwx 1 root root 15 Oct 10 22:22 /dev/stdin ->/proc/self/fd/0
lrwxrwxrwx 1 root root 15 Oct 10 22:22 /dev/stdout ->/proc/self/fd/1

TIPS:">"该符号代表导出,并且会直接覆盖已存在的文件。
[root@rhel5 ~]#cal 2009>2009calendar →→将日历输入到档案上
TIPS:">>"该符号代表导出,并且会在已存在的文件后继续添加。
[root@rhel5 ~]#cal 2009>>2009calendar →→将日历附加到档案上

『导出标准输出讯息』
[root@rhel5 ~]#find /etc -name passwd 1> stdout
 →→ 普通用户查看此目录会输出标准错误讯息Permission denied和标准输出讯息 ,
这里的 "1" 表示 将标准输出的内容导入到 stdout 文件中去(而不包括错误讯息,可以将"1"改为"2"用以导出错误讯息)。
TIPS:默认(default)是 "1",即会导出标准输出讯息。

『导出标准错误讯息』
[root@rhel5 ~]#find /etc -name passwd 2> stderr →→将标准的错误讯息导出到档案里

『分别导出标准错误讯息和标准输出讯息』
[root@rhel5 ~]#find /etc -name passwd 2> errs 1>output  →→将 标准错误讯息和标准输出讯息 分别导出到档案里

『导出标准错误讯息和标准输出讯息到同一档案里』
[root@rhel5 ~]#find /etc -name passwd > alloutput  2>&1 →→将 标准错误讯息和标准输出讯息 导出到同一个档案里,
这里的 "2>&1"表示:将2(错误讯息)同样导出到1(标准输出讯息)所导出的档案里。

 

[root@rhel5 ~]#find /etc -name passwd &> alloutput  →→ "&" 表示All(0,1,2,...)所有的编号。
TIPS:建议不使用该方式,因为可能还会导入其他讯息。


『导入功能使用 "<"符号』
[root@rhel5 ~]#tr 'A-Z' 'a-z' < .bash_profile →→  "tr" 表示:translate or delete characters ,将文件.bash_profile 大写改成小写并导入

——————————————————————————————————————

 END:打印机

【Printing in Linux】
CUPS →→  (Common Unix Printing System)
Documents  →→  Queue(FirstComeFirstOut FIFO) →→  CUPSD  →→  Input Filter →→  Printer

Queue: →→已经准备打印的状态为:Active, 其他状态可进行 Move、Cut和Delete的管理。
 →→Queue 的存放地址为:/var/spool/cups


『CUPS Configuration Files』:
[user1@localhost~]$vi /etc/cups/cupsd.conf
[user1@localhost~]$vi /etc/cups/printers.conf
lpadmin system-config-printer CUPS web  →→ 用于配置printers.conf


『CUPS Queue Management』:

[user1@localhost~]$system-config-printer  →→  图形界面打印机配置工具*/
[user1@localhost~]$system-config-printer-tui  →→  文字模式打印机配置工具*/
浏览器:
   →→ Web 模式管理打印机配置工具*/

1.设置打印机
[root@rhel5 ~]# system-config-printer  →→ Printer Configuration  安装完毕后点击Apply 用以应用到本机。

 

2.BSD (FreeBSD)
[root@rhel5 ~]# lpr Readme  →→ 打印 Readme 文件 
[root@rhel5 ~]# lpq →→ 查看打印结果 
HP is ready and printing
Rank Owner Job File(s) Total Size
active root 1 Readme 13312  bytes

[root@rhel5 ~]#lpr -Pdell Readme  →→ 指定使用哪个打印机进行打印工作。  
[root@rhel5 ~]# lpq -Pdell  →→ 用来查看非默认打印机的打印结果 。"-P" 后面跟打印机名称(无空格)
HP is ready and printing
Rank Owner Job File(s) Total Size
active root 1 Readme 13312  bytes

 

3.System V
[root@rhel5 ~]# lp Readme  →→ 打印 Readme 文件 
request id is HP-2 (1 file(s))
[root@rhel5 ~]# lpstat →→ 查看打印结果 
HP-2 root 13312 Web 10 Jun 2009  12:12:05   AM CST

[root@rhel5 ~]#lp -ddell Readme  →→ 指定使用哪个打印机进行打印工作。 

 

4.删除打印工作
[root@rhel5 ~]#lprm 1 →→ BSD(FreeBSD) 里删除jobId 为 " 1 "的打印工作
[root@rhel5 ~]#cancel 1 →→ System V 里删除jobId 为 " 1 "的打印工作

 

5.移动Queue 里的打印工作
[root@rhel5 ~]#lpmove HP-1 dell →→ 将HP-1 的打印工作搬到 dell的Queue里进行打印工作。

 

6.如何插队打印
[root@rhel5 ~]#lp -i17 -q51 →→ "-i"表示ID, 默认优先权是50  使用"-q51" 设定优先权。


7.如何马上处理打印
[root@rhel5 ~]#disable HP →→ 首先关闭打印机 
[root@rhel5 ~]#/usr/bin/enable HP  →→ 然后开启打印机  
[root@rhel5 ~]#reject HP  →→ 停止Queue ,文件无法传到Queue里。
[root@rhel5 ~]#accept HP →→ 打开Queue


 

8.【Printing Utilities - PostScript】
[root@rhel5 ~]#enscript Readme -p Readme.ps  →→ 转换成 postscript 格式  
[5  pages * 1 copy ] left in Readme.ps
35 lines were wrapped


[root@rhel5 ~]#ggv Readme.ps  →→ ggv指令可以查看 postscript格式文件 和 pdf 格式文件


[root@rhel5 ~]#ps2pdf Readme.ps > Readme.pdf  →→ ps2pdf指令可以将ps格式文件转换为pdf格式文件 
[root@rhel5 ~]#pdf2ps Readme.pdf > Readme.ps  →→ pdf2ps指令可以将pdf格式文件转换为ps格式文件


[root@rhel5 ~]#mpage -4 Readme.ps > mpage.Readme.ps  →→ 将4页合成一张纸来打印。

——————————————————————————————————————

阅读(459) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~