版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、,第2章 Shell及常用命令,2.1 Shell的基本形式 2.2 帮助命令 2.3 文件与目录的基本概念 2.4 目录和文件的基本操作 2.5 归档与压缩命令 2.6 其它常用命令,2.1 Shell的基本形式,Shell是一个命令解释器,它可以用来启动、挂起、停止程序,还允许用户编写由Shell命令组成的程序。 在/etc/shells文件中理出了系统可以接受的shell,默认安装的Red Hat Linux 9中的shells文件的内容如下:,Shell的基本形式,可以查看这些Shell的详细情况:,Shell的种类: ash:贝尔实验室开发的shell;bsh是对它的符号链接 bas
2、h:GNU的Bourne Again shell,是GNU操作系统上默认的shell;sh是对它的符号链接 tcsh:Berkeley UNIX C shell;csh是对它的符号链接,Shell命令的基本格式,格式:命令名 选项 表示可选项 “选项”是对命令的特别定义。 以“-”开头的选项通常只有一个字母,多个选项可以用一个“-”连接起来;如“ls -a -l”与“ls -al”是等价的。 有的选项以“-”开头,这些选项通常是一个单词,如-number。 很多“-”格式的选项也有用“-”加上首字母的简写方式。,命令格式举例,RIGHT WRONG 1. Separation $ mail -
3、f personal $ mail - f personal $ who -u $ who-u 2. Order $ mail -s test root $ mail test root -s $ who -u $ -u who 3. Multiple options $ who -m -u $ who -m-u $ who -mu $ who -m u,命令自动补全,如果输入了文件名、命令或路径名的一部分,然后按Tab键,则: 如果能匹配唯一的文件名,则bash将把文件名的剩余部分补全; 如果能匹配多个文件名,可以再按一次Tab键来获取与已输入部分匹配的文件名或路径名的列表。,查看命令历史,
4、使用上、下箭头可以上下翻阅已输入过的命令 方便输入重复的或类似于以前输入过的命令 bash命令行历史文件中默认可以储存500条命令,2.2 帮助命令,man命令 info命令 whereis命令 -help选项,(1) man命令,查看命令的手册页(manual pages),它存放在/usr/share/man下。 手册页的组成:,man命令的格式,man 选项 命令名称 常用选项: -f:只显示命令的功能而不显示其中详细的说明文件。 -w:不显示手册页,只显示将被格式化和显示的文件所在的位置 -k:显示和给定关键字相关的所有命令。,man example (1),man example (
5、2),(2) info命令,info是另一个非常有用的帮助命令。 命令格式: info 命令名 其信息存放在/usr/share/info下。,info举例,# info pwd,(3) whereis:寻找命令所在位置,命令格式:whereis 选项 命令名 常用选项:,whereis举例,以ls命令为例,(4) - help选项,help是内置在命令中的,2.3 文件与目录的基本概念,2.3.1 文件与文件类型 Linux的文件均为无结构的字符流形式。 文件名的命名规则 Linux系统有三种基本的文件类型: 普通文件 文本文件 二进制文件 目录文件,简称为目录 设备文件 块设备文件 字符设
6、备文件,2.3.2 目录,Linux通过目录将系统中所有的文件分级、分层组织在一起,形成树形层次结构,以根目录 “/” 为起点。 特殊目录: “.”代表该目录自己, “.”代表该目录的父目录, 对于根目录,“.”和“.”都代表其自己。,2.3.3 Linux的标准文件系统,All Linux directories are contained in one, virtual, unified filesystem Physical devices are mounted on mount points Floppy disks Hard disk partitions CD-ROM drive
7、s No drive letters like A:, C:, .,Linux的标准文件系统,/bin, /lib, /sbin,/bin contains executables for every user /sbin contains system administration executables /lib contains libraries Should always be available At system boot In single user mode When booting from rescue disk Can not be in separate filesy
8、stems(不能作为独立的文件系统),/boot,Contains kernel image and some other goodies Should be located below cylinder 1023 and/or 8 GB (earlier version) Good habit to always make it a separate filesystem,/dev,Contains special files that represent hardware devices Block special device, for example, a hard disk Char
9、acter special device, for example, mouse and keyboard Each device has a major and minor number Identification within the kernel Can not be a separate filesystem,/etc,Contains system-wide configuration files Some subsystems have multiple files and therefore use a separate directory /etc/X11 contains
10、X Window System configuration /etc/skel contains default user configuration files /etc/sysconfig contains system configuration Can not be a separate filesystem,/home,Home directories of users Can be a separate filesystem,/mnt,Mount points for other filesystems Can be a separate filesystem Note: SuSE
11、 uses /media instead of /mnt for floppy and cdrom mountpoints,/proc,Virtual filesystem Represents kernel and process information,/root,Home directory of the root user Should not be a separate filesystem,/tmp,Temporary storage space for programs, users Can be a separate filesystem Sometimes automatic
12、 cleanup mechanism active,/usr,UNIX System Resources Contains all programs, libraries and so on which are not essential for system boot and emergency operations Can be a separate filesystem Can be mounted over NFS(可通过NFS挂载) Can be read-only /usr/local intended for programs not in the distribution Ma
13、y be a separate filesystem too,/var,Files of variable size logfiles lockfiles Directories with variable content mail scheduling printing Temporary storage space, longer than /tmp Can be a separate filesystem,/lost+found,Exists in every filesystem Place where lost+found files are stored after a crash
14、 recovery by fsck.,Other Directories in /,/opt: used for some software from external providers Separate filesystem advisable Whatever you create yourself.,Linux的标准文件系统,以下文件系统可以作为独立的文件系统存在: /boot /home /mnt /tmp /usr /var,2.3.4 工作目录、用户主目录与路径,工作目录(Working Directory):用户登录到系统后,每时每刻都处在某个目录之中,此目录称为工作目录。 用
15、户主目录(Home Directory):是系统管理员在增加用户时为该用户建立起来的目录,每个用户都有自己的主目录。 使用符号表示。 默认情况下,用户主目录是/home目录下与用户名相同的目录。 例:用户lh的主目录是/home/lh,命令 cd 和命令 cd /home/lh意义相同。 绝对路径 相对路径,通配符,*:匹配零或多个字符 ?:匹配任意一个字符 :该通配符把所有想匹配的字符放在 内,结果表达式将与 中任一字符相匹配;也可以使用 - 指定范围。例如: myfile12将与myfiel1和myfile2匹配。 CchangeLlog将与Changelog、ChangeLog、chan
16、gelog、changeLog匹配。 ls /etc/0-9*将列出/etc中以数字开头的所有文件。,通配符,! !与 构造类似,只要不是列在 ! 和 之间的字符,它将与任何字符匹配。例:myfile!9匹配除myfile9之外的名为myfile加一个字符的所有文件。,2.4 目录和文件的基本操作,pwd, cd, ls cat, more, less cp, rm ,mv mkdir ,rmdir touch find, locate ln,pwd:打印当前工作目录,pwd:打印当前工作目录,Print Working Directory,$ pwd /home/tux1 $,cd :切换目
17、录,$ cd doc (relative) $ cd /home/tux1/doc (full) $ cd tux1/doc (home) $ cd (Go to your home directory) $ cd (Go to your home directory) $ cd . (Go one directory up) $ cd - (Go to previous directory),ls :显示目录内容,格式:ls dir/file ,$ ls /home tux1 tux2 tux3 Important options: -l long listing (more informa
18、tion) -a lists all files (including hidden) -t lists files sorted by change date -R lists contents recursively,在Shell下使用ls命令显示目录及文件的颜色是不一样的: 目录:蓝色 普通文件:白色 可执行文件:绿色,ls :显示目录内容,cat (concatenate), more, less,cat:显示文件内容 格式:cat filename more, less:分屏显示文件内容 看完一页后按空格键向后翻页,按Enter键往下滚动一行,按Q键退出显示 more和less的区别
19、在于: more显示的内容只能向后翻页; less显示的内容可以前后翻页,用B键向前翻页,并且支持PageUp和PageDown键上下翻页。,mkdir :创建子目录,使用-p选项可以一次创建多层空目录,$ mkdir /home/tux1/doc (full pathname) $ cd /home/tux1 $ mkdir doc (relative pathname),rmdir :删除目录,格式:rmdir dir_name 注意:rmdir只能删除空目录。 使用-p选项可以一次删除多层空目录。,rm :删除文件或目录,要删除目录,必须指定-r选项,此时将会删除目录下的所有文件,然后删
20、除该目录,$ rm test/rob If unsure, use -i option $ rm -i test/rob rm: remove test/rob? To remove files and directories recursively: $ rm -ir test/,cp :复制文件,格式:cp sources target,Copying one file to another: $ cp .bashrc bashrc.old Copying multiple files into a target directory: $ cp doc/mon_report doc/wal
21、rus /tmp,-R选项:递归地复制目录,$ cp -R /home/tux1/doc /tmp To prevent cp from overwriting existing files, use: $ cp -R -i /home/tux1/doc /tmp cp: overwrite /tmp/doc/walrus?,mv :移动文件,重命名,格式:mv sources target mv默认以递归的方式移动 如果名称与目标位置一样,其作用就是更改目录或文件名,To move a file to another directory: $ mv doc/walrus ././tmp To
22、 rename a file: $ mv doc documents Use the -i option to prevent mv from overwriting existing files!,touch,The touch command updates the access and/or modification times of a file.,$ ls -l -rw-rw-r- - 1 tux1 penguins 512 Feb 24 11:10 docs $ touch docs $ ls -l -rw-rw-r- - 1 tux1 penguins 512 Mar 5 15:
23、37 docs $ touch new $ ls -l -rw-rw-r- - 1 tux1 penguins 512 Mar 5 15:37 docs -rw-rw-r- - 1 tux1 penguins 0 Mar 5 15:37 new,find,格式:find path expression 该命令在目录树层次中从path开始向下查找文件,将符合expression要求的文件列出来。,find / -name test 从根目录开始,查找所有名称为test的文件 find . -cmin -20 查找当前目录(也包含其所有的子目录)下所有最近20分钟内更新过的文件 find /usr
24、/ -size 50K 查找/usr目录(也包含其所有的子目录)下超过50K的文件,locate:搜索文件,使用locate命令,可以看到每一个包括搜寻条件的目录或文件。例如要搜寻名称中带有finger这个词的文件,输入命令: $ locate finger 搜索结果为:所有文件或目录名中带有finger这个词的文件和目录。 locate命令使用一个数据库来定位所有文件或目录名中带有finger这个词的文件和目录。,ln: 链接命令,命令格式: ln 选项 该命令产生一个从dest到source的链接。 在UNIX/Linux文件系统中,链接可分为: 硬链接 符号链接,硬链接,系统调用link
25、在文件系统结构中将一个文件链接到另外一个新的名字上,也就是为被链接文件的索引节点创建一个新的目录项。 通过链接操作,一个文件可以有两个以上的不同名字,系统可以通过其中任意一个名字来访问文件。内核并不知道哪个名字是最初的文件名。 这种为索引节点创建新目录项的链接方式称为“硬链接”。,一个给定的索引节点可以有任意数目的硬链接,该索引节点一直存在于文件系统,直到所有的硬链接消失。 可以使用ln命令来创建新的硬链接:,$ cd /tmp$ touch firstlink$ ln firstlink secondlink$ ls i firstlink secondlink15782 firstlink
26、 15782 secondlink,硬链接,用ln命令创建硬链接,硬链接工作于索引节点级别,指向特殊的文件 硬链接的局限性: 只能给文件建立硬链接,而不能给目录建立硬链接。即使. 和 . 是系统给目录创建的硬链接,也不允许用户创建自己的硬链接。 硬链接不能跨文件系统。,硬链接,符号链接,与 “硬链接”类似,也为新名字创建一个新的目录项,但是目录项中不含有索引节点号,而是含有从当前目录下访问被链接文件的相对路径名或绝对路径名。 当进程使用新名字访问文件时,内核首先找到新名字所在的目录项,发现目录项中含有符号链接标志,然后从目录项中读出被链接文件的路径名,按此路径名访问指定的文件。,符号链接是一种
27、专门的文件类型,在这种文件类型中,链接通过名称引用另一个文件,而不是直接引用索引节点。 符号链接不阻止文件被删除;如果源文件被删除,那么符号链接仍然存在,但是不可用。,符号链接,在ls-l输出中,可以用三种方式区分符号链接和一般文件: 第一栏包含一个l字符 符号链接的大小是目标文件名的字符数 输出的最后一栏显示目标文件名,$ln s secondlink thirdlink$ls l firstlink secondlink thirdlink -rw-rw-r- 2 agriffis agriffis 0 Dec 31 19:08 firstlink -rw-rw-r- 2 agriffis
28、 agriffis 0 Dec 31 19:08 secondlink lrwxrwxrwx 1 agriffis agriffis 10 Dec 31 19:39 thirdlink - secondlink,通过ln 的-s选项,可以创建符号链接:,用ln -s命令创建符号链接,符号链接的优点,符号链接比硬链接更灵活 可以给任何类型的文件系统对象(包括目录)创建符号链接 因为符号链接的实现是基于路径的,所以符号链接可以跨文件系统,2.5 归档与压缩命令,归档文件(archive file)是一个文件和目录的集合,而这个集合被存储在一个文件中。归档文件没有经过压缩。 压缩文件(compres
29、sed file)也是一个文件和目录的集合,这个集合也被存储在一个文件中,但是,它的存储方式所占用的磁盘空间比所有文件和目录的总和要少。 归档文件可以压缩也可以不压缩,压缩的归档文件才是压缩文件。,1. 压缩文件,压缩工具,(1) zip和unzip,压缩文件,例:$ zip r filename.zip file1 file2 dir ,从zip文件中删除文件: $ zip d filename.zip file1 file2 ,解压文件:$ unzip filename.zip 显示zip文件内容:$ unzip v filename.zip 指定解压缩的目录(unzip默认将文件解压到当
30、前目录)$ unzip filename.zip d dir,(2) gzip和gunzip,压缩文件:$ gzip myfile文件即会被压缩并保存为myfile.gz 解压文件:$ gunzip myfile.gz myfile.gz文件会被删除,而产生myfile文件,(3) bzip2和bunzip2,压缩文件:$ bzip2 myfile 文件即会被压缩并保存为myfile.bz2 解压文件,使用命令:$ bunzip2 myfile.bz2 myfile.bz2文件会被删除,而产生myfile文件,2. 文件归档,.tar文件是几个文件和(或)目录在一个文件中的集合 使用在tar命令上的选项有:,创建tar文件,$ tar -cvf filename.tar files/dir,$ tar cvf file.tar file1 file2 filedir file1 file2 filedir/ filedir/filea filedir/fileb,显示tar文件内容,$ tar tf filename.tar,$ tar tf file.tar file1 file2 filedir/ filedir/filea filedir/fileb,向tar文件内加入文件,$ tar rvf filen
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论