Linux学习笔记

 Linux 学习笔记 换 请切换 web 视图查看,表格比较大 ,方法:视图》》web 板式视图

 目录 Linux 学习笔记 ................................................................................................................................. 1 请切换 web 视图查看,表格比较大,方法:视图》》web 板式视图 ................................ 1 1. 常用命令 ........................................................................................................................... 3 1.1 文件处理命令 ............................................................................................................. 3 1.2 权限管理命令 ............................................................................................................. 8 1.3 文件搜索命令 ........................................................................................................... 12 1.4 帮助命令 ................................................................................................................... 19 1.5 压缩解压命令 ........................................................................................................... 20 1.6 网络通信指令 ........................................................................................................... 23 1.7 Shell 应用技巧 .......................................................................................................... 25 2. Vim/Vi ............................................................................................................................. 28 3. 引导 ................................................................................................................................. 34 4 软件包的安装 ...................................................................................................................... 39 4.1 挂载光盘 ................................................................................................................... 39 5.用户管理 .............................................................................................................................. 40 5.1 组管理 ....................................................................................................................... 41 5.2 批量添加用户 ........................................................................................................... 41 6. 进程管理 ......................................................................................................................... 44 6.2 计划任务 ................................................................................................................... 45 7. shell 编程 ........................................................................................................................ 46 7.1 hello world .......................................................................................................... 46 7.2 标准输入和输出 ...................................................................................................... 46 7.2 变量, 位置变量$! $$ $# $@ $* $?, 环境变量,运算符 ........................................... 47 7.3if…else ........................................................................................................................ 52 7.4for ............................................................................................................................... 53

 7.5awk ............................................................................................................................. 54 7.6 一个显示用户信息的脚本....................................................................................... 54 7.7 一个杀死登陆用户的脚本....................................................................................... 56 结果如下 ......................................................................................................................... 56 脚本内容 ......................................................................................................................... 56 7.8select case .................................................................................................................. 57 select ............................................................................................................................... 57 select+case ...................................................................................................................... 57 case ................................................................................................................................. 58 7.9while .......................................................................................................................... 59 7.10 批量添加用户 ........................................................................................................ 59 7.11 批量删除用户 ........................................................................................................ 60 7.12break

 .. continue .................................................................................................. 60 7.13 shift, 参数累加求和 ................................................................................................ 61 效果:

 参数左移 ........................................................................................................... 61 7.14 用户执行脚本的权限 ............................................................................................ 62 sh script ........................................................................................................................... 62 1. 对脚本有 r 权限 ..................................................................................................... 62 2. 脚本所在目录要有 r-x 权限 .................................................................................. 62 脚本直接执行 ................................................................................................................. 62 1. 对脚本有 rx 权限 .................................................................................................... 62 2. 脚本所在目录有 rx 权限 ........................................................................................ 62 7.15 正则表达式 ............................................................................................................ 62 7.16 截取字符串 awk ,cut ,set................................................................................... 65 awk 列操作 .................................................................................................................... 66 第一行不执行,第一行先读取的 ................................................................................. 66 所以加 BEGIN ................................................................................................................. 67 行操作 ............................................................................................................................. 67 选取某一行 ..................................................................................................................... 68 去掉某一行 ..................................................................................................................... 68 追加某一行或几行 ......................................................................................................... 68 替换 ................................................................................................................................. 69 7.17 定时任务 ................................................................................................................ 69 8. 安装 jdk................................................................................................................... 71 [root@dc-01 java]#

 rpm -qa | grep jdk ......................................................................... 71 java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.i686 ............................................................. 71 卸载之:

 ......................................................................................................................... 71 [root@dc-01 java]# yum -y remove java-1.6.0-openjdk-1.6.0.0-1.45.1.11.1.el6.i686 .... 71 查看系统位数 ................................................................................................................. 71 下载对应的安装包 ......................................................................................................... 71 http://pan.baidu.com/s/1qWnzJCK ................................................................................ 71 1.修改权限为可执行 ...................................................................................................... 71 2.rpm –ivh 安装包 ......................................................................................................... 71 3.vi /etc/profile 尾部添加环境变量 .............................................................................. 72

 79 JAVA_HOME=/usr/java/jdk1.7.0_67 .......................................................................... 72 80 PATH=$JAVA_HOME/bin:$PATH ................................................................................. 72 81 CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ............................ 72 82 export JAVA_HOME .................................................................................................... 72 83 export PATH ................................................................................................................ 72 84 export CLASSPATH ...................................................................................................... 72 4. 执行 source /etc/profile 命令让修改生效。

 ............................................................ 72 5.查看安装结果 .............................................................................................................. 72 至此,安装 jdk 成功 ...................................................................................................... 73 9. 安装 Tomcat ............................................................................................................ 73 1.下载安装包,千万选好安装源,因为安装包的问题,硬是让我调试了一天,真是傻逼,重新官网下载安装之后立马可以用了 ............................................................. 73 http://tomcat.apache.org/download-70.cgi ................................................................... 73 2.解压到/usr/local 下,并重命名为 tomcat ................................................................ 73 #tar zxvf apache-tomcat-7.0.35.tar.gz /解压 .................................................................. 73 #mv apache-tomcat-7.0.35 apache-tomcat /更名 .......................................................... 73 #cd /apache-tomcat/bin/ /打开此目录 .......................................................................... 74 #chmod 777 *.sh /加 777 权限 ....................................................................................... 74 #bash catalina.sh start /启动 .......................................................................................... 74

  1. 常用命令 命令存放,哪些命令可以执行?

 root: /sbin /usr/sbin all users:

 /bin /usr/bin bin--binary 二进制文件 Linux 所有都是文件,都是二进制文件 usr—user sbin—super binary

  1.1 文件处理命令

 文件处理命令

 1 ls 英 文 原 意 是 list

  Eg 含义

 功能:显示目录文件 属性可以连着写,比如 ls -ld -a

 all 显示所有文件,包括隐藏文件 ls -a /

 -l

 long

 -I 显示 i 节点 详细信息显示 [root@localhost ~]# ls -l total 68 -rw------- 1 root root

 1211 Feb 14 04:02 anaconda-ks.cfg drwxr-xr-x 3 root root

 4096 Feb 14 11:20 Desktop -rw-r--r-- 1 root root 31736 Feb 14 04:02 install.log -rw-r--r-- 1 root root

  0 Feb 14 03:16 install.log.syslog drwxr-xr-x 2 root root

 4096 Feb 14 17:36 test drwxr-xr-x

  2

 root

  root

  4096

 Feb 14 11:20 每三个表示权限

  表示硬连接数

  所有者

 所属组

 文件大小

 创建时间 数 据 块 block

 512 字节 第一个字符表示如下:

 d

 目 录directory - 二进制文件 l

 软链接文件 每三个分配如下:

 r—read 读

  w—write 写

 x—execute 执行 rwx

  r-x

 r-x 所有者 u

 所属组 g

 其他人 o user

  group

 others -d 查看 [root@localhost ~]# ls -ld /test

 目录属性 drwxr-xr-x 3 root root 4096 Feb 14 17:42 /test 2 cd 切换目录

  Cd

 / 进入根目录

  Cd [目录]

 Cd .. 返回父级目录

  3 pwd 英文:print working directory 查看当前目录 [root@localhost ~]# pwd /root

 4 touch 创 建 空 文件

  [root@localhost test]# touch testfile [root@localhost test]# ls -l testfile -rw-r--r-- 1 root root 0 Feb 14 18:51 testfile

  5 mkdir 英文:make directories 创建空目录 [root@localhost ~]# mkdir test

 6 cp 英文:copy 复制文件 -R 复制目录

 copy

  [源文件,可多个] [目的目录] [root@localhost test]# cp /etc/inittab /etc/services /test [root@localhost test]# ls /test abc.txt

 inittab

 lost+found

 samlee

 services

 testfile

 将 etc 拷贝到 test 下 [root@localhost test]# cp -R /etc /test

 /etc 主 要 是 配置文件,很重要,记得备份 命令停止快捷键:Ctrl+c 7. mv 英文:move 移动文件,改名 [root@localhost test]# ls /test abc.txt

 etc

 inittab

 lost+found

 samlee

 services

 testfile 改名:

 [root@localhost test]# mv services service

  [root@localhost test]# ls /test abc.txt

 etc

 inittab

 lost+found

 samlee

 service

 testfile 移动:

 [root@localhost test]# mv /test/inittab

 /tmp/ [root@localhost test]# ls /test abc.txt

 etc

 lost+found

 samlee

 service

 testfile 改名+移动:

 [root@localhost test]#

 mv /test/testfile /tmp/file.test [root@localhost test]# ls /tmp file.test

  scim-panel-socket:0-root gconfd-root

  scim-socket-frontend-root inittab

  ssh-yoUWCY4180 keyring-z1fmdW

 virtual-root.IYU2ep mapping-root

 vmware-config0 orbit-root

 VMwareDnD scim-bridge-0.3.0.lockfile-0@localhost:0.0

 vmware-root scim-bridge-0.3.0.socket-0@localhost:0.0

 vmware-root-592157060 scim-helper-manager-socket-root

 8 rm remove 删除 [root@localhost test]# ls /test abc.txt

 etc

 lost+found

 samlee

 service 删除 service [root@localhost test]# rm service rm: remove regular file `service"? y [root@localhost test]# ls /test abc.txt

 etc

 lost+found

 samlee 删除不询问:

 [root@localhost test]# touch testfile [root@localhost test]# ls abc.txt

 etc

 lost+found

 samlee

 testfile [root@localhost test]# rm -f testfile [root@localhost test]# ls abc.txt

 etc

 lost+found

 samlee

 rm –f 目录名称

 不提示,强制删除,在写脚本的时候用,因为没有 yes 交互 r 表示删除,f 表示强制 9 cat concatenate 查看 [root@localhost test]# cat /etc/issue 适合查看文件

 and display files CentOS release 5.5 (Final) Kernel \r on an \m

 小,因为多的话会把前面的覆盖,不能翻页。

 比 如 cat /etc/services 10 more (空格)或 f 显 示 下 一页 (enter)显示下一行 q或Q 退出 分页查看 [root@localhost test]# more /etc/services

  11 head -num 文件名 查看文件前num行 [root@localhost test]# head -5 /etc/services # /etc/services: # $Id: services,v 1.42 2006/02/23 13:09:23 pknirsch Exp $ # # Network services, Internet style #

  12 tail -num

 文件名 -f

  文件名 查看文件尾num行 动态查看文件尾部信息 [root@localhost test]# tail -5 /etc/services com-bardac-dw

  48556/tcp

  # com-bardac-dw com-bardac-dw

  48556/udp

  # com-bardac-dw iqobject

 48619/tcp

 # iqobject iqobject

 48619/udp

  # iqobject # Local services [root@localhost test]# tail -f /etc/services

 tail –f 为了监视日志文件,默认显示 10 行 13 ln 英文:link ln 文 件 名

 目的目录 ln –s 文件名 目的目录 -s soft 软连接 创建硬链接 创建软链接 [root@localhost test]# ln -s /etc/issue /issue.soft [root@localhost test]# ls -l /etc/issue /issue.soft -rw-r--r-- 1 root root 47 Apr 26

 2010 /etc/issue lrwxrwxrwx 1 root root 10 Feb 14 16:51 /issue.soft -> /etc/issue

 硬链接:

 [root@localhost test]# ln

 /etc/issue /issue.hard 软连接文件类型是 l 软连接所有人都有权限,但能不能访问取决于源文件 软连接时间值 是创建软连接的时间 相当于快捷方式

 [root@localhost test]# ls -l /etc/issue /issue.hard -rw-r--r-- 2 root root 47 Apr 26

 2010 /etc/issue -rw-r--r-- 2 root root 47 Apr 26

 2010 /issue.hard

 拷贝:

 [root@localhost test]# cp /etc/issue /test/issue [root@localhost test]# ls -l /etc/issue /test/issue -rw-r--r-- 2 root root 47 Apr 26

 2010 /etc/issue -rw-r--r-- 1 root root 47 Feb 14 20:31 /test/issue

 拷贝并且不改变时间,比如某些备份 [root@localhost test]# cp -p /etc/issue /test/issue cp: overwrite `/test/issue"? y [root@localhost test]# ls -l /etc/issue /test/issue -rw-r--r-- 2 root root 47 Apr 26

 2010 /etc/issue -rw-r--r-- 1 root root 47 Apr 26

 2010 /test/issue

 类似于 copy,文件大小相同 不同于 copy 的是,它是同步更新的。

 还有一个不同的是,拷贝的时间是不同,时间是创建的时间

 一个 i 节点可以映射到多个文件,所以硬链接可以同步更新。

  1.2 权限管理命令 权限管理命令 14 chmod change the permissions mode of a file chmod[{ugo0}{+-=}[文 件 或 目录][mode=421][文件或目录] 功能:

 改变文件或目录权[root@localhost test]# ls -l a -rwxr-x--- 1 root root 0 Feb 19 15:45 a [root@localhoschmod u+权限 文件或目录-------给所属者增加权限 chmod u+权限 文件或目录-------给所属者减少权限

 限

  用数字表示 r--4 w--2 x—1 rwxr-xr--

 754 rw-r-x—x

 651

  文件:

 r-car,more,head,tail w-echo,vi 可以修改内容 x-命令,脚本 目录:

 r-ls w-touch,mkdir,rm 对目录有这些权限 x-cd 对目录可以进去 所以,所有的目录都有 rx 权限,表示可以读取可以进去 t test]# chmod o+rwx a [root@localhost test]# ls -l a -rwxr-xrwx 1 root root 0 Feb 19 15:45 a [root@localhost test]# chmod o-rw a [root@localhost test]# ls -l a -rwxr-x--x 1 root root 0 Feb 19 15:45 a [root@localhost test]# chmod g=r a [root@localhost test]# ls -l a -rwxr----x 1 root root 0 Feb 19 15:45 a [root@localhost test]# chmod g=rx a [root@localhost test]# ls -l a -rwxr-x--x 1 root root 0 Feb 19 15:45 a

 [root@localhost test]# chmod 641 a [root@localhost test]# ls -l a -rw-r----x 1 root root 0 Feb 19 15:45 a

 chmod g=权限 文件或目录-------给所属组赋予权限

 chmod o -----------------------------------同 u,为加减权限

 用数字表示权限,直接赋值就可以

 chmod 641 a

 15 chown change file ownership 改变所有者 [root@localhost test]# ls -l a -rw-r----x 1 root root 0 Feb 19

 15:45 a [root@localhost test]# chown helen a [root@localhost test]# ls -l a -rw-r----x 1 helen root 0 Feb 19 15:45 a [root@localhost test]# chown nobody a [root@localhost test]# ls -l a -rw-r----x 1 nobody root 0 Feb 19 15:45 a

 16 chgrp change file group ownership 改变所属组 [root@localhost test]# ls -l a -rw-r----x 1 nobody root 0 Feb 19 15:45 a [root@localhost test]# chgrp adm a [root@localhost test]# ls -l a -rw-r----x 1 nobody adm 0 Feb 19 15:45 a

  17 umask

 查看创建文件的默认权限 改 变 默 认 权 限 umask 权限掩码值 [root@localhost test]# mkdir newdir [root@localhost test]# ls -ld newdir drwxr-xr-x 2 root root 4096 Feb 20 10:40 newdir [root@localhost test]# umask

 0022 [root@localhost test]# umask -S u=rwx,g=rx,o=rx

 * 0022

 特殊权限位 022-用户权限位,权限掩码值

 777 022 755 022 就是表示 755

 SetUID 4 SetGID 2 chmod u+s

 4755

 2755 组 id

  6755同时 SetUID 和SetGID

 * Linux权 限规则

 缺省创建的文件不能授予可执行 x 权限 [root@localhost test]# touch testfile [root@localhost test]# ls -l testfile -rw-r--r-- 1 root root 0 Feb 20 10:50 testfile

 虽然创建文件的默认权限是 022,但创建的空文件 testfile 没有 x 权限 * 添 加用户

  [root@localhost test]# useradd miao [root@localhost test]# passwd miao Changing password for user miao. New UNIX password:

 BAD PASSWORD: it is too short Retype new UNIX password:

 passwd: all 添加用户信息 设置密码 设置密码

 authentication tokens updated successfully. * su - 切换用户

  1.3 文件搜索命令 Linux 中所有都是文件,命令也是文件 文件搜索命令 18 which

 查询命令所在的绝对路径 [root@localhost ~]# which chmod /bin/chmod [root@localhost ~]# whereis ls ls: /bin/ls /usr/share/man/man1p/ls.1p.gz /usr/share/man/man1/ls.1.gz

 命 令 也是文件 whereis除 了 绝对路径,还 包 括帮 助 文档 19 find

 查找文件或目录 find [搜索路径][搜索关键字]

 -name 文 件名 *匹配任意字符 init* ?匹配单个字符 init???

 -size 文件大小 block 数据块 512 字节=0.5kb 大于+ 小于- 等于 find / -size 204800

 -user 文件所[root@localhost ~]# find /etc -name init /etc/sysconfig/init

 试验通配符和占位符 [root@localhost ~]# find /etc -name init* /etc/selinux/targeted/contexts/initrc_context /etc/sysconfig/init /etc/sysconfig/network-scripts/init.ipv6-global /etc/init.d /etc/mail/spamassassin/init.pre /etc/inittab /etc/initlog.conf /etc/rc.d/init.d

 [root@localhost ~]# find /etc -name init??? /etc/inittab [root@localhost ~]# find /etc -name init? [root@localhost ~]# find /etc -name init?? /etc/init.d /etc/rc.d/init.d

  *通配符

 大范围查找只在晚上2点左右进行

 有者

 时间 1. 天 ctime

 atime

 mtime 2. 分 钟 cmin

 amin

 mmin c—change 改变,表 示 文件 的 属性 被 修改过,所有者、所属组、权限 a—access 访问 m—modify 修改,表 示 文件 的 内容 被 修改过 -之内,+超过 find /etc –mmin

 -120 一分钟内被修改过 -type 文件类型

 f 二进制文件

 l 软 连 接 文件

 d 目录 1. 连 接 符

 -a and

  逻辑与

  查找用户 [root@localhost ~]# find /home -user helen /home/helen /home/helen/.bash_logout /home/helen/.kde /home/helen/.kde/Autostart /home/helen/.kde/Autostart/.directory /home/helen/.mozilla /home/helen/.mozilla/extensions /home/helen/.mozilla/plugins /home/helen/.bash_profile /home/helen/.bashrc /home/helen/.bash_history

 测试转义字符 [root@localhost ~]# which rm alias rm="rm -i"

  /bin/rm [root@localhost ~]# rm abc.txt rm: remove regular empty file `abc.txt"? [root@localhost ~]# \rm abc.txt

 查找二进制文件 [root@host141 ~]# find /etc -name init* -a -type f /etc/inittab /etc/initlog.conf /etc/sysconfig/network-scripts/init.ipv6-global /etc/sysconfig/init /etc/selinux/targeted/contexts/initrc_context

 测试连接符-exec [root@localhost ~]# find /etc -name inittab /etc/inittab [root@localhost ~]# find /etc -name inittab -exec ls -l {} \; -rw-r--r-- 1 root root 1666 Feb 14 04:02 /etc/inittab

 [root@localhost ~]# cd /test [root@localhost test]# pwd /test ,因为访问人数少,只占用一部分资源

 ?匹配单个字符,相当于占位

  通 常用>或者<

 -o or

  逻辑或 2.

 连 接 符 find …. –exec 命令{}\;

  {}find 查询的结果

  \转义符,符号命令使用本身的含义

  ;结果

  -ok 提示

  [root@localhost test]# ls a

 a.hard

 a.soft

 etc

 issue

 lost+found [root@localhost test]# find /test -name a.soft /test/a.soft [root@localhost test]# find /test -name a.soft -exec rm {} \; [root@localhost test]# ls a

 a.hard

 etc

 issue

 lost+found

 测试连接符-ok [root@localhost test]# find /etc -name inittab -exec ls -l {} \; -rw-r--r-- 1 root root 1666 Feb 14 04:02 /etc/inittab [root@localhost test]# find /etc -name inittab -ok ls -l {} \; < ls ... /etc/inittab > ? y -rw-r--r-- 1 root root 1666 Feb 14 04:02 /etc/inittab

 [root@localhost test]# ls a

 a.hard

 etc

 issue

 lost+found [root@localhost test]# find /test -name a.hard -ok rm {} \; < rm ... /test/a.hard > ? y [root@localhost test]# ls a

 etc

 issue

 lost+found

 [root@localhost test]# find /etc -name init* -a -type f -exec ls -l {} \; -rw-r--r-- 1 root root 28 Apr 10

 2010 /etc/selinux/targeted/contexts/initrc_context -rw-r--r-- 1 root root 1068 Jul

 4

 2009 /etc/sysconfig/init -rwxr-xr-x 1 root root 5433 Jul

 4

 2009 /etc/sysconfig/network-scripts/init.ipv6-global -rw-r--r-- 1 root root 1299 Jan 21

 2009 /etc/mail/spamassassin/init.pre -rw-r--r-- 1 root root 1666 Feb 14 04:02 /etc/inittab

  rm 的别 名是 rm-i 所 以要 提示,那么 要使 用本 身的 含义,加转 义字符\

 查 找二 进制 文件

 -rw-r--r-- 1 root root 658 Sep 29

 2009 /etc/initlog.conf

 测试根据 i 节点删除文件 [root@localhost test]# touch "a b"; [root@localhost test]# ls a

 a b [root@localhost test]# touch -- -abc [root@localhost test]# ls a

 a b

 -abc

 [root@localhost test]# rm -abc rm: invalid option -- a Try `rm ./-abc" to remove the file `-abc". Try `rm --help" for more information. [root@localhost test]# rm a b rm: remove regular empty file `a"? a rm: cannot lstat `b": No such file or directory [root@localhost test]# rm a b rm: remove regular empty file `a"? y rm: cannot lstat `b": No such file or directory

 [root@localhost test]# rm -- -abc "a b" rm: remove regular empty file `-abc"? y rm: remove regular empty file `a b"? y [root@localhost test]# ls [root@localhost test]#

 [root@localhost test]# ls -i 1653280 a b

 1653352 -abc [root@localhost test]# find . -inum 1653280 ./a b [root@localhost test]# find . -inum 1653280 -exec rm {} \; [root@localhost test]# ls -abc

  查 找inittab 查 找并 显示 详细 信息

 进 入test 显 示当 前目录

 显 示当 前目 录下 的文件

 查 找a.soft文件

 查 找a.soft文 件并 删除 a.soft已 删除

  查 找inittab并 查看 详细 信息

 提 示的 查看(为了 试验,真正 用在rm)

 查 找init 开头,文件 类型 是二 进制 文件 的详 细信息

 一 些奇 葩的 创建 文件

 这 时候 想要 删除 文件 就会 出问题

  我 们知 道创 建过 程的 时候 可以 这样 删除,如果 不知 道呢?

 查找 i节点

 . 表 示当 前目录,

 即/test

 根据 i节 点删除,万能

 20 locate list files in databases locate [搜索关键字] locate file 可以查找所有跟 file 相关的文件,速度快 locate 指令是根据数据库查找的,如果数据库中文件没有更新,那么就会找不到。

 定 期updatedb,则可 以 直 接locate 找到

 21 updatedb update the slocate database 建立整个系统目录文件的数据库

  22 grep

 grep [指定字串][源文件] 在文件中搜寻字串匹配的行,并输出 [root@localhost test]# grep tftp /etc/services tftp

 69/tcp tftp

 69/udp tftp-mcast

 1758/tcp tftp-mcast

 1758/udp mtftp

  1759/udp subntbcst_tftp

 247/tcp

  # SUBNTBCST_TFTP subntbcst_tftp

 247/udp

  # SUBNTBCST_TFTP etftp

  1818/tcp

  查 找 和tftp 相关的行

 # Enhanced Trivial File Transfer Protocol etftp

  1818/udp

 # Enhanced Trivial File Transfer Protocol tftps

  3713/tcp

 # TFTP over TLS tftps

  3713/udp

  # TFTP over TLS

  1.4 帮助命令

 帮助命令

 23 man manual man [命令或配置文件] 获得帮助信息 命令和配置文件相同时优先查看命令的帮助 帮助分很多种,其中第一种是命令,第五种是配置文件,当名字相同时通过 man 5 来查看配置文件

 man ls

 使用 more 查看,空格翻页,enter下一行

 [root@localhost test]# man 5 passwd

  passwd 可 以修 改 用 户 密码, 和一个配置文件同名 24 info information info [任何关键字] 获取帮助信息

  25 whatis

 whatis apropos[任何关键字] 获得索引的简短说明信息 [root@localhost test]# whatis whereis whereis

 (1)

 - locate the binary, source, and manual page files for a command

  26 help

 命令 –help help 内置命令 [root@localhost test]# ls --help

 1.5 压缩解压命令 windows 可解压 Linux 所有,但相反不可 共同:zip 但压缩比不高,如果小文件建议使用 gzip 和 bzip2 只能压缩文件且不保留源文件,但后者-k 可以保留 27 gzip GNU zip gzip 选项[文件] 压 缩 后 文 件 格式: .gz 只能压缩文件,不能压缩目录 不保留源文件

 解压缩:

 gunzip 压缩文件 gzip –d 压缩文件

  [root@localhost test]# ls etc

 issue

 lost+found

 newfile [root@localhost test]# gzip newfile [root@localhost test]# ls etc

 issue

 lost+found

 newfile.gz

 不能压缩目录 [root@localhost test]# mkdir newdir [root@localhost test]# ls etc

 issue

 lost+found

 newdir

 newfile.gz [root@localhost test]# gzip newdir gzip: newdir is a directory – ignored

 解压缩 [root@localhost test]# gzip -d newfile.gz [root@localhost test]# ls etc

 issue

 lost+found

 newdir

 newfile [root@localhost test]# gzip newfile [root@localhost test]# ls etc

 issue

 lost+found

 newdir

 newfile.gz [root@localhost test]# gunzip newfile.gz [root@localhost test]# ls etc

 issue

 lost+found

 newdir

 newfile

  28 tar

 打包目录 [root@localhost test]# ls

 tar 选项[cvf][目录]

  -c 产生.tar 打包文件

  -v 显示详细信息

  -f 指定压缩后的文件名

  -z 打包同时压缩 压 缩 后 文 件 格式:

 .tar.gz

  解压缩

  -x 解包.tar 文件

  -v 显示详细信息

  -f 指定解压文件

  -z 解压缩 etc

 issue

 lost+found

 newdir

 newfile [root@localhost test]# tar -zcf newdir.tar.gz newdir [root@localhost test]# ls etc

 issue

 lost+found

 newdir

 newdir.tar.gz

 newfile

 当没有后缀,或者不知道文件类型 [root@localhost test]# tar -zcf new newdir [root@localhost test]# ls -l new -rw-r--r-- 1 root root 113 Feb 26 14:56 new [root@localhost test]# file new new: gzip compressed data, from Unix, last modified: Thu Feb 26 14:56:08 2015

 先打包再压缩 [root@localhost test]# tar -cf newdir2.tar newdir [root@localhost test]# gzip newdir2.tar [root@localhost test]# ls etc

 issue

 lost+found

 new

 newdir

 newdir2.tar.gz

 newdir.tar.gz

 newfile

 解压缩 [root@localhost test]# ls etc

 issue

 lost+found

 new

 newdir

 newdir2.tar.gz

 newdir.tar.gz

 newfile [root@localhost test]# tar -zxf newdir.tar.gz [root@localhost test]# ls etc

 issue

 lost+found

 new

 newdir

 newdir2.tar.gz

 newdir.tar.gz

 newfile [root@localhost test]# tar -zxf newdir2.tar.gz [root@localhost test]# ls

  tar –zcf 打包后的名字 要打包的文件名

  注:打包后的文件名可以没有后缀,但是为了辨识方便,养成良好习惯

  看起来像是文本文件,但这种使用 cat是无法查看的 使用 file 查看文件了性

 etc

 issue

 lost+found

 new

 newdir

 newdir2.tar.gz

 newdir.tar.gz

 newfile [root@localhost test]# rm -rf newdir [root@localhost test]# ls etc

 issue

 lost+found

 new

 newdir2.tar.gz

 newdir.tar.gz

 newfile [root@localhost test]# tar -zxf new [root@localhost test]# ls etc

 issue

 lost+found

 new

 newdir

 newdir2.tar.gz

 newdir.tar.gz

 newfile 29 zip

 zip 选项[-r][压缩后文件名称][文件或目录] -r

 压缩目录 功能:压缩文件或目录 压 缩 后 文 件 格式:.zip 保留源文件 范 例 :

  zip services.zip /etc/services

  zip –r test.zip /test

 压缩目录

  30 unzip

 unzip [压缩文件] 功能:解压.zip 的压缩文件 范例:unzip test.zip

  31 bzip2

 bzip2 选 项 [-k][ 文件]

 -k 产 生压缩文件后保留源文件 不加 k 则和 gzip 相

 同,但压缩比很高 功能:压缩文件 压 缩 后 文 件 格式:

 .bz2 范例:

 bzip2 –k file1 32 bunzip2

 bunzip2 选 项[-k][压缩文件]

  -k 解压缩后保留源文件 功能:解压缩 范例:

 bunzip2 –k file1.bz2

 1.6 网络通信指令 33 write

 当用户登陆的时候,可以进行信息交互 , 以 以 Ctrl+D 作为结束

 [root@localhost test]# write helen this is a test,hello [helen@localhost ~]$

  这里使用SSH 登陆 Message from root@localhost.localdomain on pts/1 at 15:37 ... this is a test,hello

 34 wall

 登陆的所有人都会收到

  35 ping

 测试网络连通性 ping 192.168.1.1

 和 windows 不同的是,Linux 如果不 ctrl+c 终止就会一直进行下去

 注意:

  丢包率:packet loss

  有时候数据存取不完整,检查网络设备。

 改变 ping 的次数 [root@localhost test]# ping -c 3 192.168.0.141 像 windows 一样定义ping 的次数 PING 192.168.0.141 (192.168.0.141) 56(84) bytes of data. 64 bytes from 192.168.0.141: icmp_seq=1 ttl=128 time=0.568 ms 64 bytes from 192.168.0.141: icmp_seq=2 ttl=128 time=0.456 ms 64 bytes from 192.168.0.141: icmp_seq=3 ttl=128 time=0.451 ms

 --- 192.168.0.141 ping statistics ---

  ping 上万次

  如果 ping 不通,先 ping 本机,证明自己没问题,ping 127.0.0.1 回环地址,检查自己的设置。

 不通的时候不一定是网络不通,有可能防火墙等屏蔽了,不要武断的判断网络有问题。

 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 0.451/0.491/0.568/0.059 ms

 默认 64 字节,改变数据包大小 [root@localhost test]# ping -s 300 192.168.0.141 PING 192.168.0.141 (192.168.0.141) 300(328) bytes of data. 308 bytes from 192.168.0.141: icmp_seq=1 ttl=128 time=0.517 ms 36 ifconfig

 ifconfig 选 项[-a][网卡设备标识]

 -a 显示所有网卡信息 功能:查看网络设置信息 范例:ifconfig –a eth0 本 机 物 理网卡 lo 回环网卡地址 ifconfig eth0 192.168.9.4 修改 ip,仅本次生效 Linux 基本:只有写入配置文件才会永久生效

  37 shutdown 只 有root 可以执行

 [root@localhost test]# shutdown -h now 最好不要使用,立马关机 关机前 wall 一下大家保存,然后shutdown,有缓冲时间

 38 reboot 重启,root ,服务器通常不随便开关

  1.7 Shell 应用技巧 通常使用 bash,查看 cat /etc/shells 命令补全 按 按 Tab ,如果是唯一的则直接补全,如果不唯一则按两下之后出现候选命令 清屏 clear 或者 Ctrl+l 清除光标前字符 Ctrl+u 查看以前的命令 history !123 执行第 123 条命令 方向键上下可以找到历史命令 定义别名

 alias copy=cp alias xrm=”rm –r” 组合要括起来

 清除别名:unalias copy [root@localhost test]# alias alias cp="cp -i" alias l.="ls -d .* --color=tty" alias ll="ls -l --color=tty" alias ls="ls --color=tty" alias mv="mv -i" alias rm="rm -i" alias which="alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde" 这是系统定义的别名

 [root@localhost test]# alias copy=cp

  自定义别名 [root@localhost test]# copy /etc/inittab .

 拷贝到当前目录 [root@localhost test]# ls etc

 issue

  new

  newdir2.tar.gz

 newdir.zip

 newfile.bz2 inittab

 lost+found

 newdir

 newdir.tar.gz

  newfile

  输 入、输出重定向

 通标准 I/O 一样,Shell 对于每一个进程预先定义 3 个文件描述字(0,1,2),分别对应:

 0 (STDIN) 标准输入 1 (STDOUT) 标准输出 2 (STDERR) 标准错误输出

 > 或>> 输出重定向 :

 范例:

 ---------------------------------------》 》》

  [root@localhost test]# ls -l /tmp >tmp.msg [root@localhost test]# date > /tmp.msg [root@localhost test]# cat /tmp.msg Thu Feb 26 16:49:57 CST 2015 原内容被覆盖

 使用>>追加进去 ...

推荐访问:学习笔记 Linux