Lamp兄弟连 linux课程
(自己整理 完整版)第三讲LINUX常用命令
兄弟连34期 房子 qq:2089764 第三讲 LINUX常用命令
文件命名规则
1、 除了/之外,所有的字符都合法 。
(因为linux中‘/’用来表示根目录或作为目录结构的分隔符。) 2、 有些字符最好不用,如空格,制表符,退格符和字符@ # $ & () -等。
(windows有个8.3命名规则:8位以内的文件名;3位后缀,如exe、inf等。在linux
中无此要求,linux中大多数文件都没有后缀名,文件名也可以很长。) 3、 避免使用.作为普通文件名的第一个字符,在LINUX下以.开头是隐藏文件。
(linux中想隐藏一个文件,如abc,只有一个方法,改名为.abc。) 4、 linux下大小写敏感。终端命令都是小写的。
(因为C语言是严格区分大小写的。)
命令格式
有的命令后可带选项和参数;有的命令不能接参数,如clear。 命令格式:命令 [,选项] [参数]
例: ls -la /etc 或写成:ls -l -a /etc 命令用来指定干什么;选项是做细微的调整;
参数用来指定操作的对象,如目录、文件、用户、用户组、进程等。
:1、个别命令使用不遵循此格式。
2、选项和参数都可以是多个。
兄弟连34期 房子 qq:2089764
3、多个命令可以写在一起,不用考虑顺序;
如: ls –d - l /etc可写成ls -ld /etc或ls -dl /etc。
4、两个特殊目录 . 和 .. 分别表示当前目录和当前目录的父目录。
(在windows的dos下也有这两个目录,在dos下要进入当前目录的父目录,直接cd.. 就可以,中间不用加空格;但在linux中要加空格,linux对格式要求比较严格。)
小技巧
1. 清终端屏幕:clear或ctrl+l (L)。
2. 终止一个命令:ctrl+c。
3. 清除光标所在命令行之前的所有字符:ctrl+u。
4. “.”表示当前目录。 “..”表示上一级目录。
5. ../../连续跳2层。
6. 当前目录下的文件可不加/。
7. *号表示当前目录所有东西。
8. #最高权限,$普通权限。
9. 刚打开终端时就有一个当前工作目录,该目录是由管理员在创建用户时指定的:
这个最初的目录称为用户的主目录(home directory),主目录可用“~”表示。
回到主目录:cd ~ 或直接cd。
兄弟连34期 房子 qq:2089764 课程大纲
文件处理命令
权限管理命令
文件搜索命令
帮助命令
压缩命令
网络通信命令
系统关机命令
Shell应用技巧
重点为六大部分。
文件处理命令:ls
英文原意:list
所在路径:/bin/ls
执行权限:所有用户
功能描述:显示目录文件
语法: ls 选项[-ald] [文件或目录]
-a 显示所有文件,包括隐藏文件。 (a:all)
-A显示所有文件,包括隐藏文件,但不显示 . 和 .. 。
-l 详细信息显示。(l:long)
-d 查看目录属性。(d:directory)
兄弟连34期 房子 qq:2089764
linux中,命令所在路径的不同意味着执行权限的不同。
普通用户可以执行的大多数命令 所在的路径为:/bin 或 /usr/bin。
(bin :binary,二进制文件。usr : user。
每个命令就是一个程序,每个程序就是一个文件。)
root可以执行的大多数命令 所在的路径为:/sbin或/usr/sbin。
(sbin :super binary。)
[root@localhost ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog [root@localhost ~]# ls /
bin dev home lost+found misc net proc sbin srv test usr web boot etc lib media mnt opt root selinux sys tmp var [root@localhost ~]# ls -a /
. .autorelabel dev lib misc opt sbin sys usr .. bin etc lost+found mnt proc selinux test var .autofsck boot home media net root srv tmp web
兄弟连34期 房子 qq:2089764
有些linux系统中,在终端输入ls时,可以看到不同的文件类型会标记成不同的颜色。最常见的类型有两种:二进制文件(黑色);目录(蓝色)。有的系统不会标记颜色。 [root@localhost ~]# ls -l /
总计 160
drwxr-xr-x 2 root root 4096 02-05 00:22 bin drwxr-xr-x 3 root root 4096 02-05 22:35 boot drwxr-xr-x 11 root root 4400 02-07 21:18 dev drwxr-xr-x 109 root root 12288 02-07 21:18 etc drwxr-xr-x 3 root root 4096 02-05 00:12 home drwxr-xr-x 13 root root 4096 02-05 00:21 lib drwx------ 2 root root 16384 02-05 08:30 lost+found drwxr-xr-x 2 root root 4096 02-07 21:18 media drwxr-xr-x 2 root root 0 02-07 21:18 misc drwxr-xr-x 3 root root 4096 02-05 21:48 mnt drwxr-xr-x 2 root root 0 02-07 21:18 net drwxr-xr-x 2 root root 4096 2010-01-27 opt dr-xr-xr-x 141 root root 0 02-07 21:16 proc drwxr-x--- 18 root root 4096 02-07 21:19 root drwxr-xr-x 2 root root 12288 02-05 21:48 sbin drwxr-xr-x 2 root root 4096 02-05 22:29 selinux drwxr-xr-x 2 root root 4096 2010-01-27 srv drwxr-xr-x 11 root root 0 02-07 21:16 sys drwxr-xr-x 4 root root 4096 02-05 21:46 test drwxrwxrwt 14 root root 4096 02-07 21:19 tmp drwxr-xr-x 14 root root 4096 02-05 22:31 usr drwxr-xr-x 25 root root 4096 02-05 22:41 var drwxr-xr-x 3 root root 4096 02-05 08:30 web 列出的是根目录下每个子目录的信息。
drwxr-xr-x,第一个字符表示文件类型:
“-”打头的是普通的二进制文件;
“d”打头的是目录(directory);
“l”打头的是软连接文件(link,即快捷方式);
“p”打头的是管道(pipe)。
兄弟连34期 房子 qq:2089764
ln 用来建立连接。mkfifo 用来建立管道。
drwxr-xr-x,后九个字符表示权限。
权限: r - 读read;w - 写write; x - 执行execute; “-” - 没有权限。
兄弟连34期 房子 qq:2089764
d rwx r-x r-x : rwx - 文件所有者(user)对该文件的访问权限;
r-x – 所属组(group)对该文件的访问权限;
r-x - 其他人(others)对该文件的访问权限。
文件所有者:谁创建了该文件谁就是它的所有者,所有者是可以转让的。 【linux系统中,大多数时候用数据块(block)作为文件大小的单位; 1block=512字节=0.5KB;
block 是linux中存储数据的最小单位,如一个1
节的文件,也要占一个数据块; 数据块是可以调整大小的。】
如需列出根目录自己的信息,需要加选项d。
[root@localhost ~]# ls -ld /
drwxr-xr-x 25 root root 4096 02-07 21:18 /
ls -r 表示按字母逆序排列。
兄弟连34期 房子 qq:2089764
ls –t 表示按修改时间(time)来排列。
ls -R 表示列出从指定目录开始的整棵目录树,默认是当前目录;即列出目录以及子目录下的所有内容。
兄弟连34期 房子 qq:2089764
文件处理命令:cd
英文原意:change directory 命令所在路径:shell 内置命令 执行权限:所有用户
功能描述:切换目录
语法:cd [目录]
例: cd / 切换到根目录
cd .. 回到上一级目录
兄弟连34期 房子 qq:2089764
文件处理命令:pwd
英文原意:print work directory
命令所在目录:/bin/pwd
执行权限:所有用户
功能描述:显示当前所在的工作目录
语法:pwd
绝对路径(从根目录出发,以“/”开始的就是绝对路径) 路径
相对路径(从当前所在目录出发,不以“/”开始的就是相对路径)
表示绝对路径时:第一个“/”表示根目录;以后的“/”只起分隔符的作用。
用 ~ + 用户名,表示另一用户的主目录。
文件处理命令:touch
命令所在路径:/bin/touch
功能描述:创建空文件
语法:touch [文件名]
例子: touch newfile
兄弟连34期 房子 qq:2089764
touch 文件名1 文件名2 ……
“>”(输出重定向):将左边命令产生的输出写到右边的文件中去;会清空文件中原有内容。 文件查看命令:cat more head tail
命令more 是分屏查看文件的内容,使用回车键可以查看下一行内容,使用空格键查看下一屏内容,按q退出。
命令head和tail查看文件的开头几行和结尾几行。如 head -5。
“>>”(追加):不会覆盖原有内容,而是追加到右边文件的末尾。
兄弟连34期 房子 qq:2089764
文件处理命令:mkdir
英文原意:make directory
命令所在路径:/bin/mkdir
执行权限:所有用户
功能描述:创建新目录
语法:mkdir [目录名]
mkdir 目录名1 目录名2 … ;都是同层次的目录。 目录名1存在的前提下,可以:mkdir 目录名1/目录名2;以此类推。 如果需要一次建立多层目录,需要使用-p选项。
兄弟连34期 房子 qq:2089764
文件处理命令:cp
英文原意:copy
命令所在路径:/bin/cp
执行权限:所有用户
功能描述:复制文件或目录(复制目录需加r选项) 语法: cp -r [源文件或目录] [目的目录]
例: cp file1 file2 dir1 将文件file1 file2 复制到目录dir1。
cp –r dir1 dir2 将dir1下的所有文件及子目录复制到目录dir2。
如果希望能看到提示,可以用选项-i (interactive)。
兄弟连34期 房子 qq:2089764
目录没有提示,除非两个目录下面有文件名相同的文件。 只有输入“y”时才会覆盖。如需直接退出,可按ctrl+c。 文件处理命令:mv
英文原意:move
命令所在路径:/bin/mv
执行权限:所有用户
功能描述:移动文件、更名
语法:mv [源文件或目录] [目的目录]
例: mv file1 file3 将当前目录下文件file1更名为file3。
mv file2 dir2 将文件file2移动到目录dir2下。
mv dir1 dir2
如果目录dir2存在,就是将dir1目录移动到dir2目录下;
如果目录dir2不存在,就是将dir1目录更名为dir2。
mv dir1 dir2/ dir3
目录dir2必须存在;如果目录dir3存在,就是将dir1移到dir2下的dir3目录
下;如果目录dir3不存在,就是将dir1目录移动到dir2目录下,并改名为dir3。
兄弟连34期 房子 qq:2089764
[root@localhost test]# ls
lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib [root@localhost test]# touch abc
[root@localhost test]# ls
abc lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib [root@localhost test]# mv abc /tmp 或者mv abc /tmp/
[root@localhost test]# ls
lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
/ [root@localhost test]# ls /tmp 或者ls /tmp
abc scim-panel-socket:0-root gconfd-root scim-socket-frontend-root keyring-RgLzzb ssh-sqYTXr4175 mapping-root virtual-root.J89Q99 orbit-root vmware-config0 scim-bridge-0.3.0.lockfile-0@localhost:0.0 VMwareDnD scim-bridge-0.3.0.socket-0@localhost:0.0 vmware-file-mod0 scim-helper-manager-socket-root vmware-root
[root@localhost test]# mkdir dir1 dir2
[root@localhost test]# ls
dir1 dir2 lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib [root@localhost test]# touch dir1/f1 dir1/f2 dir2/f3 dir2/f4 [root@localhost test]# ls dir1 dir2
dir1:
f1 f2
dir2:
f3 f4
[root@localhost test]# mv dir1 dir2
[root@localhost test]# ls
dir2 lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib [root@localhost test]# ls dir2
dir1 f3 f4
[root@localhost test]# mv dir2 dir3
[root@localhost test]# ls
dir3 lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib 注意:只有移动一个文件或目录的时候才能更名。
兄弟连34期 房子 qq:2089764
文件处理命令:rm
命令英文原意:remove
命令所在路径:/bin/rm
执行权限:所有用户
功能描述:删除文件
语法:rm [-r] 文件名 【删除文件】
rm –r 目录名 【可以用来删除空目录和非空目录】
rmdir 空目录名 【只能用来删除空目录,用的极少】
-i (interactive)删除前先提示咨询用户。
-f (false)强制删除,不会出现提示,unix默认不会提示,linux因为有一
个别名的机制,所以需要咨询用户是否删除。 例子:rm file3 删除文件file3
rm -rf dir1 强制删除目录dir1(用的比较多)
一般不用rm –r dir1 ,因为对每个文件它都会提示用户是否删除,很麻烦,
直接加f选项,强制删除。
兄弟连34期 房子 qq:2089764
在移动、复制和删除命令中,如果希望看到提示,可以用选项-i(interactive)。
兄弟连34期 房子 qq:2089764
文件处理命令:cat(查看文件)
英文原意:concatenate and display files
命令所在路径:/bin/cat
执行权限:所有用户
功能描述:显示文件内容
语法:cat [文件名]
例子:cat /etc/issue
cat /etc/services cat命令适合查看一些文件内容不是很长的文件。如果文件较长,屏幕只能看到文件
最后部分的内容,不方便查看。
文件处理命令:more(查看文件)
命令所在路径:/bin/more
执行权限:所有用户
功能描述:分布显示文件内容
语法:more [文件名]
空格 或 f键 :显示下一页
回车(enter) :显示下一行
Q或q :退出
例子:more /etc/services
兄弟连34期 房子 qq:2089764 文件处理命令:head(查看文件)
命令所在路径:/bin/head
所有用户 执行权限:
功能描述:查看文件前几行
语法:head –num [文件名]
-num:显示文件的前num行。
例子:head -5 /etc/services 文件处理命令:tail(查看文件)
命令所在路径:/bin/tail
执行权限:所有用户
功能描述:查看文件后几行
语法:tail –num [文件名]
-num:显示文件的后num行。
-f : 动态显示文件内容(比如要查看日志信息,会显示最新的几条信息)。
例子:tail -30 /etc/services
兄弟连34期 房子 qq:2089764
文件处理命令:ln
英文原意:link
命令所有路径:/bin/ln
执行权限:所有用户
功能描述:产生链接文件
语法: ln 文件名 硬连接名 【创建硬连接】
ln -s 文件名 软连接名
【创建软连接;软连接也叫符号链接;-s表示soft或sign】
例子: ln -s /etc/issue /issue.soft(创建文件/etc/issue的软链接/issue.soft)
ln /etc/issue /issue.hard(创建文件/etc/issue的硬链接/issue.hard)
[root@localhost ~]# ln -s /etc/issue /issue.soft [root@localhost ~]# ls -l /etc/issue /issue.soft -rw-r--r-- 1 root root 47 2010-04-26 /etc/issue lrwxrwxrwx 1 root root 10 02-15 23:04 /issue.soft -> /etc/issue
[root@localhost ~]# ln /etc/issue /issue.hard [root@localhost ~]# ls -l /etc/issue /issue.hard -rw-r--r-- 2 root root 47 2010-04-26 /etc/issue -rw-r--r-- 2 root root 47 2010-04-26 /issue.hard [root@localhost ~]# cp /etc/issue /test/issue [root@localhost ~]# ls -l /etc/issue /test/issue -rw-r--r-- 2 root root 47 2010-04-26 /etc/issue -rw-r--r-- 1 root root 47 02-15 23:17 /test/issue
[root@localhost ~]# cp -p /etc/issue /test/issue.2 (-p选项可以保留原有的时间值)
[root@localhost ~]# ls -l /etc/issue /test/issue.2 -rw-r--r-- 2 root root 47 2010-04-26 /etc/issue -rw-r--r-- 1 root root 47 2010-04-26 /test/issue.2
软链接文件类型为l,文件的权限是rwxrwxrwx。软链接文件比较小,因为里面只保存了
兄弟连34期 房子 qq:2089764
目标文件的位置。软连接是通过快捷方式实现对另一个目标的操作。软链接可以跨文件系统生成。
硬链接不是快捷方式,但通过它也可以操作另一个目标。硬连接相当于源文件复制+同步更新。大多数文件硬链接数为1;目录是不能建立硬链接的,目录的硬链接数目有它自己专门的统计方式,用户无法操作它,所以不用管它。
硬链接还有个附带的作用,在linux中删除一个文件,只是把这个文件的硬链接的数量减一。只有硬链接数变为0的时候,文件才真正从磁盘删除。而软连接里面只保存了目标文件的位置,当删掉目标文件的时候,软连接文件也失去了访问磁盘数据的通路。 硬链接的文件之间没有主次关系,相互同等,不存在谁是谁的硬链接这一说。 而软连接有明确的方向,比如,a是b的软连接,a里面保存的就是b的路径。 [root@localhost ~]# cat /etc/issue CentOS release 5.5 (Final)
Kernel \r on an \m
[root@localhost ~]# cat /issue.soft CentOS release 5.5 (Final)
Kernel \r on an \m
[root@localhost ~]# cat /issue.hard CentOS release 5.5 (Final)
Kernel \r on an \m
[root@localhost ~]# echo "this is a test" >> /etc/issue
[root@localhost ~]# cat /etc/issue /issue.soft /issue.hard
CentOS release 5.5 (Final)
Kernel \r on an \m
this is a test
CentOS release 5.5 (Final)
Kernel \r on an \m
this is a test
CentOS release 5.5 (Final)
Kernel \r on an \m
兄弟连34期 房子 qq:2089764
this is a test
[root@localhost ~]# rm /etc/issue
rm:是否删除 一般文件 “/etc/issue”? y
[root@localhost ~]# cat /issue.soft cat: /issue.soft: 没有那个文件或目录
[root@localhost ~]# cat /issue.hard CentOS release 5.5 (Final)
Kernel \r on an \m
this is a test
[root@localhost ~]#
linux内核只认识数学,不认识字母;即linux中任何内核要处理的对象都必须有一个数字标识,例如每个文件都有一个数字标识,这个文件数字标识就叫i节点(inode),此外用户有UID、进程有PID等等都是数字标识。每个文件必须对应一个i节点,但每个节点不一定指向一个文件,可用命令ls -i查看。硬连接和原文件有相同的i节点,相当于一个节点同时映射到多个文件,所以可以同步更新,而且删除一个文件只是减掉一个映
兄弟连34期 房子 qq:2089764
射。同时要求文件必须在同一个物理设备上,不能跨文件系统,不能跨分区。
[root@localhost test]# touch a
[root@localhost test]# ln -s a a.soft
[root@localhost test]# ln a a.hard
[root@localhost test]# ls -i
13 a 14 a.soft 12 VMwareTools-8.8.4-743747.tar.gz
13 a.hard 11 lost+found 29250 vmware-tools-distrib [root@localhost test]# ln a /a.hard.2
ln: 正在创建指向“a”的硬链接“/a.hard.2”: 无效的跨设备连接
兄弟连34期 房子 qq:2089764
权限管理命令:chmod
英文原意:change the permissions mode of a file 命令所在路径:/bin/chmod
执行权限:所有用户
功能描述:改变文件或目录权限
语法:chmod u/g/o/a =/+/- r/w/x 文件名
u/g/o/a : 所有者/所属组/其他用户/所有用户;
=/+/- :最终权限/增加权限/减去权限;
r/w/x :读取权限/写入权限/执行权限。
兄弟连34期 房子 qq:2089764
绝对模式下,用三位数字表示,三个数字都是0、1、2、4的和, 其中r - 4 ;w - 2;x - 1;- - 0。和最大为7,最小为0,其实就是八进制。 比如是600的话,所有者就有读与写权限,同组者与其他人没有任何权限。 专业的写法600应该写成0600,0打头表示是8进制。
兄弟连34期 房子 qq:2089764
chmod ugo+x z,也可以写成:
文件目录的权限总结
代表字符 权限 对文件的含义 对目录的含义
r 读权限 可以查看文件内容 可以列出目录中内容
(ls) (cat、head、more、tail)
w 写权限 可以修改文件内容 可以在目录中创建、删除文件
(echo、vi) (touch、mkdir、rm)
x 执行权限 可以执行文件 可以进入目录
(cd) (命令 脚本)
su - 用户名:切换用户;从管理员root切换到普通用户,不需要输密码,但从普通用户切换到另外一个普通用户或切换到root,是需要密码的。
[root@localhost test]# mkdir /prem
[root@localhost test]# ls -ld /prem
drwxr-xr-x 2 root root 4096 02-16 11:29 /prem [root@localhost test]# touch /prem/newfile [root@localhost test]# ls -l /prem/newfile -rw-r--r-- 1 root root 0 02-16 11:29 /prem/newfile [root@localhost test]# chmod 777 /prem/newfile [root@localhost test]# ls -ld /prem
drwxr-xr-x 2 root root 4096 02-16 11:29 /prem [root@localhost test]# ls -l /prem/newfile -rwxrwxrwx 1 root root 0 02-16 11:29 /prem/newfile 问普通用户是否能删除newfile文件,【不能】
[root@localhost test]# su - helen
[helen@localhost ~]$ rm /prem/newfile rm: 无法删除 “/prem/newfile”: 权限不够
[helen@localhost ~]$ exit
兄弟连34期 房子 qq:2089764
[root@localhost test]# chmod 777 /prem [root@localhost test]# chmod 644 /prem/newfile [root@localhost test]# ls -ld /prem drwxrwxrwx 2 root root 4096 02-16 11:29 /prem [root@localhost test]# ls -l /prem/newfile -rw-r--r-- 1 root root 0 02-16 11:29 /prem/newfile
[root@localhost test]# su - helen [helen@localhost ~]$ rm /prem/newfile
/prem/newfile”? y rm:是否删除有写保护的 一般空文件 “
[helen@localhost ~]$ ls /prem
[helen@localhost ~]$ exit
[root@localhost test]#
所以删除文件必需是对文件所在的目录有写的权限。
linux中,基本上所有的目录都有r、x权限。
问:谁有可以改变文件中的权限,
答:所有者、root。
权限管理命令:chwon
英文原意:change file ownership
命令路径:/bin/chown
执行权限:所有用户
功能描述:改变文件或目录的所有者
语法:chown [用户] [文件或目录]
例子:chown nobody file1 改变文件file1的所有者为nobody nobody:是linux系统自带的用户,类似于windows中的guest账号。
兄弟连34期 房子 qq:2089764 [root@localhost test]# touch a
[root@localhost test]# ls
a lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
[root@localhost test]# ls -l a
-rw-r--r-- 1 root root 0 02-16 11:52 a [root@localhost test]# chown helen a [root@localhost test]# ls -l a
-rw-r--r-- 1 helen root 0 02-16 11:52 a [root@localhost test]# chown nobody a [root@localhost test]# ls -l a
-rw-r--r-- 1 nobody root 0 02-16 11:52 a
linux中添加用户比较简单,分两步:用useradd + 用户名;passwd + 用户名,设置密码。 [root@localhost test]# useradd samlee [root@localhost test]# passwd samlee Changing password for user samlee. New UNIX password:
BAD PASSWORD: it is based on a dictionary word Retype new UNIX password:
passwd: all authentication tokens updated successfully.
[root@localhost test]#
权限管理命令:chgrp
命令原意:change file group ownership
命令所在路径:/bin/chgrp
功能描述:改变文件或目录的所属组
语法:chgrp [用户组] [文件或目录]
例子:chgrp adm file 改变文件file的所属组为adm
adm:系统自带的组。
[root@localhost test]# chgrp adm a [root@localhost test]# ls -l a
-rw-r--r-- 1 nobody adm 0 02-16 11:52 a
兄弟连34期 房子 qq:2089764 权限管理命令:umask
所在路径:/bin/umask
执行权限:所有用户
功能描述:显示、设置文件的缺省权限
语法:umask [-S]
-S 以rwx形式显示新建文件或目录缺省权限
例子:umask
umask –S
[root@localhost test]# touch testfile [root@localhost test]# ls -l testfile -rw-r--r-- 1 root root 0 02-16 12:01 testfile 【是默认权限】
[root@localhost test]# mkdir newdir [root@localhost test]# ls -ld newdir drwxr-xr-x 2 root root 4096 02-16 12:01 newdir 【是默认权限】
[root@localhost test]# umask
0022
[root@localhost test]# umask -S u=rwx,g=rx,o=rx
[root@localhost test]#
显示数字为0022。
0----表示特殊权限位;
022----表示用户权限位,是权限掩码值(777-022=755,即默认创建目录的权限为rwxr-xr-x)。
linux有个基本的权限规则:由于安全因素,缺省创建的文件不能授予可执行权限,所以文件的默认缺省权限为755-111,644,即:rw-r--r--。
兄弟连34期 房子 qq:2089764 可以改变创建目录或文件的默认权限,但不建议改变。 如果设置为缺省为764则777-764=013 umask 013 即可。
兄弟连34期 房子 qq:2089764
文件搜索命令:which
命令所在路径:/bin/which
执行权限:所有用户
功能描述:显示系统命令所在目录
语法:which [命令名称]
范例:$ which ls
[root@localhost /]# which ls alias ls='ls --color=tty' 【命令对应的别名记录,敲ls时实际是输入ls --color=tty,所以有颜色标记】
/bin/ls 【绝对路径】
[root@localhost /]# which chmod /bin/chmod
[root@localhost /]#
文件搜索命令:whereis
命令所在路径:/usr/bin/where
执行权限:所有用户
功能描述:找到文件位置及其帮助文件所在位置
语法:whereis [命令名称]
范例:whereis useradd
[root@helen ~]# whereis useradd useradd: /usr/sbin/useradd /usr/share/man/man8/useradd.8.gz
[root@helen ~]# man /usr/share/man/man8/useradd.8.gz
用man命令显示帮助文件内容。
兄弟连34期 房子 qq:2089764
文件搜索命令:find
命令所在路径:/usr/bin/find
执行权限:所有用户
功能描述:查找文件或目录
语法:find [搜索路径] [选项] [搜索关键字]
范例:$ find /etc -name init 在目录/etc中查找文件init
$ find / -size +204800 在根目录下查找大于100MB的文件
$ find /home -user samless 在跟目录下查找所有者为samlee的文件
$ find /etc -ctime -1 在/etc下查找24小时内被修改过属性的文件和目录
$ find /etc -size +163840 -a -size -204800
在/etc下查找大于80MB小于100MB的文件
$ find /etc -name inittab -exec ls -l {} \;
在/etc下查找inittab文件并显示其详细信息
选项:
-name 根据文件名查找
find /etc -name init 【只查找名字与init完全匹配的文件,windows中则会找出包含init关键词的所有文件】
两个通配符:* 匹配任意字符,包括0个字符 ; , 匹配单个字符
find /etc -name init*
find /etc -name init???
find /etc -name *init*
兄弟连34期 房子 qq:2089764
-size根据文件大小查找
单位:block数据块 (512字节) 100MB=102400KB=204800 block
大于 +n
等于 n
小于 -n
-user 根据文件所有者查找
find /home -user samless -group 组名
按时间查看:-ctime、-atime、-mtime 【以天为单位】
-cmin、-amin、-mmin 【以分钟为单位】
c- change 改变;表示文件的属性被修改过,比如所有者、所属组、权限等被改过。 a- access 访问;被cat、more、vi查看或编辑过等。
m- modify修改;表示文件内容被修改过。
“-”表示之内,如一天为单位时,-1就表示一天之内。
“+”表示超过。
两组连接符之一:-a(and 逻辑与); -o(or 逻辑或)。
find /etc -name inittab -o -size +204800
-type 根据文件文件类型查找 find /etc -name init* -a -type f
d-目录 l-软链接 f-二进制文件
兄弟连34期 房子 qq:2089764
两组连接符之二: -exec(连接执行符);-ok(会询问确认)。
find …… -exec 命令 {} \;
{} 代表find查询的结果;
\表示转义符(即能够让一些符号与命令使用它本身的含义);
;结束符号。
[root@localhost test]# rm testfile
rm:是否删除 一般空文件 “testfile”? n 【会出现提示】
[root@localhost test]# which rm
alias rm='rm -i' 【因为实际是加了-i选项】
/bin/rm
[root@localhost test]# \rm testfile 【加转义符,就不再出现提示】
[root@localhost test]#
[root@localhost test]# find /etc -name inittab
/etc/inittab
[root@localhost test]# find /etc -name inittab -exec ls -l {} \; -rw-r--r-- 1 root root 1666 02-05 22:43 /etc/inittab
[root@localhost test]# find /etc -name inittab -exec ls -l {} \; -rw-r--r-- 1 root root 1666 02-05 22:43 /etc/inittab [root@localhost test]# find /etc -name inittab -ok ls -l {} \; < ls ... /etc/inittab > ? y
-rw-r--r-- 1 root root 1666 02-05 22:43 /etc/inittab [root@localhost test]#
[root@localhost test]# find /etc -name init* -a -type f -exec ls -l {} \; -rw-r--r-- 1 root root 28 2010-04-10 /etc/selinux/targeted/contexts/initrc_context
-rw-r--r-- 1 root root 1666 02-05 22:43 /etc/inittab -rw-r--r-- 1 root root 658 2009-09-29 /etc/initlog.conf -rw-r--r-- 1 root root 1299 2009-01-21 /etc/mail/spamassassin/init.pre -rw-r--r-- 1 root root 1068 2009-07-04 /etc/sysconfig/init -rwxr-xr-x 1 root root 5433 2009-07-04 /etc/sysconfig/network-scripts/init.ipv6-global
[root@localhost test]#
[root@localhost test]# touch "a b"
[root@localhost test]# ls
a b lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib [root@localhost test]# touch -- -a
兄弟连34期 房子 qq:2089764
[root@localhost test]# ls
-a a b lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
[root@localhost test]# rm -abc
rm:无效选项 -- a
请尝试执行“rm --help”来获取更多信息。
[root@localhost test]# rm a b
a” 失败: 没有那个文件或目录 rm: lstat “
rm: lstat “b” 失败: 没有那个文件或目录
[root@localhost test]# ls -i
14 -a 11 lost+found 29250 vmware-tools-distrib
13 a b 12 VMwareTools-8.8.4-743747.tar.gz [root@localhost test]# find . -inum 13 【‘.’ :表示在当前目录查找】
./a b
[root@localhost test]# find . -inum 13 -exec rm {} \; [root@localhost test]# ls
-a lost+found VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
说明:查找范围越小越好,尽量不要在根目录下查找;服务器不能随便查找,能不find就尽量不find;查找范围是当前目录时有可能出现问题。
文件搜索命令:locate
命令英文愿意:list files in databases
命令所在路径:/usr/bin/locate
执行权限:所有用户
功能描述:寻找文件或目录
语法:locate [搜索关键字]
说明:该命令在unix系统中一般不提供;该命令是在定期更新的文件数据库中查找文件,速度比find要快,但有时会搜索不到已存在的文件,所以需用updatedb命令更新数据库后再用locate命令。
范例:$ locate file 列出所有跟file相关的文件
兄弟连34期 房子 qq:2089764 文件搜索命令:updatedb
命令英文愿意:update the slocate database 命令所在路径:/usr/bin/update 执行权限:root
功能描述:建立整个系统目录文件的数据库
【linux系统有一个
任务,该任务会定期执行updatedb命令】
语法:updatedb
范例:#updatedb
文件搜索命令:grep
命令所在路径:/bin/grep
执行权限:所有用户
功能描述:在文件中搜索字串匹配的行并输出
语法:grep [指定字串] [源文件]
范例: # grep ftp /etc/services
兄弟连34期 房子 qq:2089764
帮助命令:man
命令英文愿意:manual
命令所在路径:/usr/bin/man
执行权限:所有用户
功能描述:获得帮助信息
语法:man [命令或配置文件]
范例: $man ls 查看ls命令的帮助信息
$ man services 查看配置文件services的帮助信息
说明:
1. man在弹出信息的浏览状态实际是调用more这条命令,按空格或f键--显示下一页,
按回车(enter)--显示下一行,按q键--退出。
2. man [配置文件] 获得配置文件的帮助无需路径,直接写配置文件的名称就可以。
# man services
# man /etc/services 错误写法
3. passwd即是命令,又有配置文件/etc/passwd,
问:man passwd到底是查看passwd命令的帮助,还是查看配置文件的帮助,
答:会优先查看命令的帮助。linux中帮助是分成很多种的,有的linux中可以用命令# man man查看到,分成一到九种。第一种是命令的帮助,第五种是配置文件的帮助。 帮助类型:1——命令帮助 5——配置文件帮助;默认帮助类型是1——命令帮助。 $man 1 passwd 查看passwd命令的帮助 参数1可以不写;
$jman 5 passwd 查看/etc/passwd配置文件的帮助。
兄弟连34期 房子 qq:2089764 帮助命令:info
命令英文愿意:information
命令所在路径:/usr/bin/info
执行权限:所有用户
功能描述:获得帮助信息
语法:info [任何关键字]
范例:$ info ls 查看ls指令的帮助信息
说明:unix中无此命令
帮助命令:whatis apropos
命令名称:whatis apropos
命令英文愿意:search the whatis database for strings
命令所在路径: /usr/bin/whatis apropos
/usr/sbin/makewhatis 执行权限:All User,All User,root
功能描述:获得索引的简短说明信息
语法:whatis [命令名称] 【查看命令最基本的用法】
apropos [任何关键词] 【查看所有包含该关键词的配置文件的索引信息】 范例:$whatis ls
$apropos fstab 相当于man -k
[root@localhost test]# whatis whereis whereis (1) - locate the binary, source, and manual page files for a command
兄弟连34期 房子 qq:2089764
[root@localhost test]# whatis ls
ls (1) - list directory contents
ls (1p) - list directory contents
[root@localhost test]# apropos fstab
endfsent [getfsent] (3) - handle fstab entries
fstab (5) - static information about the filesystems fstab-decode (8) - run a command with fstab-encoded arguments getfsent (3) - handle fstab entries
getfsfile [getfsent] (3) - handle fstab entries
getfsspec [getfsent] (3) - handle fstab entries
nfs (5) - nfs and nfs4 fstab format and options setfsent [getfsent] (3) - handle fstab entries
[root@localhost test]# man -k fstab
endfsent [getfsent] (3) - handle fstab entries
fstab (5) - static information about the filesystems fstab-decode (8) - run a command with fstab-encoded arguments getfsent (3) - handle fstab entries
getfsfile [getfsent] (3) - handle fstab entries
getfsspec [getfsent] (3) - handle fstab entries
nfs (5) - nfs and nfs4 fstab format and options setfsent [getfsent] (3) - handle fstab entries
当安装完某个软件后,它的帮助文档也会安装到系统的相应的存放帮助文档的目录中,
所以需要执行makewhatis来更新索引数据库,否则通过whatis和apropos可能无法搜
到相关的索引介绍。linux系统会定期生成whatis的数据库。
$makewhatis 建立whatis和apropos搜索使用的数据库,当使用这两个命令发生错误
时,就是whatis datebase没有建立。
帮助命令:help
语法:help [命令] 【查看shell内置命令的帮助】
[命令] --help 【获得命令的选项的主要信息】
范例:$ls –help
$help cd
兄弟连34期 房子 qq:2089764
shell实际也是一个软件,shell有很多种,每一种里面内置的命令有所不同。有些命令不
属于任何目录,是安装shell时自带的,这种命令在获得帮助的时候有些区别。
[root@localhost test]# man cd
BASH_BUILTINS(1) BASH_BUILTINS(1)
NAME
bash, :, ., [, alias, bg, bind, break, builtin, cd, command, compgen,
complete, continue, declare, dirs, disown, echo, enable, eval, exec,
exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let,
local, logout, popd, printf, pushd, pwd, read, readonly, return, set,
shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit,
umask, unalias, unset, wait - bash built-in commands, see bash(1)
BASH BUILTIN COMMANDS
Unless otherwise noted, each builtin command documented in this section
as accepting options preceded by - accepts -- to signify the end of the
………
告诉了我们以下命令都是当前shell的内置命令:
alias, bg, bind, break, builtin, cd, command, compgen,
complete, continue, declare, dirs, disown, echo, enable, eval, exec,
exit, export, fc, fg, getopts, hash, help, history, jobs, kill, let,
local, logout, popd, printf, pushd, pwd, read, readonly, return, set,
shift, shopt, source, suspend, test, times, trap, type, typeset, ulimit,
umask, unalias, unset, wait
用命令man cd列出的帮助文档并不是cd命令的帮助文档,而是所有bash内置命令的帮
助文档。如果仅想获得cd命令的帮助文档,可以使用help cd。如果有些命令使用man
命令查不到,它可能就是shell内置命令,可用man bash(默认是bash)查看哪些是bash
的内置命令。
[root@localhost test]# help cd
cd: cd [-L|-P] [dir]
Change the current directory to DIR. The variable $HOME is the
default DIR. The variable CDPATH defines the search path for
the directory containing DIR. Alternative directory names in CDPATH
are separated by a colon (:). A null directory name is the same as
the current directory, i.e. `.'. If DIR begins with a slash (/),
兄弟连34期 房子 qq:2089764
then CDPATH is not used. If the directory is not found, and the
shell option `cdable_vars' is set, then try the word as a variable
name. If that variable has a value, then cd to the value of that
variable. The -P option says to use the physical directory structure
instead of following symbolic links; the -L option forces symbolic links
to be followed.
兄弟连34期 房子 qq:2089764
压缩解压命令:gzip
命令英文愿意:GNU zip
命令所在路径:/bin/gzip
执行权限:所有用户
功能描述:压缩文件
语法:gzip选项 [文件]
压缩后文件格式:.gz
范例:$gzip service
说明:
1.不保留原文件。
2.只能压缩文件不能压缩目录。
3.解压文件用gunzip [压缩文件]或gzip -d [压缩文件]
[root@localhost test]# touch newfile [root@localhost test]# ls
lost+found newfile VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
[root@localhost test]# gzip newfile [root@localhost test]# ls
lost+found newfile.gz VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
[root@localhost test]# gunzip newfile.gz [root@localhost test]# ls
lost+found newfile VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
压缩解压命令:gunzip
命令英文愿意:GNU unzip
兄弟连34期 房子 qq:2089764
命令所在路径:/bin/gunzip
执行权限:所有用户
功能描述:解压缩.gz的压缩文件
语法:gunzip 选项 [压缩文件]
范例:$gunzip file1.gz
压缩解压命令:tar
命令所在路径:/bin/tar
执行权限:所有用户
功能描述:将目录打包成 .tar文件
语法:tar 选项[cvf] [打包或压缩后目录名称] [目录]
-c (creat)产生.tar打包文件,必选项
-v 显示详细信息,必选项
-f 指定压缩后的文件名
-z 打包同时用gzip压缩(有的unix系统不支持,就先打包后压缩)
-j 打包同时用bzip2压缩
压缩后文件格式:.tar.gz
范例: $tar -zcvf dir.tar.gz dir1 将目录dir1压缩成一个打包并压缩的文件dir.tar.gz。 [root@localhost test]# mkdir newdir [root@localhost test]# ls
lost+found newdir newfile VMwareTools-8.8.4-743747.tar.gz vmware-tools-distrib
[root@localhost test]# tar -zcf newdir.tar.gz newdir 也可以不写.tar.gz后缀,但最好写上,便于区分 [root@localhost test]# ls
lost+found newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz
newdir newfile vmware-tools-distrib
兄弟连34期 房子 qq:2089764
[root@localhost test]# ls -l newdir.tar.gz
-rw-r--r-- 1 root root 113 02-16 21:14 newdir.tar.gz
[root@localhost test]# tar -zcf new newdir 会误认为new是一个文本文件,用cat无法浏览或是乱码
[root@localhost test]# ls -l new
-rw-r--r-- 1 root root 113 02-16 21:19 new
什么文件类型 [root@localhost test]# file new 用file命令可以查看new是
new: gzip compressed data, from Unix, last modified: Sat Feb 16 21:19:47 2013 [root@localhost test]# ls
lost+found newdir newfile vmware-tools-distrib new newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz [root@localhost test]# tar -cf newdir.tar newdir
[root@localhost test]# ls
lost+found newdir newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz new newdir.tar newfile vmware-tools-distrib [root@localhost test]# gzip newdir.tar
gzip: newdir.tar.gz already exists; do you wish to overwrite (y or n)? y [root@localhost test]# ls
lost+found newdir newfile vmware-tools-distrib new newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz
tar命令解压缩语法:
-x 解包.tar文件
-v 显示详细信息
-f 指定解压文件
-z 用gunzip解压缩
-j 用 bunzip2解压缩
[root@localhost test]# ls
lost+found newfile vmware-tools-distrib newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz
[root@localhost test]# tar -zxf newdir.tar.gz (或先gunzip newdir.tar.gz 再tar –xf newdir.tar)
[root@localhost test]# ls
lost+found newdir.tar.gz VMwareTools-8.8.4-743747.tar.gz newdir newfile vmware-tools-distrib
兄弟连34期 房子 qq:2089764 压缩解压命令:zip
命令所在路径:/usr/bin/zip
执行权限:所有用户
功能描述:压缩文件或目录(-r)。保留原文件。 语法:zip 选项[-r] [压缩后文件名称] [文件或目录]
-r 压缩目录
压缩后文件格式:.zip
范例:$ zip services.zip /etc/services 压缩文件
$ zip -r test.zip /test 压缩目录
说明:.zip格式是Linux和Windows通用的默认支持的格式,不需要装软件就能解压。
压缩解压命令:unzip
命令所在路径:/usr/bin/unzip 执行权限:所有用户
功能描述:解压.zip的压缩文件
语法:unzip [压缩文件]
范例:$ unzip test.zip
压缩解压命令:bzip2
命令所在路径:/usr/bin/bzip2 执行权限:所有用户
功能描述:(是gzip的升级版),压缩文件,压缩比非常高
兄弟连34期 房子 qq:2089764 语法:bzip2 选项[-k] [文件]
-k 产生压缩文件后保留原文件 压缩后文件格式:.bz2
范例:$ bzip2 -k file1 产生file1.bz2并保留file1文件
$ bzip2 file1 产生file1.bz2不保留file1文件 压缩解压命令:bunzip2
命令所在路径:/usr/bin/bunzip2 执行权限:所有用户
功能描述:解压缩文件
语法:bunzip2 选项[-k] [文件]
-k 解压缩文件后保留原文件 范例:$ bunzip2 -k file1.bz2 解压file1.bz2并保留file1文件
$ bunzip2 file1.bz2 解压file1.bz2不保留file1文件
兄弟连34期 房子 qq:2089764
网络通信指令:write
命令所在路径:/usr/bin/write 执行权限:所有用户
功能描述:向另外一个用户发信息,以Ctrl+D作为结束。 语法:write [用户名](用户必须已经登录,否则收不到信息) 范例:$ write samlee
网络通信指令:wall
命令英文愿意:write all
命令所在路径:/urr/bin/wall
执行权限:所有用户
兄弟连34期 房子 qq:2089764 功能描述:向所有用户广播信息
语法:wall [message] [文件名]
范例:$ wall Happy New Year!
网络通信指令:ping
命令所在路径:/usr/bin/ping
执行权限:root
功能描述:测试网络连通性
语法:ping 选项 IP地址
-c (count)发icmp请求包的次数
-s I (size)发送icmp数据包的大小,默认是64字节
范例: # ping 192.168.1.1
兄弟连34期 房子 qq:2089764
按Ctrl+C终止。
即便得到icmp的应答包,也不能确定网络完全没问题,首先要观察延时的情况,另外要观察丢包率(packet loss)。
如果自己与对方的主机ping不通,就先ping一下自己的ip地址,如果能ping通自己的,就证明自己本机的ip网络设置没问题,可能是网络连接或网络设备有问题,也可能对方主机有问题,如果ping不通本机的ip地址,就ping回环地址127.0.0.1,能ping通就证明本机的TCP/IP协议安装没有问题。
即便自己与对方的主机ping不通,也不代表网络就是不通的,有可能是防火墙的问题,有可能是某个软件屏蔽了icmp请求包的应答等等,有很多种可能。
兄弟连34期 房子 qq:2089764
网络通信指令:ifconfig
命令所在路径:/usr/bin/ifconfig
执行权限:root
功能描述:查看网络配置信息
语法:ifconfig 选项[-a] [网卡设备标识]
-a 显示所有网卡信息,linux 系统中-a可以省略,unix不能。
范例:# ifconfig –a
# ifconfig eth0 192.168.0.105 配置eth0网卡地址为192.168.0.105
# ifconfig eth0:1 192.168.0.106 为eth0配置多个IP地址
[root@localhost test]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:70:FC
inet6 addr: fe80::20c:29ff:fe1f:70fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:72439 errors:0 dropped:0 overruns:0 frame:0
TX packets:109 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
兄弟连34期 房子 qq:2089764
RX bytes:4656388 (4.4 MiB) TX bytes:19116 (18.6 KiB)
Interrupt:67 Base address:0x2024
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2261 errors:0 dropped:0 overruns:0 frame:0
TX packets:2261 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:3903884 (3.7 MiB) TX bytes:3903884 (3.7 MiB)
[root@localhost test]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:70:FC
inet6 addr: fe80::20c:29ff:fe1f:70fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:72638 errors:0 dropped:0 overruns:0 frame:0
TX packets:109 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4670495 (4.4 MiB) TX bytes:19116 (18.6 KiB)
Interrupt:67 Base address:0x2024
eth0:是实际的物理网卡;
lo:回环地址的网卡。
[root@localhost test]# ifconfig eth0 192.168.9.6
[root@localhost test]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:1F:70:FC
inet addr:192.168.9.6 Bcast:192.168.9.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe1f:70fc/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:72842 errors:0 dropped:0 overruns:0 frame:0
TX packets:118 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:4682739 (4.4 MiB) TX bytes:21022 (20.5 KiB)
Interrupt:67 Base address:0x2024
[root@localhost test]#
但linux系统重启后,ip地址并没有保留成192.168.9.6,这是因为在linux中修改一
些设置,必须写入到相对应的配置文件中才会永久生效,用命令行做的更改只是临时在
本次会话中生效。
兄弟连34期 房子 qq:2089764
系统关机命令:shutdown 命令所在路径:/usr/bin/shutdown 执行权限:root
功能描述:关机
语法:shutdown
【如果只输入shutdown,会默认有个缺省设置,等待一段时间后才关机】
范例:# shutdown -h now 【马上关机】 系统关机命令:reboot
命令所在路径:/sur/bin/reboot 执行权限:root
功能描述:重启系统
语法:reboot
范例:# reboot
shell应用技巧
命令别名
输入/输出重定向
管道
命令连接符
命令替换符
兄弟连34期 房子 qq:2089764
[root@localhost test]# cat /etc/shells 【查看已安装的shell类型】 /bin/sh 【较老的unix缺省的是sh】
/bin/bash 【重要应用的shell】
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
【csh与ksh,在写脚本时比较倾向于C语言的语法,可以写一些功能更强大的脚本】 bash应用技巧
命令补齐
命令补齐允许用户输入文件名起始的若干个字母后,按
键补齐文件名。如果输入若干个字母后,按键没反应,可能是命令敲错了,也可能是可选的命令太多,此时再按键,就会列出所有的可选命令。
[root@localhost test]# um 【连按两次键】
umask umount.ecryptfs umount.nfs4
umount umount.ecryptfs_private
umount.cifs umount.nfs 命令历史
命令历史允许用户浏览先前输入的命令并重新调用它们,用history命令可以显示用过的命令,用 ~+历史命令号 就可执行执行过的某条命令。按方向键?和?可以查找以前执行过的命令。
兄弟连34期 房子 qq:2089764
$ history
$ !88 执行88号历史记录
命令别名
查看系统定义的别名信息:alias
[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
inittab newdir.tar VMwareTools-8.8.4-743747.tar.gz
lost+found newfile vmware-tools-distrib [root@localhost test]#
命令别名定义:alias 别名=命令或“命令组合”
范例: alias copy =cp
alias xrm = "rm -r"
兄弟连34期 房子 qq:2089764
删除别名:unalias copy
unix系统中没有别名的定义。
输入/输出重定向
同I/O一样,shell对于每个进程预先定义3个文件描述字(0、1、2)。
分别对应于:
0 (STDIN) 标准输入—键盘;
1 (STDOUT)标准输出—显示器;
2 (STDERR)标准错误输出—显示器。
输出重定向( >或>> 或写成 0>或0>>)
>: 将左边命令产生的输出写到右边的文件中去,会清空文件中原有内容。
>>: 不会覆盖原有内容,而是追加到右边文件的末尾。
[root@localhost test]# ls -l /tmp > /tmp.msg
[root@localhost test]# more /tmp.msg
总计 32
drwx------ 3 root root 4096 02-07 21:19 gconfd-root drwx------ 2 root root 4096 02-07 21:19 keyring-RgLzzb srwxr-xr-x 1 root root 0 02-07 21:19 mapping-root drwx------ 2 root root 4096 02-16 22:50 orbit-root -rw-r--r-- 1 root root 5 02-16 23:26 scim-bridge-0.3.0.lockfile-0@localhost:0.0
srwxr-xr-x 1 root root 0 02-16 14:25 scim-bridge-0.3.0.socket-0@localhost:0.0
srw------- 1 root root 0 02-07 21:19 scim-helper-manager-socket-root srw------- 1 root root 0 02-07 21:19 scim-panel-socket:0-root srw------- 1 root root 0 02-07 21:19 scim-socket-frontend-root drwx------ 2 root root 4096 02-07 21:19 ssh-sqYTXr4175 drwx------ 2 root root 4096 02-07 21:19 virtual-root.J89Q99 drwx------ 2 root root 4096 02-07 21:19 vmware-root [root@localhost test]# date > /tmp.msg
[root@localhost test]# cat /tmp.msg
兄弟连34期 房子 qq:2089764
2013年 02月 16日 星期六 23:42:11 CST
[root@localhost test]# ls -l /tmp >> /tmp.msg
[root@localhost test]# more /tmp.msg
2013年 02月 16日 星期六 23:42:11 CST
总计 32
drwx------ 3 root root 4096 02-07 21:19 gconfd-root drwx------ 2 root root 4096 02-07 21:19 keyring-RgLzzb srwxr-xr-x 1 root root 0 02-07 21:19 mapping-root drwx------ 2 root root 4096 02-16 22:50 orbit-root
-rw-r--r-- 1 root root 5 02-16 23:26 scim-bridge-0.3.0.lockfile-0@localhost:0.0
srwxr-xr-x 1 root root 0 02-16 14:25 scim-bridge-0.3.0.socket-0@localhost:0.0
srw------- 1 root root 0 02-07 21:19 scim-helper-manager-socket-root srw------- 1 root root 0 02-07 21:19 scim-panel-socket:0-root srw------- 1 root root 0 02-07 21:19 scim-socket-frontend-root drwx------ 2 root root 4096 02-07 21:19 ssh-sqYTXr4175 drwx------ 2 root root 4096 02-07 21:19 virtual-root.J89Q99
2 root root 4096 02-07 21:19 vmware-root drwx------
输入重定向 ( < 或写成 1<)
[root@localhost test]# wall
hello 【按ctrl+d 结束】
Broadcast message from root (pts/1) (Sat Feb 16 23:50:58 2013):
hello
[root@localhost test]# wall < /etc/issue.net
Broadcast message from root (Sat Feb 16 23:52:38 2013):
CentOS release 5.5 (Final)
Kernel \r on an \m
[root@localhost test]#
错误输出重定向 ( 2> )
2> :执行的命令出错时,将错误信息重定向到某文件,执行正确则什么都不干。
&> :正确和错误信息输出重定向
[root@localhost test]# ls /kdls
ls: /kdls: 没有那个文件或目录
兄弟连34期 房子 qq:2089764
[root@localhost test]# ls /kdls 2> ls.err
[root@localhost test]# cat ls.err
ls: /kdls: 没有那个文件或目录
[root@localhost test]# ls /tmp 2> ls.err
root scim-bridge-0.3.0.lockfile-0@localhost:0.0 scim-socket-frontend-root gconfd-
keyring-RgLzzb scim-bridge-0.3.0.socket-0@localhost:0.0 ssh-sqYTXr4175 mapping-root scim-helper-manager-socket-root virtual-root.J89Q99
orbit-root scim-panel-socket:0-root vmware-root [root@localhost test]# cat ls.err
[root@localhost test]#
管道
管道:将一个命令的输出传送给另一个命令,作为另一命令的输入。
使用方法:命令1|命令2|命令3„„|命令n
范例: $ ls -l /etc
$ ls -l /etc | more
$ ls -l /etc | grep init
$ ls -l /etc | grep init | wc -l 统计输出结果的数量
[root@localhost test]# ls -l /etc | grep init
lrwxrwxrwx 1 root root 11 02-05 22:30 init.d -> rc.d/init.d
-rw-r--r-- 1 root root 658 2009-09-29 initlog.conf
-rw-r--r-- 1 root root 1666 02-05 22:43 inittab
lrwxrwxrwx 1 root root 15 02-05 22:32 rc.sysinit -> rc.d/rc.sysinit
[root@localhost test]#
[root@localhost test]# wc -l /etc/services 【wc –l:统计文件内容的行数】
9249 /etc/services
[root@localhost test]# ls -l /etc | grep init | wc -l
4
[root@localhost test]#
兄弟连34期 房子 qq:2089764
命令连接符
;
【顺序格式--用;间隔的各命令按顺序依次执行,在执行多条命令花费时间较长时使用】
语法: cmd1;cmd2;cmd3
例子: $ date; pwd; ls
&&
【逻辑与—前后命令的执行存在逻辑与关系,只有在&&前的命令执行成功后,它后面的命令才被执行】 cmd1 && cmd2 命令1执行成功后再执行命令2
[root@localhost test]# write Mary < /home/samlee/love.txt && rm /home/samlee/love.txt
[root@localhost test]# ls && pwd
inittab ls.err newfile vmware-tools-distrib
lost+found newdir.tar VMwareTools-8.8.4-743747.tar.gz /test
[root@localhost test]# lskkk && pwd
bash: lskkk: command not found
[root@localhost test]#
||
【逻辑或—前后命令的执行存在逻辑或关系,只有在||前的命令执行失败后,它后面的命令才被执行】 cmd1 || cmd2 命令1执行失败后再执行命令2
[root@localhost test]# ls || pwd
inittab ls.err newfile vmware-tools-distrib
lost+found newdir.tar VMwareTools-8.8.4-743747.tar.gz [root@localhost test]# lskkk || pwd
bash: lskkk: command not found
/test
[root@localhost test]#
兄弟连34期 房子 qq:2089764
命令替换符
命令替换:将一个命令的输出作为另一个命令的参数。
格式为: 命令1 `命令2`
[root@localhost test]# which touch
/bin/touch
[root@localhost test]# ls -l /bin/touch -rwxr-xr-x 1 root root 42284 2010-03-01 /bin/touch [root@localhost test]# ls -l `which touch` -rwxr-xr-x 1 root root 42284 2010-03-01 /bin/touch [root@localhost test]#
兄弟连34期 房子 qq:2089764 其他命令
ps命令(process status 进程状态)
ps命令:当前正在运行的命令或程序。
每个命令就是一个程序,每个程序就是一个文件。
常用的shell类型:sh 、bash 、 csh。
CMD :就是command(命令)
PID:进程的编号id。
TTY:终端的编号,每个终端都有一个编号。
进程编号与终端编号都是唯一的。进程id每次运行都未必一样。 TIME:用的CPU时间。
切换shell:如ksh 、 csh 、 sh 、 tcsh等,谁最后运行谁就起作用。
兄弟连34期 房子 qq:2089764
退出shell的方法:exit,退出最后进的shell。
再exit,就会关闭终端。因为一个终端至少要有一个shell以便与用户交互。
兄弟连34期 房子 qq:2089764 管理员root可以指定要用的shell。管理员指定的shell叫做登录shell。 用exec命令可以切换shell。
telnet命令(连接远程终端)
用telnet可以连接远程终端。
退出连接:logout 或者ctrl + d 或者exit 退出终端。
兄弟连34期 房子 qq:2089764 passwd命令(更改密码)
用passwd可以更改密码。
/etc目录中有一个文件:passwd;
用户名:soft01。
密码:x(不让看);密码实际是加密后放在了/etc /shadow文件中。 前一个500:用户id。
后一个500:群组id。
:: :之间可以写用户的注释或说明。
主目录:/home/soft01。
登录shell:/bin/bash;bash也是一个文件,该文件的绝对路径。
查看用户的id,组id:
兄弟连34期 房子 qq:2089764 cal命令(日历显示)
也可以显示整年的日历:。 也可以显示指定的某个月:
echo命令(随意输出命令)
根目录下有两个特殊文件:/dev/tty,表示终端屏幕,也表示终端键盘。
/dev/null:空设备文件(黑洞文件),永远的空文件,往里面输入的东西直接丢弃。
兄弟连34期 房子 qq:2089764
1,3 :不是字节,而是设备的编号。
echo后面有多个连续空格的,结果只保留一个空格。想要保留所有空格,加引号。
兄弟连34期 房子 qq:2089764
今日单词
Directory 目录
List 列出 列表
Change 改变
Force 强制
Interactive 交互 互动 Modify 修改
Owner 所有者 属主 Group 组
Manual 手册
Binary 二进制
Built-in 内置
Invert-match 反转匹配 Verbose 详细输出 Extract 提取