本节主要内容

  1. 怎样获取帮助文档
  2. Linux文件系统简单介绍
  3. 文件夹操作
  4. 訪问权限

1. 怎样获取帮助文档

在实际工作过程其中,常常会忘记命令的使用方式。比如ls命令后面能够跟哪些參数,此时能够使用man命令来查看其使用方式。比如

  1. //man命令获取命令帮助手冊
  2. xtwy@ubuntu:~$ man ls

能够使用键盘上的 来显示下一行或上一行命令,也能够使用 进行上一页或下一页(屏)命令的查看,另外 空格鍵也能够用来显示下一屏的命令。想退出命令查看,直接按q鍵退出就可以。也能够h鍵显示less命令列表(man命令通过less命令输出结果)

2. Linux文件系统简单介绍

(一) 文件和文件夹

本节从使用者的角度来介绍Linux文件系统,Linux依据文件形式将文件分为文件夹和普通文件,例如以下图:



文件夹或文件的名称长度不超过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 命令能够显示。想定义隐藏文件。仅仅要文件名称或文件夹以.開始就可以

(三) 绝对路径与相对路径

在Linux中绝对路径与相对路径是一个非常重要的概念。下图给出了什么是绝对路径



全部以根文件夹”/”作为開始的都是绝对路径,其他的均为相对路径

  1. //绝对路径訪问
  2. xtwy@ubuntu:~/Public$ cd /home/
  3. xtwy@ubuntu:/home$ ls
  4. xtwy
  5. //相对路径訪问
  6. xtwy@ubuntu:/home$ cd xtwy/

3. 文件夹操作

(一) 创建文件夹 mkdir

为演示方便,使用下列文件夹结构进行演示:

1 绝对路径创建方式

  1. //使用绝对路径创建
  2. root@ubuntu:/home# mkdir /home/max
  3. root@ubuntu:/home# ls
  4. max xtwy
  5. root@ubuntu:/home#

2 相对路径创建方式

  1. //使用相对路径进行创建
  2. root@ubuntu:/home# mkdir max/names
  3. root@ubuntu:/home# mkdir max/temp
  4. root@ubuntu:/home# mkdir max/literature
  5. root@ubuntu:/home# cd max
  6. root@ubuntu:/home/max# mkdir demo
  7. root@ubuntu:/home/max# ls
  8. demo literature names temp

有时不想层层文件夹创建。此时能够在mkdir 后面加上參数 -p(parents)。将父子文件夹一起创建

  1. root@ubuntu:/home/max# mkdir -p literature/promo
  2. root@ubuntu:/home/max# ls
  3. demo literature names temp
  4. root@ubuntu:/home/max# cd literature/
  5. root@ubuntu:/home/max/literature# ls
  6. promo

(二) 更改文件夹 cd

工作文件夹与主文件夹的区别

用户每次登录后的默认文件夹就是主文件夹,与系统会话期间保持不变,主文件夹用~表示

  1. xtwy@ubuntu:/root$ cd ~
  2. xtwy@ubuntu:~$ pwd
  3. /home/xtwy

工作文件夹又称当前文件夹,cd命令运行完毕后的文件夹就是工作文件夹,它是能够任意改变的。

  1. //.表示当前文件夹即工作文件夹
  2. //..表示当前文件夹的上一级文件夹
  3. xtwy@ubuntu:~$ cd .
  4. xtwy@ubuntu:~$ cd ..
  5. xtwy@ubuntu:/home$

(三) 删除文件夹 rmdir

rmdir是remove directory的简称,用于删除文件夹,它先删除文件夹下的全部文件,然后再删除该文件夹,但当文件夹下还有子文件夹时。该命令不能运行。须要使用rm命令,比如

  1. //删除temp文件夹,先删除文件夹下的文件
  2. //再删除temp文件夹自身
  3. root@ubuntu:/home/max# rmdir temp/
  4. root@ubuntu:/home/max# rmdir literature/
  5. rmdir: failed to remove `literature/': Directory not empty
  6. root@ubuntu:/home/max# rm -r literature/
  7. root@ubuntu:/home/max# ls
  8. demo names

其中rm -r中的r指的是递归的删除文件夹及文件夹中的文件,因此它具有非常强的破坏力,要慎重使用。

(四) 移动文件夹 mv

  1. //将文件夹demo移到/home/xtwy/文件夹下
  2. root@ubuntu:/home/max# mv demo/ /home/xtwy/
  3. root@ubuntu:/home/max# cd /home/xtwy/
  4. root@ubuntu:/home/xtwy# ls
  5. demo Documents examples.desktop Pictures Templates
  6. Desktop Downloads Music Public Videos
  7. root@ubuntu:/home/xtwy# rmdir demo
  8. //原来文件夹的demo文件夹已经不存在了
  9. root@ubuntu:/home/xtwy# cd /home/max/
  10. root@ubuntu:/home/max# ls
  11. names

(五) 拷贝文件夹 cp

前面用mv命令移动文件夹,有时候须要对文件夹进行拷贝,使用方式例如以下:

  1. //先创建一个演示文件夹。用-p,父文件夹假设不存在将会被创建
  2. root@ubuntu:/home/max# mkdir -p literature/demo
  3. //由于literature还包含子文件夹,此时拷贝不成功
  4. root@ubuntu:/home/max# cp literature/ /home/xtwy/
  5. cp: omitting directory `literature/'
  6. //假设包含子文件夹的话,则加上-r參数,表示递归地拷贝
  7. root@ubuntu:/home/max# cp -r literature/ /home/xtwy/
  8. root@ubuntu:/home/max# cd /homt
  9. bash: cd: /homt: No such file or directory
  10. root@ubuntu:/home/max# cd /home/xtwy/
  11. root@ubuntu:/home/xtwy# ls
  12. Desktop Downloads literature Pictures Templates
  13. Documents examples.desktop Music Public Videos
  14. root@ubuntu:/home/xtwy# cd literature/
  15. root@ubuntu:/home/xtwy/literature# ls
  16. demo

4. 文件操作

(一) 创建文件

直接通过命令行的方式创建文件的方式有多种,常常使用方式例如以下:

  1. //通过echo命令。将输出的命令重定向到文件
  2. root@ubuntu:/home/xtwy# echo "hello linux" > hello.txt
  3. root@ubuntu:/home/xtwy# ls
  4. Desktop Downloads hello.txt Music Public Videos
  5. Documents examples.desktop literature Pictures Templates
  6. //touch命令。怎样文件不存在。会创建文件
  7. root@ubuntu:/home/xtwy# touch hell1.txt
  8. root@ubuntu:/home/xtwy# ls
  9. Desktop Downloads hell1.txt literature Pictures Templates
  10. Documents examples.desktop hello.txt Music Public Videos

(二) 显示文件内容

cate命令能够显示文件内容。它的全称是catenate。意思是将单词一个接一个地连接起来

  1. root@ubuntu:/home/xtwy# cat hello.txt
  2. hello linux

cat命令会将文件里全部的内容全部一次性显示出现,比如

  1. root@ubuntu:/home/xtwy# cat /etc/profile
  2. # /etc/profile: system-wide .profile file for the Bourne shell (sh(1))
  3. # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).
  4. if [ -d /etc/profile.d ]; then
  5. for i in /etc/profile.d/*.sh; do
  6. if [ -r $i ]; then
  7. . $i
  8. fi
  9. done
  10. unset i
  11. ......

有时候我们希望能够分屏查看文件内容,此时能够使用less或more分页程序。less和more的使用方式相差不大,通过空格键显示下一屏信息,它们之间的区别在于less在文件末尾会显示END消息,而more直接返回shell终端。比如:

less命令

more命令

(三) cp命令拷贝文件

  1. root@ubuntu:/home/xtwy# ls
  2. Desktop Downloads hell1.txt literature Pictures Templates
  3. Documents examples.desktop hello.txt Music Public Videos
  4. //拷贝文件
  5. root@ubuntu:/home/xtwy# cp hell1.txt literature/demo
  6. root@ubuntu:/home/xtwy# cd literature/demo
  7. //cd -返回上一次运行的工作文件夹
  8. root@ubuntu:/home/xtwy/literature/demo# cd -
  9. /home/xtwy

须要注意的是cp命令在复制时,假设目标文件夹中已存在该文件,系统不会给出警告,而是直接覆盖。因此它可能存在销毁文件的风险,为解决问题能够使用-i參数让系统给出警告,比如:

  1. root@ubuntu:/home/xtwy# cp -i hell1.txt literature/demo
  2. cp: overwrite `literature/demo/hell1.txt'?

(三) mv命令移动或重命名文件

  1. //在同一文件夹时,相当于文件重命名,运行完毕后hell1.txt不存在
  2. root@ubuntu:/home/xtwy# mv hell1.txt hell2.txt
  3. root@ubuntu:/home/xtwy# ls
  4. Desktop Downloads hell2.txt literature Pictures Templates
  5. Documents examples.desktop hello.txt Music Public Videos
  6. //移动hell2.txt到literature/demo
  7. root@ubuntu:/home/xtwy# mv hell2.txt literature/demo
  8. root@ubuntu:/home/xtwy# cd literature/demo/
  9. root@ubuntu:/home/xtwy/literature/demo# ls
  10. hell1.txt hell2.txt
  11. root@ubuntu:/home/xtwy/literature/demo# cd -
  12. /home/xtwy
  13. //源文件夹hell2.txt已不存在
  14. root@ubuntu:/home/xtwy# ls
  15. Desktop Downloads hello.txt Music Public Videos
  16. Documents examples.desktop literature Pictures Templates

(四)显示文件头部或尾部

显示文件头部内容用head命令。尾部用tail命令,默认显示行数为10

  1. root@ubuntu:/home/xtwy# head ~/.bashrc
  2. # ~/.bashrc: executed by bash(1) for non-login shells.
  3. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
  4. # for examples
  5. # If not running interactively, don't do anything
  6. [ -z "$PS1" ] && return
  7. # don't put duplicate lines in the history. See bash(1) for more options
  8. # ... or force ignoredups and ignorespace
  9. HISTCONTROL=ignoredups:ignorespace
  10. root@ubuntu:/home/xtwy# tail ~/.bashrc
  11. if [ -f ~/.bash_aliases ]; then
  12. . ~/.bash_aliases
  13. fi
  14. # enable programmable completion features (you don't need to enable
  15. # this, if it's already enabled in /etc/bash.bashrc and /etc/profile
  16. # sources /etc/bash.bashrc).
  17. #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
  18. # . /etc/bash_completion
  19. #fi

head及tail的默认行数是能够改动的,比如:

  1. //仅显示前两行
  2. root@ubuntu:/home/xtwy# head -2 ~/.bashrc
  3. # ~/.bashrc: executed by bash(1) for non-login shells.
  4. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)

tail命令在查看日志文件内容增长时可能常常会使用,比如在hadoop启动之后,会产生很多日志,但出现故障时,能够採用tail命令动态地监測日志文件内容的增长。查看问题出在哪个地方。

  1. //初始显示情况
  2. root@ubuntu:/home/xtwy# tail -f hello.txt
  3. hello linux
  4. //向文件里追加内容
  5. root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt
  6. //追加后的输出情况
  7. root@ubuntu:/home/xtwy# tail -f hello.txt
  8. hello linux
  9. hello linux linux

(五)其他常见文件操作命令

以下的命令都不会改变文件内容

  1. root@ubuntu:/home/xtwy# cp hello.txt hello1.txt
  2. root@ubuntu:/home/xtwy# ls
  3. Desktop Downloads hello1.txt literature Pictures Templates
  4. Documents examples.desktop hello.txt Music Public Videos
  5. //依据文件内容排序
  6. root@ubuntu:/home/xtwy# sort hello1.txt
  7. hello linux
  8. hello linux linux
  9. //逆序输出
  10. root@ubuntu:/home/xtwy# sort -r hello1.txt
  11. hello linux linux
  12. hello linux
  13. //diff进行内容比較
  14. root@ubuntu:/home/xtwy# diff hello1.txt hello.txt
  15. //向文件里追加内容
  16. root@ubuntu:/home/xtwy# echo "hello linux linux" >> hello.txt
  17. //内容比較
  18. root@ubuntu:/home/xtwy# diff hello1.txt hello.txt
  19. 2a3
  20. > hello linux linux
  21. //格式化输出
  22. //-u參数将文件分成多块
  23. //比較的两个文件分别用-、+表示
  24. //本例中 -表示hello1.txt,+表示hello.txt
  25. root@ubuntu:/home/xtwy# diff -u hello1.txt hello.txt
  26. --- hello1.txt 2015-08-22 17:28:44.071202558 -0700
  27. +++ hello.txt 2015-08-22 17:29:49.131181281 -0700
  28. //@@xxx@@用于标识行起始编号、行数
  29. //-1,2表示 hello1.txt文件起始编号为1,行数为2
  30. //+1,3表示 hello.txt文件起始编号为1。行数为3
  31. @@ -1,2 +1,3 @@
  32. hello linux
  33. hello linux linux
  34. +hello linux linux

加入公众微信号。能够了解很多其他最新Spark、Scala相关技术资讯

Spark修炼之道(基础篇)——Linux大数据开发基础:第二节:Linux文件系统、文件夹(一)的更多相关文章

  1. Spark修炼之道(基础篇)——Linux大数据开发基础:第三节:用户和组

    本节主要内容 理解用户和组的概念 用户管理 组管理 权限分配 1. 理解用户和组的概念 在第一讲中我们提到.linux是一种多任务.多用户的操作系统,在讲ls -l命令行我们看到例如以下文件具体信息: ...

  2. Spark修炼之道——Spark学习路线、课程大纲

    课程内容 Spark修炼之道(基础篇)--Linux基础(15讲).Akka分布式编程(8讲) Spark修炼之道(进阶篇)--Spark入门到精通(30讲) Spark修炼之道(实战篇)--Spar ...

  3. 大数据开发-Spark-拷问灵魂的5个问题

    1.Spark计算依赖内存,如果目前只有10g内存,但是需要将500G的文件排序并输出,需要如何操作? ①.把磁盘上的500G数据分割为100块(chunks),每份5GB.(注意,要留一些系统空间! ...

  4. Linux 网络协议栈开发基础篇—— 网桥br0

    一.桥接的概念 简单来说,桥接就是把一台机器上的若干个网络接口"连接"起来.其结果是,其中一个网口收到的报文会被复制给其他网口并发送出去.以使得网口之间的报文能够互相转发. 交换机 ...

  5. 一篇了解大数据架构及Hadoop生态圈

    一篇了解大数据架构及Hadoop生态圈 阅读建议,有一定基础的阅读顺序为1,2,3,4节,没有基础的阅读顺序为2,3,4,1节. 第一节 集群规划 大数据集群规划(以CDH集群为例),参考链接: ht ...

  6. JavaEE 学大数据是否掌握 JavaSE 和 Linux 就够了?

    引言 如果你是学习大数据的童靴,可能经常在网上看到一些公众号或博客告诉你,学习大数据基础部分只需要掌握 JavaSE 和 Linux 就够了,至于 JavaWeb 和 JavaEE 简单了解一下就可以 ...

  7. 大数据入门基础系列之Hadoop1.X、Hadoop2.X和Hadoop3.X的多维度区别详解(博主推荐)

    不多说,直接上干货! 在前面的博文里,我已经介绍了 大数据入门基础系列之Linux操作系统简介与选择 大数据入门基础系列之虚拟机的下载.安装详解 大数据入门基础系列之Linux的安装详解 大数据入门基 ...

  8. 大数据开发实战:Spark Streaming流计算开发

    1.背景介绍 Storm以及离线数据平台的MapReduce和Hive构成了Hadoop生态对实时和离线数据处理的一套完整处理解决方案.除了此套解决方案之外,还有一种非常流行的而且完整的离线和 实时数 ...

  9. Spark 介绍(基于内存计算的大数据并行计算框架)

    Spark 介绍(基于内存计算的大数据并行计算框架)  Hadoop与Spark 行业广泛使用Hadoop来分析他们的数据集.原因是Hadoop框架基于一个简单的编程模型(MapReduce),它支持 ...

随机推荐

  1. Vue 数组和对象更新,但是页面没有刷新

    在使用数组的时候,数组内部数据发生改变,但是与数组绑定的页面的数据却没有发生变化. <ul> <li v-for="(item,index) in todos" ...

  2. 「二叉搜索树 / set / 朝鲜树 / 替罪羊树」快速排序

    要求 给定n个数,对这n个数进行排序 这题当然可以直接调用sort #include<cstdio> #include<vector> #define ll long long ...

  3. php+nginx 限制上传文件大小

    问题:在后台上传8M大小的图片,上传不成功 nginx返回413,如下图所示: 分析:nginx配置文件或者php中,可上传的大小设置太小了 解决办法------检查nginx和php的配置文件里面的 ...

  4. Luogu P4299 首都 LCT

    既然是中文题目,这里便不给题意. 分析: 这个题的做法据说是启发式合并? 但是我不会啊…… 进入正题,LCT是怎样做掉这道题的.记得在前面的一篇<大融合>的题解中,介绍过LCT维护子树信息 ...

  5. [Luogu] P3846 [TJOI2007]可爱的质数

    题目描述 给定一个质数P(2<=P<2312^{31}231),以及一个整数B(2<=B<P),一个整数N(2<=N<P). 现在要求你计算一个最小的L,满足BL≡ ...

  6. Python中的列表(3)

    我们创建的列表元素的顺序是无法预测的,因为我们无法控制用户提供数据的顺序. 为了组织列表中的元素,所以Python帮我们提供一些方法用来排序列表中的元素. 1.方法 sort() 可以对列表永久性排序 ...

  7. Data of Ch5 --Dual rotor

    * Results *Conclusion*- little effect of rear rotor on Cp_1- Cp1 is independent of TI** TI effect on ...

  8. 添物不花钱学JavaEE(基础篇)- Tomcat

    Tomcat是大家常用的Java Web容器. 添物网使用的也是Tomcat. 官方网址: http://tomcat.apache.org/ 官方文档看看. 可以看的图书 <Tomcat权威指 ...

  9. 鳥哥的 Linux 私房菜

    RootKit Hunter 後端偵測軟體之架設與執行 切換解析度為 800x600 最近更新日期:2004/11/16 由前面幾個章節的說明,我們可以曉得因為主機的某些服務是有漏洞的, 黑客們可以針 ...

  10. [luoguP1056] 排座椅(sort + 模拟)

    传送门 nc题,一直sort就过了 代码 #include <cstdio> #include <iostream> #include <algorithm> #d ...