|
Module4 Changing the Solaris OE Directory Contents
Copying Files and Directories
要拷贝文件可以使用cp命令,命令格式为: cp -option source(s) target 可以使用-i选项来进行交互性提示,提示你是否要覆盖已有文件;source(s)是源文件,target可以是目标文件或目标目录
Copying a File within a Directory
在同1个目录里进行拷贝,如下,把1个名为file3的文件拷贝并重命名为feathers: $ pwd /export/home/user1 $ ls dante dir2 dir5 file.2 file3 fruit2 dante_1 dir3 file.1 file2 file4 practice dir1 dir4 file1 file.3 fruit tutor.vi $ cp file3 feathers $ ls dante dir2 dir5 file1 file.3 fruit tutor.vi dante_1 dir3 feathers file.2 file3 fruit2 dir1 dir4 file.1 file2 file4 practice
把多个文件拷贝到1个目录里,如下,把user1目录里的feathers和feathers_6文件拷贝到dir1目录里: $ pwd /export/home/user1 $ ls dir1 coffees fruit trees $ cp feathers feathers_6 dir1 $ ls dir1 coffees feathers feathers_6 fruit trees
可以使用-i选项防止覆盖已有文件,如下: $ cp -i feathers feathers_6 cp: overwrite feathers_6 (yes/no)? y $
Copying Directories
使用cp命令加上-r选项来将1个目录及其所有内容拷贝到另1目录里,如果目标目录不存在,cp命令将创建1个新目录.假如你没有使用-r选项就进行拷贝的话,你将收到错误信息: cp: [directory-name]: is a directory.
拷贝目录的格式是: cp -option source(s) destination source(s)可以是1个或多个源目录;destination是单独的1个目标目录;选项如下:
 来看1个例子,如下: $ cd $ pwd /export/home/user1 $ ls dir3 planets $ cp dir3 dir10 cp: dir3: is a directory $ cp -r dir3 dir10 $ ls dir10 planets $ ls dir3 planets $
Moving Files and Directories
mv:移动文件和目录.命令格式为: mv -i source target -i选项为交互性提示
来看个例子,我们把brands文件从coffees目录中移动到user1目力里去.如下: $ cd ~/dir1/coffees $ pwd /export/home/user1/dir1/coffees $ ls beans brands nuts $ mv brands ~ $ ls beans nuts $ ls ~/brands /export/home/user1/brands
来看下目录的转移.我们把practice目录转移到letters目录中去,如下: $ cd $ pwd /export/home/user1 $ ls practice mailbox project projection research results $ mv practice letters $ ls letters mailbox project projection research results $ 当你转移1个单独的目录到1个不存在的目录中去的时候,新目录将被重命名;但是当你转移多个目录到1个不存在的目录中去的时候,将收到以下错误提示: mv: target_directory not found.
Creating Files and Directories
touch:可以创建新的空文件,如果目标文件存在的话,目标文件的最后访问和修改时间将改变为当前时间.命令格式为: touch filename(s) 来看个实例,如下: $ cd ~/dir3 $ ls planets $ touch space $ ls planets space $ 同时创建多个空文件,如下: $ cd ~/dir3 $ ls planets space $ touch moon sun cosmos $ ls cosmos moon planets space sun $
mkdir:创建新目录.命令格式为: mkdir directory_name mkdir -p directory_names -p选项是同时创建之新目录和其父目录 实例如下: $ cd $ pwd /export/home/user1 $ mkdir Reports $ ls -ld Reports drwxr-xr-x 2 user1 staff 512 Mar 14 16:24 Reports $ 再看下mkdir命令加上-p选项的用法,创建1个新目录empty和其父目录newdir.如下: $ cd $ pwd /export/home/user1 $ ls brands dir10 dir5 file1 file3 letters dante dir2 feathers file.2 file4 Reports dante_1 dir3 feathers_6 file2 fruit tutor.vi dir1 dir4 file.1 file.3 fruit2 $ mkdir -p newdir/empty $ ls newdir empty $
Renaming Files and Directories
mv:重命名文件或目录,格式为: mv source target 把当前目录中的nuts文件重命名为cream,如下: $ mv nuts cream $ ls nuts cream $ 把当前目录中的noco目录重命名为jojo,如下: $ mv noco jojo $ ls noco jojo $
Removing Files and Directories
rm:删除文件和目录.命令的格式为: rm -option filename -i:交互性提示.如下: $ rm -i mailbox rm: remove mailbox: (yes/no) ? y $
rmdir:用于删除空目录.命令的格式为: rmdir directory(s) 假如你用rmdir命令删除非空目录,将收到以下错误提示信息: rmdir: directory [directory_name]: Directory not empty
使用rm命令和-r选项删除目录及其子目录的所有内容,格式为: rm -r directory(s) 假如你只使用rm命令来删除非空目录的话,会提示以下错误信息 rm: [directory-name]: is a directory.: 比如要删除用户主目录下的practice目录及其子目录dir1,如下: $ mkdir -p ~/practice/dir1 $ ls practice dir1 $ rm -ir ~/practice rm: examine files in directory /export/home/user1/practice (yes/no)? y rm: examine files in directory /export/home/user1/practice/dir1 (yes/no)? y rm: remove /export/home/user1/practice/dir1: (yes/no)? y rm: remove /export/home/user1/practice: (yes/no)? y $
Using Symbolic Links
你的文件或者目录可能是位于不同的文件系统(file system)中,你可以使用symbolic link来对它们进行链接 symbolic link就是包含到其他文件或目录的路径名的指针(pointer).这样的链接使得和必须输入长路径名相比更为容易访问.还记得这张图吗:
 如果是symbolic link的话,前面第一个标志不是d也不是符号-,而是字母l.可以使用ls命令加上-l或-F选项来查看哪些是symbolic link文件
使用ln命令加上-s选项来创建1个symbolic link文件,命令格式为: ln -s source_file target_file source_file是源文件;target_file是symbolic link文件.如下是给文件dante创建1个symbolic link文件danta_link: $ cd $ pwd /export/home/user1 $ ln -s dante dante_link $ ls -F brands dir10/ feathers file2 fruit2 tutor.vi* dante dir2/ dante_link@ dir4/ file1 file4 newdir/ dir1/ $ $ ls -l dante_link lrwxrwxrwx 1 user1 staff 5 Mar 15 09:45 dante_link -> dante 注意@符号表示是symbolic link文件;而dante_link -> dante表示是链接到文件dante
如果要删除symbolic link文件就使用rm命令,和删除普通文件的方法是一样的,如下: $ cd $ pwd /export/home/user1 $ ls -l dante_link lrwxrwxrwx 1 user1 staff 5 Mar 15 09:45 dante_link -> dante $ rm dante_link $ ls dante_link dante_link: No such file or directory $
Module5 Searching Files and Directories
Using the grep Command
grep:在不改变文件内容的情况下,根据某种关键字来对文件内容进行搜索查找,然后把符合这些关键字的内容输出到屏幕上.grep命令格式为: grep option(s) pattern filename(s) 一些选项如下表:
 来看看在/etc/group文件中搜索关键字root,如下: $ grep root /etc/group root::0:root bin::2:root,bin,daemon sys::3:root,bin,sys,adm adm::4:root,adm,daemon uucp::5:root,uucp mail::6:root tty::7:root,adm lp::8:root,lp,adm nuucp::9:root,nuucp daemon::12:root,daemon $ 搜索那些不包含root的内容,如下: $ grep -v root /etc/group other::1: staff::10: sysadmin::14: nobody::60001: noaccess::60002: nogroup::65534: $ 从/etc/group,/etc/passwd和/etc/hosts文件中搜索包含关键字root的文件,如下: $ cd /etc $ grep -l root group passwd hosts group passwd $ 查找出包含关键字root所在行数,如下: $ grep -c root group 10 $
grep命令还支持一些正则表达式(regular expression,RE)来作为关键字,来看些常用的RE,如下图:
 来看1下RE应用的实例.比如要找出在/etc/passwd文件中以no开头的内容,如下: $ grep ‘^no’ /etc/passwd nobody:x:60001:60001:Nobody:/: noaccess:x:60002:60002:No Access User:/: nobody4:x:65534:65534:SunOS 4.x Nobody:/: $ 再来看看在/etc/passwd文件中搜索以A开头,中间3个字符任意匹配,以n结尾的内容,如下: $ grep ‘A...n’ /etc/passwd etc/passwd adm:x:4:4:Admin:/var/adm lp:x:71:8:Line Printer Admin:/usr/spool/lp uucp:x:5:5:uucp Admin:/usr/lib/uucp nuucp:x:9:9:uucp Admin:/var/spool/uucppublic listen:x:37:4:Network Admin:/usr/net/nls $ 在/etc/passwd中搜索以adm结尾的内容,如下: $ grep ‘adm$’ /etc/group sys::3:root,bin,sys,adm tty::7:root,tty,adm lp::8:root,lp,adm $
Using the egrep Command
egrep命令和grep命令类似,但是支持扩展RE,先来看看一些扩展RE,如下图:

egrep命令的格式为: egrep -option(s) keyword filename(s) 选项除了-w,其他的和grep的选项都一样
来看1个关于使用扩展RE的例子,搜索包含N开头的,紧跟e或o的内容,如下: $ egrep ’N(e|o)+’ /etc/passwd listen:x:37:4:Network Admin:/usr/net/nls: nobody:x:60001:60001:Nobody:/: noaccess:x:60002:60002:No Access User:/: nobody4:x:65534:65534:SunOS 4.x Nobody:/: 查找包含Network Admin或者是uucp Admin的内容,如下: $ egrep ’(Network|uucp) Admin’ /etc/passwd uucp:x:5:5:uucp Admin:/usr/lib/uucp: nuucp:x:9:9:uucp Admin:/var/spool/uucppublic:/usr/lib/uucp/uucico listen:x:37:4:Network Admin:/usr/net/nls:
Searching for Files and Directories
find:查找文件或目录的位置,命令格式为: find pathname(s) expression(s) action(s) 这条命令的含义其实可以这样理解:在某个位置(pathname)根据某个条件(expression)查找(find)某个文件或目录,然后顺便执行某样命令(action) 先来看下expression(s),如图:

再来看下action,如图:
 来看几个例子: 在用户主目录查找名为dante的文件,如下: $ find ~ -name dante /export/home/user1/dante $ 注意所找到的文件的路径名被输出到了终端屏幕(-print是默认隐含的) 在用户主目录查找名字为noco的文件并删除它,如下: $ find ~ -name noco -exec rm {} \; $ 查找当前路径中文件修改时间为2天以上的文件,如下: $ find . -mtime +2 (略) 在当前路径中查找文件大小大于10个block(512bytes)的文件,如下: $ find . -size +10 (略) 查找用户主目录以es结尾的文件,如下: $ find ~ -name ‘*es’ /export/home/user1/.dt/sessions/current/dt.resources /export/home/user1/.dt/sessions/current.old/dt.resources /export/home/user1/.dt/types /export/home/user1/.dt/palettes /export/home/user1/dir1/coffees /export/home/user1/dir1/trees /export/home/user1/dir2/notes /export/home/user1/dir2/recipes /export/home/user1/games /export/home/user1/veggies $
Module6 Using the vi Editor
Identifying the Fundamentals of vi Editor Operation
vi编辑器是一种交互性的文本编辑器.当你的CDE不可用的时候,可以使用vi编辑器来进行文本编辑,下图就是vi编辑器的界面:
 vi的3种模式: 1.command mode 2.edit mode 3.last line mode
command模式是vi的默认模式,在这个模式中,你可以输入命令来删除,替换,拷贝和移动文本,还可以对光标进行定位,查找文本字符串和退出vi等等
edit模式可以在用来输入文本,vi编辑器把你输入的任何内容都作为文本.要进入这个模式,输入以下的命令: i:在光标的位置前插入文本 o:在光标的位置后面打开新的空行 a:在光标的位置后面增加文本
last line模式可以用来使用高级编辑命令,要进入这个模式,在command模式下输入符号:就可以进入
刚进入vi编辑器后,默认是处在command模式里的,然后输入a,i或者o就进入编辑模式,当完成编辑以后,按下键盘上的Escape键退回到command模式.在command模式里,你可以对文件进行保存和退出
下面是1个如何在vi编辑器模式之间的交换的例子: 1.输入vi [文件名]来创建1个文件,自动进入command模式 2.输入i来插入文本,进到edit模式 3.按下Escape键返回command模式 4.输入:wq来保存文件和退出vi编辑器
Introducing the vi Command
vi命令可以用来在vi编辑器中创建,编辑和查看文件,命令格式为: vi vi filename vi option(s) filename 假如你用vi编辑器编辑系统文件,而系统受到影响以后,可以使用-r选项来进行恢复,如下: vi -r filename
Manipulating Files within the vi Editor
可以使用view命令在只读模式下读取文件,它调用了vi和只读选项,大部分的命令可以在只读模式下使用,但是不能保存修改过了的文件,命令格式为: view filename 比如要在只读模式下读取文本文件jojo,如下: $ view jojo (略) 然后jojo的文件内容输出到屏幕上来,可以使用:q命令退出
来看看进入到edit模式以后一些可用命令和它们的作用,如下图:
 注意vi编辑器是大小写敏感的
来看看一些移动按键,如下图:

一些编辑按键,先来看看删除按键,如下:

一些更改命令,如下图:
 一些替换和查找命令,如下图:
 一些复制粘贴命令,如下:
 最后看看退出和保存的命令,如下:

|