5)vim 文本方式编辑文件
vim [filename] ##进入到命令模式命令模式不能编辑文件,如果要编辑文件要进入插入模式按"i"进入插入模式如果完成编辑,按“Esc”退出插入模式,输入":wq",保存退出vim [filename] 如果文件名字不存在,会自动创建vim 没有操作对象直接打开,编辑文件后要输入":wq [filename]"":q!"表示不保存,强制退出":wq!"表示强制保存并退出####################以下为自己总结,不一定正确:"i"表示进入插入模式"a"表示向右移动一个字符,并且进入插入模式"s"表示删除当前字符,并且进入插入模式"S"表示删除整行字符,并且进入插入模式"cc"表示删除整行字符,并且进入插入模式"C"表示从光标处删除到行尾,并且进入插入模式"o"表示向下插入一行,并且进入插入模式"X"表示向左删除一个字符"x"表示向右删除一个字符,如果后面没有字符,则向左删除一个字符"D"表示从光标处删除到行尾,如果后面没有字符,则向左删除一个字符"dd"表示删除整行"p"撤销上一次删除"u"撤销"R"替换"J"把行末回车变成空格"h"表示以字符为单位向左移动"l"表示以字符为单位向右移动"b"表示以单词为单位向左移动,光标停留在下一个单词的第一个字符上"B"表示以单词为单位向左移动,光标停留在下一个单词的第一个字符上"e"表示以单词为单位向右移动,光标停留在下一个单词的最后一个字符上"w"表示以单词为单位向右移动,光标停留在下一个单词的第一个字符上"k"表示跳转到上一行相同的位置"j"表示跳转到下一行相同的位置"gg"表示跳转到起始第一个有效字符上"H"表示跳转到起始第一个有效字符上"G"表示跳转到末尾"L"表示跳转到最后一行行首"K"表示man [当前字符]"ZZ"保存退出####################6)cd 切换工作目录cd [directory] ##切换工作目录到指定目录cd - ##切换工作目录到之前所在目录cd ~ ##切换到自己的家目录cd ~usernmae ##切换到指定用户家目录cd .. ##进入到当前目录的上级目录7)ls 列出目录或文件信息ls ##如果后面没有目标那么默认目标为当前目录ls direcory|filename ##列出文件或目录内容ls -d direcotry ##列出目录本身ls -l filename|dorectory ##列出文件或目录里面内容的属性ls -ld directory ##列出目录本身属性ls -a ##显示目录中的所有内容,包括以"."开头的隐藏文件ls -R ##第归显示目录中的内容####################例如:[root@foundation50 Desktop]# mkdir -p 111/222/[root@foundation50 Desktop]# touch 111/222/333[root@foundation50 Desktop]# touch 111/222/.444[root@foundation50 Desktop]# ls -a 111. .. 222 ##"."表示当前目录,".."表示当前目录上一级[root@foundation50 Desktop]# ls -a 111/222. .. 333 .444[root@foundation50 Desktop]# ls -aR 111111:. .. 222111/222:. .. 333 .444"ll" = "ls -l"l. ##显示当前目录下所有隐藏文件"l." = "ls -d .*"例如:[root@foundation50 Desktop]# cd 111/222[root@foundation50 222]# l.. .. .444[root@foundation50 222]# ls -d .*. .. .444####################8)cp 文件复制cp是新建文件的过程cp file file1 file2 ... directory ##把file file1 file2 复制到 directory中cp file test ##建立test文件模板为filecp -r directory direcotry1 ##复制目录####################[root@foundation50 Desktop]# touch file1[root@foundation50 Desktop]# touch file2[root@foundation50 Desktop]# mkdir 111/[root@foundation50 Desktop]# mkdir 222/[root@foundation50 Desktop]# cp file1 file2 111/ ##将法文件file1和文件file2复制到目录111中[root@foundation50 Desktop]# vim file1 ##输入一串字符,保存退出[root@foundation50 Desktop]# cp file1 file2 ##将文件file1中的内容复制到文件file2cp: overwrite ‘file2’? y ##是否覆盖?YES[root@foundation50 Desktop]# cp file1 test ##创建文件test,并复制文件file1中的内容[root@foundation50 Desktop]# cp -r 111/ 222/ ##将目录111整个复制进目录222中,即222/111/[file1,file2][root@foundation50 Desktop]# cp -r 111/ 333/ ##创建目录333,并将目录111的文件复制进来,即333/[file1,file2]####################9)mv 移动或重命名相同磁盘的mv是重命名,不同磁盘的mv是复制删除的过程mv file file1 direcotry ##移动file和file1到directory中mv 存在文件 不存在文件 ##重命名文件mv westos/linux . ##把westos中的linux移动到当前目录 .代表当前目录####################[root@foundation50 Desktop]# touch 111[root@foundation50 Desktop]# touch 222[root@foundation50 Desktop]# mkdir 123/[root@foundation50 Desktop]# mv 111 222 123/ ##将文件111和文件222移至目录123中[root@foundation50 Desktop]# mv 123/111 123/222 . ##将目录123中的文件111和文件222移至当前目录[root@foundation50 Desktop]# mv 111 333 ##将文件111重命名为文件333########################################4.正则表达式####################* ##匹配0到任意字符? ##匹配单个字符[[:alpha:]] ##匹配单个字母[[:lower:]] ##匹配单个小写字母[[:upper:]] ##匹配单个大写字母[[:digit:]] ##匹配单个数字[[:alnum:]] ##匹配单个数字或字母[[:punct:]] ##匹配单个符号[[:space:]] ##匹配单个空格{}表示不存在的或者存在的{1..9} ##1-9{a..f} ##a-f{1,3,5} ##135{a,c,e} ##a c e{1..3}{a..c} ##1a 2a 3a 2a 2b 2c 3a 3b 3c[]表示存在的[a-C] ##a或者A或者b或者B或者c或者C[a-c] ##a或者A或者b或者B或者c[abc] ##a或者b或者c[1-3] ##1或者2或者3[145] ##1或者4或者5[^abc]|[!abc] ##除了a并且除了b并且除了c~ ##当前用户家目录~username ##指定的用户家目录~+ ##当前目录~- ##当前目录之前所在目录. ##当前目录.. ##当前目录的上一级####################{}既能创建又能删除[]只能删除touch fi" "le ##表示创建了一个名为"fi le"的文件"cd ~-"等同于"cd -"################################################ 第三单元 ################################################1. man####################man ->>manual ##man是手册的缩写man man ##查看man命令的帮助man passwd ##查看passwd命令的帮助man的级别1 系统命令2 系统接口3 函数库4 特殊文件,比如设备文件5 文件6 游戏7 系统的软件包8 系统管理命令9 内核man 5 passwd ##查看/etc/passwd文件的帮助man 1 passwd ##查看passwd命令的帮助,1可以省略man页面的快捷方式上下键 ##向上或向下一行pageup|pagedown ##向下一个屏幕或者向上一个屏幕d|u ##向下或者向上半个屏幕G|gg ##跳转页面到man最下面或最上面/关键字 ##搜索关键字,关键字会高亮显示,n向下匹配,N向上q ##退出帮助页面####################"passwd --help" = "/bin/passwd --help"ll file ##查看文件的Modification time[root@foundation50 Desktop]# touch file[root@foundation50 Desktop]# touch -a file[root@foundation50 Desktop]# stat file File: ‘file’ Size: 0 Blocks: 0 IO Block: 4096 regular empty fileDevice: fd00h/64768d Inode: 404876853 Links: 1Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)Context: unconfined_u:object_r:admin_home_t:s0Access: 2016-09-29 16:51:50.418866685 +0800Modify: 2016-09-29 16:50:25.102868288 +0800Change: 2016-09-29 16:51:50.418866685 +0800 Birth: -[root@foundation50 Desktop]# ll file-rw-r--r--. 1 root root 0 Sep 29 16:50 file需求:让student用户在下次登陆时,强制更改密码[root@localhost Desktop]# man 5 shadow输入“/log”进行查找,找到需要的内容如下:date of last password change ##文中第三个字段 ...... The value 0 has a special meaning, which is that the user should change her pasword the next time she will log in the system. ......按“q”退出[root@localhost Desktop]# vim /etc/shadowkiosk:[太长省略]:16261:0:99999:7::: ##找到第三个字段"16261",将其修改为"0"按“Esc”,输入“:wq!”使用student用户重新登陆,发现验证成功后,被强制要求更换密码################################################ 第四单元 ################################################1.管理输入输出####################在linux系统中,正确输出的编号为1,错误输出编号为2在系统中使用普通用户"student"执行以下命令:find /etc -name passwd因为"student"用户权限问题会有以下输出:find: ‘/etc/pki/CA/private’: Permission denied ##权力受限,报错find: ‘/etc/pki/rsyslog’: Permission deniedfind: ‘/etc/audit’: Permission denied/etc/passwd ##正确输出find: ‘/etc/polkit-1/rules.d’: Permission deniedfind: ‘/etc/polkit-1/localauthority’: Permission deniedfind: ‘/etc/dhcp’: Permission deniedfind: ‘/etc/selinux/targeted/modules/active’: Permission deniedfind: ‘/etc/lvm/archive’: Permission deniedfind: ‘/etc/lvm/backup’: Permission deniedfind: ‘/etc/lvm/cache’: Permission deniedfind: ‘/etc/grub.d’: Permission denied/etc/pam.d/passwd ##正确输出find: ‘/etc/audisp’: Permission deniedfind: ‘/etc/firewalld’: Permission deniedfind: ‘/etc/cups/ssl’: Permission deniedfind: ‘/etc/ipsec.d’: Permission deniedfind: ‘/etc/libvirt’: Permission deniedfind: ‘/etc/sudoers.d’: Permission deniedfind: ‘/etc/named’: Permission deniedfind /etc -name passwd > file ##重定向正确输出find /etc -name passwd 2> file ##重定向错误输出find /etc -name passwd &> file ##重定向所有输出注意: >, 2> ,&> 都会覆盖源文件内容>file ##清空filefind /etc -name passwd >> file ##追加正确输出find /etc -name passwd 2>> file ##追加错误输出find /etc -name passwd &>> file ##追加所有输出注意:>>, 2>>, &>> 不会覆盖源文件内容,会把相应的输出字符方到文件的最后"|" 管道符,管道符的作用是将管道前命令的输出变成管道后命令的输入ls /bin|wc -l ##统计ls /bin 命令输出的行数系统中错误的输出是无法通过管道符的。用"2>&1"可以把错误的输出编号由2变成1tee 复制输出到指定位置date|tee file|wc -l ##tee命令将date命令的输出复制到file中,并统计输出行数####################输入的命令先到Memory,Memory再给到CPU,CPU将结果反馈给"/dev/pts/0",即终端的输出界面"/dev/pts/0"默认用来保存终端输出,"0"指第一个终端,"1"指第二个终端,依次类推date >/dev/pts/0 ##将date命令的输出复制到"/dev/pts/0"当中,即将输出显示在第一个终端的输出界面[root@foundation50 Desktop]# >file ##清空file。如果file不存在,则创建file,内容为空[root@foundation50 Desktop]# wc file ##统计file容量信息,什么都没有0 0 0 file[root@foundation50 Desktop]# echo >file ##输出一个空格到file[root@foundation50 Desktop]# wc file ##统计file容量信息,1行0字1字节1 0 1 file[root@foundation50 Desktop]# echo 123 >file ##输出"123"到file[root@foundation50 Desktop]# wc file ##统计file容量信息,1行1字4字节1 1 4 file[root@foundation50 Desktop]# cat file123注意:echo相当于c语言中printfprintf()是c语言中产生格式化输出的函数, 一般用于向标准输出设备按规定格式输出信息。"cat -n"对所有输出的行数从1开始编号"cat -b"和"cat -n"相似,区别是对空白行不进行编号[kiosk@foundation50 ~]$ find /etc/ -name passwd | cat -bfind: ‘/etc/pki/CA/private’: Permission deniedfind: ‘/etc/pki/rsyslog’: Permission deniedfind: ‘/etc/dhcp’: Permission deniedfind: ‘/etc/lvm/archive’: Permission deniedfind: ‘/etc/lvm/backup’: Permission deniedfind: ‘/etc/lvm/cache’: Permission deniedfind: ‘/etc/selinux/targeted/modules/active’: Permission deniedfind: ‘/etc/audit’: Permission denied 1 /etc/passwd ##发现仅对正确的输出进行了编号find: ‘/etc/polkit-1/rules.d’: Permission deniedfind: ‘/etc/polkit-1/localauthority’: Permission denied 2 /etc/pam.d/passwdfind: ‘/etc/firewalld’: Permission deniedfind: ‘/etc/grub.d’: Permission deniedfind: ‘/etc/ipsec.d’: Permission deniedfind: ‘/etc/audisp’: Permission deniedfind: ‘/etc/vmware-tools/GuestProxyData/trusted’: Permission deniedfind: ‘/etc/libvirt’: Permission deniedfind: ‘/etc/cups/ssl’: Permission deniedfind: ‘/etc/sudoers.d’: Permission denied[kiosk@foundation50 ~]$ find /etc/ -name passwd 2>&1 | cat -b 1 find: ‘/etc/pki/CA/private’: Permission denied 2 find: ‘/etc/pki/rsyslog’: Permission denied 3 find: ‘/etc/dhcp’: Permission denied 4 find: ‘/etc/lvm/archive’: Permission denied 5 find: ‘/etc/lvm/backup’: Permission denied 6 find: ‘/etc/lvm/cache’: Permission denied 7 find: ‘/etc/selinux/targeted/modules/active’: Permission denied 8 /etc/passwd 9 find: ‘/etc/audit’: Permission denied 10 /etc/pam.d/passwd 11 find: ‘/etc/polkit-1/rules.d’: Permission denied 12 find: ‘/etc/polkit-1/localauthority’: Permission denied 13 find: ‘/etc/firewalld’: Permission denied 14 find: ‘/etc/grub.d’: Permission denied 15 find: ‘/etc/ipsec.d’: Permission denied 16 find: ‘/etc/audisp’: Permission denied 17 find: ‘/etc/vmware-tools/GuestProxyData/trusted’: Permission denied 18 find: ‘/etc/libvirt’: Permission denied 19 find: ‘/etc/cups/ssl’: Permission denied 20 find: ‘/etc/sudoers.d’: Permission denied ##对所有输出都进行了编号 注意:"2>&1"只有在有"|"的时候才起作用例如:[kiosk@foundation50 ~]$ find /etc/ -name passwd 2>&1 >filefind: ‘/etc/pki/CA/private’: Permission deniedfind: ‘/etc/pki/rsyslog’: Permission deniedfind: ‘/etc/dhcp’: Permission deniedfind: ‘/etc/lvm/archive’: Permission deniedfind: ‘/etc/lvm/backup’: Permission deniedfind: ‘/etc/lvm/cache’: Permission deniedfind: ‘/etc/selinux/targeted/modules/active’: Permission deniedfind: ‘/etc/audit’: Permission deniedfind: ‘/etc/polkit-1/rules.d’: Permission deniedfind: ‘/etc/polkit-1/localauthority’: Permission deniedfind: ‘/etc/firewalld’: Permission deniedfind: ‘/etc/grub.d’: Permission deniedfind: ‘/etc/ipsec.d’: Permission deniedfind: ‘/etc/audisp’: Permission deniedfind: ‘/etc/vmware-tools/GuestProxyData/trusted’: Permission deniedfind: ‘/etc/libvirt’: Permission deniedfind: ‘/etc/cups/ssl’: Permission deniedfind: ‘/etc/sudoers.d’: Permission denied[kiosk@foundation50 ~]$ cat file/etc/passwd/etc/pam.d/passwd ##发现只有正确的输出重定向到了file,即"2>&1"没有生效[root@foundation50 Desktop]# date | tee file ##将date命令的输出复制到file中,并在输出界面显示Fri Sep 30 14:47:49 CST 2016[root@foundation50 Desktop]# cat fileFri Sep 30 14:47:49 CST 2016[root@foundation50 Desktop]# date | tee file | wc -l1[root@foundation50 Desktop]# cat fileFri Sep 30 14:48:18 CST 2016[root@foundation50 Desktop]# date | tee /dev/pts/0 | wc -lFri Sep 30 14:54:07 CST 20161####################