Linux帮助文档和常用命令
本节主要内容
- 如何获取帮助文档
- Linux文件系统简介
- 目录操作
1. 如何获取帮助文档
在实际工作过程当中,经常会不记得命令的使用方式,例如ls命令后面可以跟哪些参数,此时可以使用man命令来查看其使用方式,例如
//man命令获取命令帮助手册xtwy@ubuntu:~$ man ls
image
可以使用键盘上的
image
来显示下一行或者上一行命令,也可以使用
image
进行上一页或者下一页(屏)命令的查看,另外
image
空格鍵也可以用来显示下一屏的命令。想退出命令查看,直接按q鍵退出就可,也可以h鍵显示less命令列表(man命令通过less命令输出结果)
2. Linux文件系统简介
(一) 文件和目录
本节从使用者的角度来详情Linux文件系统,Linux根据文件形式将文件分为目录和普通文件,如下图:
image
目录或者文件的名称长度不超过255个字符,文件或者目录名可由下列字符构成:
- Uppercase letters (A–Z)
- Lowercase letters (a–z)
- Numbers (0–9)
- Underscore ( _ )
- Period(.)
- Comma(,)
文件或者目录名区分大小写,属于不同的文件或者目录
(二) 文件扩展名与不可见文件名
与Window操作系统有很大不同的是,Linux文件对文件扩展名没有强制要求,例如假设编写了一个c语言源文件,你可以将其命名为complier.c,也可以是其它如complier、complier.ccc等文件名,但不推荐这么做,由于假如能将文件扩展名与特定的文件进行关联的话,有利于了解文件内容,目前商定成俗的linux文件扩展名如下表:
| 带扩展名的文件名 | 扩展名的含义 |
|---|---|
| max.c | C语言源文件 |
| max.o | 编码后的目标代码文件 |
| max | max.c对应的可执行文件 |
| memo.txt | 文本文件 |
| memo.pdf | pdf文件,必需在GUI界面上使用xpdf或者kpdf才能查看 |
| memo.ps | PostScript文件,必需在GUI界面上使用ghostscript或者kpdf才能查看 |
| memo.z | 经压缩程序压缩后的文件,可使用uncompress或者gunzip解压 |
| memo.gz | 经gzip压缩程序压缩后的文件,可使用gunzip解压 |
| memo.tar.gz或者memo.tgz | 经gzip压缩后的tar归档文件,可使用gunzip解压 |
| memo.bz2 | 经bzip2压缩后的文件,可使用bunzip2解压 |
| memo.html | html文件,使用GUI环境的firefox查看 |
| memo.jpg等 | 图像文件,使用GUI环境的照片查看器打开 |
在前一讲中我们看到,linux中还存在大量的隐藏文件,采用ls -a 命令可以显示,想定义隐藏文件,只需文件名或者目录以.开始就可
image
(三) 绝对路径与相对路径
在Linux中绝对路径与相对路径是一个很重要的概念,下图给出了什么是绝对路径
image
所有以根目录”/”作为开始的都是绝对路径,其它的均为相对路径
//绝对路径访问xtwy@ubuntu:~/Public$ cd /home/xtwy@ubuntu:/home$ lsxtwy//相对路径访问xtwy@ubuntu:/home$ cd xtwy/3. 目录操作
(一) 创立目录 mkdir
为演示方便,使用下列目录结构进行演示:
image
1 绝对路径创立方式
//使用绝对路径创立root@ubuntu:/home# mkdir /home/maxroot@ubuntu:/home# lsmax xtwyroot@ubuntu:/home# 2 相对路径创立方式
//使用相对路径进行创立root@ubuntu:/home# mkdir max/namesroot@ubuntu:/home# mkdir max/temproot@ubuntu:/home# mkdir max/literatureroot@ubuntu:/home# cd maxroot@ubuntu:/home/max# mkdir demoroot@ubuntu:/home/max# lsdemo literature names temp有时不想层层目录创立,此时可以在mkdir 后面加上参数 -p(parents),将父子目录一起创立
root@ubuntu:/home/max# mkdir -p literature/promoroot@ubuntu:/home/max# lsdemo literature names temproot@ubuntu:/home/max# cd literature/root@ubuntu:/home/max/literature# lspromo(二) 更改目录 cd
工作目录与主目录的区别
客户每次登录后的默认目录就是主目录,与系统会话期间保持不变,主目录用~表示
xtwy@ubuntu:/root$ cd ~xtwy@ubuntu:~$ pwd/home/xtwy工作目录又称当前目录,cd命令执行完成后的目录就是工作目录,它是可以随便改变的。
//.表示当前目录即工作目录//..表示当前目录的上一级目录xtwy@ubuntu:~$ cd .xtwy@ubuntu:~$ cd ..xtwy@ubuntu:/home$ (三) 删除目录 rmdir
rmdir是remove directory的简称,用于删除目录,它先删除目录下的所有文件,而后再删除该目录,但当目录下还有子目录时,该命令不能执行,需要使用rm命令,例如
//删除temp目录,先删除目录下的文件//再删除temp目录自身root@ubuntu:/home/max# rmdir temp/root@ubuntu:/home/max# rmdir literature/rmdir: failed to remove `literature/': Directory not emptyroot@ubuntu:/home/max# rm -r literature/root@ubuntu:/home/max# lsdemo names其中rm -r中的r指的是递归的删除目录及目录中的文件,因而它具备很强的破坏力,要谨慎使用。
(四) 移动目录 mv
//将目录demo移到/home/xtwy/目录下root@ubuntu:/home/max# mv demo/ /home/xtwy/root@ubuntu:/home/max# cd /home/xtwy/root@ubuntu:/home/xtwy# lsdemo Documents examples.desktop Pictures TemplatesDesktop Downloads Music Public Videosroot@ubuntu:/home/xtwy# rmdir demo//原来目录的demo目录已经不存在了root@ubuntu:/home/xtwy# cd /home/max/root@ubuntu:/home/max# lsnames(五) 复制目录 cp
前面用mv命令移动目录,有时候需要对目录进行拷贝,使用方式如下:
//先创立一个演示目录,用-p,父目录假如不存在将会被创立root@ubuntu:/home/max# mkdir -p literature/demo//因为literature还包括子目录,此时拷贝不成功root@ubuntu:/home/max# cp literature/ /home/xtwy/cp: omitting directory `literature/'//假如包括子目录的话,则加上-r参数,表示递归地拷贝root@ubuntu:/home/max# cp -r literature/ /home/xtwy/root@ubuntu:/home/max# cd /homtbash: cd: /homt: No such file or directoryroot@ubuntu:/home/max# cd /home/xtwy/root@ubuntu:/home/xtwy# lsDesktop Downloads literature Pictures TemplatesDocuments examples.desktop Music Public Videosroot@ubuntu:/home/xtwy# cd literature/root@ubuntu:/home/xtwy/literature# lsdemo4. 文件操作
(一) 创立文件
直接通过命令行的方式创立文件的方式有多种,常用方式如下:
//通过echo命令,将输出的命令重定向到文件root@ubuntu:/home/xtwy# echo "hello linux" > hello.txtroot@ubuntu:/home/xtwy# lsDesktop Downloads hello.txt Music Public VideosDocuments examples.desktop literature Pictures Templates//touch命令,如何文件不存在,会创立文件root@ubuntu:/home/xtwy# touch hell1.txtroot@ubuntu:/home/xtwy# lsDesktop Downloads hell1.txt literature Pictures TemplatesDocuments examples.desktop hello.txt Music Public Videos(二) 显示文件内容
cate命令可以显示文件内容,它的全称是catenate,意思是将单词一个接一个地连接起来
root@ubuntu:/home/xtwy# cat hello.txt hello linuxcat命令会将文件中所有的内容一律一次性显示出现,例如
root@ubuntu:/home/xtwy# cat /etc/profile# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i ......有时候我们希望能够分屏查看文件内容,此时可以使用less或者more分页程序,less和more的使用方式相差不大,通过空格键显示下一屏信息,它们之间的差别在于less在文件末尾会显示END消息,而more直接返回shell终端,例如:
less命令
image
more命令
image
(三) cp命令复制文件
root@ubuntu:/home/xtwy# lsDesktop Downloads hell1.txt literature Pictures TemplatesDocuments examples.desktop hello.txt Music Public Videos//复制文件root@ubuntu:/home/xtwy# cp hell1.txt literature/demoroot@ubuntu:/home/xtwy# cd literature/demo//cd -返回上一次执行的工作目录root@ubuntu:/home/xtwy/literature/demo# cd -/home/xtwy需要注意的是cp命令在复制时,假如目标目录中已存在该文件,系统不会给出警告,而是直接覆盖,因而它可能存在销毁文件的风险,为处理这个问题可以使用-i参数让系统给出警告,例如:
root@ubuntu:/home/xtwy# cp -i hell1.txt literature/democp: overwrite `literature/demo/hell1.txt'? (三) mv命令移动或者重命名文件
//在同一目录时,相当于文件重命名,执行完成后hell1.txt不存在root@ubuntu:/home/xtwy# mv hell1.txt hell2.txtroot@ubuntu:/home/xtwy# lsDesktop Downloads hell2.txt literature Pictures TemplatesDocuments examples.desktop hello.txt Music Public Videos//移动hell2.txt到literature/demoroot@ubuntu:/home/xtwy# mv hell2.txt literature/demoroot@ubuntu:/home/xtwy# cd literature/demo/root@ubuntu:/home/xtwy/literature/demo# lshell1.txt hell2.txtroot@ubuntu:/home/xtwy/literature/demo# cd -/home/xtwy//源目录hell2.txt已不存在root@ubuntu:/home/xtwy# lsDesktop Downloads hello.txt Music Public VideosDocuments examples.desktop literature Pictures Templates(四)显示文件头部或者尾部
显示文件头部内容用head命令,尾部用tail命令,默认显示行数为10
root@ubuntu:/home/xtwy# head ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)# for examples# If not running interactively, don't do anything[ -z "$PS1" ] && return# don't put duplicate lines in the history. See bash(1) for more options# ... or force ignoredups and ignorespaceHISTCONTROL=ignoredups:ignorespaceroot@ubuntu:/home/xtwy# tail ~/.bashrcif [ -f ~/.bash_aliases ]; then . ~/.bash_aliasesfi# enable programmable completion features (you don't need to enable# this, if it's already enabled in /etc/bash.bashrc and /etc/profile# sources /etc/bash.bashrc).#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then# . /etc/bash_completion#fihead及tail的默认行数是可以修改的,例如:
//仅显示前两行root@ubuntu:/home/xtwy# head -2 ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells.# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)tail命令在查看日志文件内容增长时可能经常会使用,例如在hadoop启动之后,会产生许多日志,但出现问题时,可以采用tail命令动态地监测日志文件内容的增长,查看问题出在哪个地方。
//初始显示情况root@ubuntu:/home/xtwy# tail -f hello.txt hello linux//向文件中追加内容root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt//追加后的输出情况root@ubuntu:/home/xtwy# tail -f hello.txt hello linuxhello linux linux(五)其它常见文件操作命令
下面的命令都不会改变文件内容
root@ubuntu:/home/xtwy# cp hello.txt hello1.txtroot@ubuntu:/home/xtwy# lsDesktop Downloads hello1.txt literature Pictures TemplatesDocuments examples.desktop hello.txt Music Public Videos//根据文件内容排序root@ubuntu:/home/xtwy# sort hello1.txthello linuxhello linux linux//逆序输出root@ubuntu:/home/xtwy# sort -r hello1.txthello linux linuxhello linux//diff进行内容比较root@ubuntu:/home/xtwy# diff hello1.txt hello.txt//向文件中追加内容root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt//内容比较root@ubuntu:/home/xtwy# diff hello1.txt hello.txt2a3> hello linux linux//格式化输出//-u参数将文件分成多块//比较的两个文件分别用-、+表示//本例中 -表示hello1.txt,+表示hello.txtroot@ubuntu:/home/xtwy# diff -u hello1.txt hello.txt--- hello1.txt 2015-08-22 17:28:44.071202558 -0700+++ hello.txt 2015-08-22 17:29:49.131181281 -0700//@@xxx@@用于标识行起始编号、行数//-1,2表示 hello1.txt文件起始编号为1,行数为2//+1,3表示 hello.txt文件起始编号为1,行数为3@@ -1,2 +1,3 @@ hello linux hello linux linux+hello linux linux作者:foreknow
链接:https://www.songma.com/p/d3c673b3f905
来源:简书
简书著作权归作者所有,任何形式的转载都请联络作者取得受权并注明出处。
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Linux帮助文档和常用命令