Linux系列:Linux常用磁盘命令、文件命令!

举报
数据分析与统计学之美 发表于 2021/09/27 20:46:04 2021/09/27
【摘要】 linux系列第一课

目录

1、知识引入
2、linux操作系统的产生过程
3、linux优点
4、linux体系结构
5、linux内核及发行版本
6、文件系统和目录结构
7、终端和常用命令
8、磁盘命令
9、文件命令
10、其他常用命令
11、案例讲解

1、知识引入

windows 桌面系统/服务器端
linux 服务器端
mac/unix
android/ios
桌面系统:主要是面对大众用户,以美观、易用为原则,有图形化界面。
服务器端:知道使用命令行进行调试的那个小黑窗口,就知道什么是服务器端。

刚刚买回来的计算机:仅有硬件;

  • 硬件:主要包括CPU、内存、主板、硬盘驱动器、光盘驱动器、各种扩展卡、连接线、电源等;外部设备包括鼠标、键盘等。

只有装上了操作系统、应用软件后,才使得电脑世界变得丰富多彩;

  • 仅有硬件(裸机)
  • 操作系统软件:windows系统
  • 应用软件:qq、微信

操作系统的作用是什么?为什么不在硬件上面直接安装“应用软件”,还需要安装一个操作系统呢?

  • 操作系统可以提供磁盘管理、内存管理、资源调度,避免杂乱无章。

什么是linux?

  • 1)Linux操作系统属于开源操作系统,主要应用于服务器端。
  • 2)基于posix和unix的多用户、多任务、多线程和多cpu的操作系统。
  • 3)Linux继承了UNIX以网络为核心的设计思想,是一个性能稳定的多用户的网络操作系统。

2、linux操作系统的产生过程

在这里插入图片描述

1)什么是GPL条款?

在这里插入图片描述

2)linux作者:李纳斯(Linus)

在这里插入图片描述
注意:linux之所以叫做linux,这个名字是由linus和unix组合而来的。

3、linux优点

1)分时多用户和多任务的操作系统;
2)多数网络协议支持、方便的远程管理;
3)强大的内存管理和文件系统管理;
4)大量可用的软件和免费的软件;
5)优良的稳定性和安全性;
6)良好的可移植性和灵活性;
7)可供选择的厂商多;

4、linux体系结构

在这里插入图片描述

5、linux内核及发行版本

Linux内核:内核建立了计算机软件与硬件之间通讯的平台,内核提供系统服务,比如文件管理、虚拟内存、设备I/O、进程管理等。“软件调用内核,内核操作硬件”

内核版:是系统的心脏,用于运行程序和管理硬件的核心。内核通过系统调用来管理计算机硬件,而终端命令用来检测系统调用是否正确,使得更加方便的调用计算机硬件。系统调用主要是方便程序间接访问计算机硬件。

发行版:发行版包含了Linux内核,发行版就是在Linux内核之上再搭建一套应用程序,有桌面管理程序,视频播放程序等一套程序。因为Linux内核已经实现了跟计算机硬件进行交互,所以发行版可以通过系统内核的系统调用来跟计算机硬件进行访问,大大缩短了开发系统的时间。

发行版本:
在这里插入图片描述

6、文件系统和目录结构

1)文件系统:确定文件的存储和管理方式

Windows : NTFSFAT16 FAT32)
Linux:  centos5 : ext3 文件系统
        centos6 : ext4 文件系统
        centos7 : xfs  文件系统

2)Linux文件系统说明

① linux中所有的内容,都是文件。
② 目录或者文件的名称长度不超过255个字符,文件或目录是由以下字符构成:

  • 大写字母(A-Z)、小写字母(a-z)、数字(0-9)
  • 下划线(_) 、句点(.) 、逗号(,)
  • 注意:文件和目录是区分大小写的。

③ Linux文件系统没有扩展名,添加扩展名仅仅是帮助我们区分,不同的文件表示的是什么,文件的真实类型(文件还是目录)是通过【文件中的内容】决定的。

④ 目录结构
在这里插入图片描述
树结构如下:
在这里插入图片描述
注意:/(根目录)底下的二级目录不要删除、也不要添加目录。
在这里插入图片描述
使用建议:
在这里插入图片描述

7、终端和常用命令

1)终端:提供用户与内核交互工具。

在这里插入图片描述

2)Linux中【命令的格式】

在这里插入图片描述

3)命令的使用举例

[root@image_boundary ~]# ls -l anaconda-ks.cfg
-rw------- 1 root root 1598 Sep 17 15:57 anaconda-ks.cfg

4)常见的命令

在这里插入图片描述
为大家奉上linux命令大全宝贝网站:http://man.linuxde.net/

8、磁盘命令

1)cd命令:目录切换

cd     相对路径/绝对路径。利用相对路径或绝对路径,切换到某个目录之下;
cd .   表示当前路径;
cd ..  返回上一层目录;
cd ~   若为root用户,就是切换到root用户的家目录【/root】;
       若为普通用户,就是切换到普通用户的家目录【/home/普通用户名】;
cd /   直接返回到"/"根目录;
cd -   返回上一次操作目录;"(这个很好用)"

操作如下:

[root@image_boundary ~]# cd /
[root@image_boundary /]# cd home
[root@image_boundary home]# cd ../usr
[root@image_boundary usr]# cd -
/home
[root@image_boundary home]# cd ..
[root@image_boundary /]# pwd
/
[root@image_boundary ~]# su hadoop
[hadoop@image_boundary root]$ cd ~
[hadoop@image_boundary ~]$ pwd
/home/hadoop

2)pwd:显示当前工作目录

[root@image_boundary /]# pwd
/
[root@image_boundary /]# cd ~
[root@image_boundary ~]# pwd
/root
[hadoop@image_boundary ~]$ pwd
/home/hadoop

3)ls命令:查看目录内容

  • -l:展示详细信息
  • -a:查看隐藏内容。(显示以.开头的那些文件)
  • -A:查看隐藏文件。(显示.开头的那些文件,与a不同的是不显示.和…)。“注意:.表示当前文件夹 …表示上一级文件夹”
  • -h:友好的方式展示(文件以K、M…结尾)
  • -R:递归展示所有文件。
  • 常用组合操作:ls -l 可以简化为ll。ll -h 显示文件的具体信息,并以友好的方式展示。

操作如下:

[root@image_boundary ~]# ls -l       可以写成ll
total 108
-rw-r--r--. 1 root root     9 Sep 17 19:10 aa.txt~
-rw-------. 1 root root  1598 Sep 17 15:57 anaconda-ks.cfg
[root@image_boundary ~]# ls -lh      可以写成ll -h
total 108K
-rw-r--r--. 1 root root    9 Sep 17 19:10 aa.txt~
-rw-------. 1 root root 1.6K Sep 17 15:57 anaconda-ks.cfg

4)du命令:查看每个文件和目录的磁盘使用空间。

  • -a:展示所有内容
  • -h:友好方式展示
  • -s:统计所有文件所占用空间总的大小
  • du -h:会递归显示当前文件夹及其子文件夹的占用空间大小。
  • du -h a.txt:会显示指定文件a.txt的占用空间大小。
  • du -h aa:会显示指定文件夹aa的占用空间大小。
  • du -sh:会显示当前文件夹下,所以文件的占用空间大小

操作如下:

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
[root@image_boundary Desktop]# du -sh
28K .

5)df命令:查看磁盘使用率

  • -h:友好方式展示
  • -a:显示全部的文件系统;
  • 注意:“df -h用于显示磁盘空间的使用情况,以及剩余的磁盘空间大小。”

操作如下:

[root@image_boundary Desktop]# df -h
文件系统               容量  已用  可用  已用%  挂载点
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/vg_image-lv_root
                       35G  3.6G   30G  11% /
tmpfs                 931M  296K  931M   1% /dev/shm
/dev/sda1             477M   42M  411M  10% /boot
/dev/sr0              3.7G  3.7G     0 100% /media/CentOS_6.9_Final

9、文件命令

1)mkdir:创建目录

  • -p:递归创建目录

操作如下:

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
[root@image_boundary Desktop]# mkdir cc
[root@image_boundary Desktop]# ll
total 12
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:21 cc

# 不写-p会报错。
[root@image_boundary Desktop]# mkdir dd/ee     
mkdir: cannot create directory `dd/ee': No such file or directory

# 参数-p表示递归产生一个目录。
# 创建一个递归目录dd/ee。
[root@image_boundary Desktop]# mkdir -p dd/ee
[root@image_boundary Desktop]# cd dd
[root@image_boundary dd]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 20:22 ee

2)touch:创建文件

[root@image_boundary Desktop]# ll
total 16
drwxr-xr-x. 2 root root 4096 Oct 15 20:02 aa
drwxr-xr-x. 2 root root 4096 Oct 15 20:05 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:21 cc
drwxr-xr-x. 3 root root 4096 Oct 15 20:22 dd
[root@image_boundary Desktop]# cd cc
[root@image_boundary cc]# ll
total 0
[root@image_boundary cc]# touch sum.txt
[root@image_boundary cc]# ll
total 4
-rw-r--r--. 1 root root 0 Oct 15 20:28 sum.txt

3)file:查看文件类型

  • -b:不显示文件名,只显示文件类型。

操作如下:

"file 文件名:可以显示其到底是一个文件,还是一个目录"
[root@image_boundary ~]$ file aa
aa: directory
[root@image_boundary cc]# ll
total 0
-rw-r--r--. 1 root root 0 Oct 15 20:28 sum.txt

"查看当前文件夹的类型(这里指的就是cc):文件夹"
[root@image_boundary cc]# file .      
.: directory    

"如果sum.txt中没有内容,显示empty"
[root@image_boundary cc]# file sum.txt   
./sum.txt: empty

"如果sum.txt中有内容,显示sum.txt的文件类型"
[root@image_boundary cc]# file sum.txt  
./sum.txt: ASCII text
[root@image_boundary cc]# file -b sum.txt
ASCII text

4)rmdir:删除空目录

  • -p:递归删除空目录

操作如下:

"注意以下两个命令的区别"
rmdir dd/ee/ff      表示删除dd下面ee下面的这一个空目录ff。
rmdir -p dd/ee/ff   表示同时递归删除dd/ee/ff这3个目录。

5)rm:删除文件或目录

  • rm 文件名
  • -i:询问。(这个是默认情况,不写就是表示要询问)
  • -r:递归删除。
  • -f:强制删除(不提示删除)。
  • “下面这条命令:慎用!!!除非真的知道你在干嘛。”
  • rm -rf 目录/文件

操作如下:

[root@image_boundary bb]# ll
total 12
-rw-r--r--. 1 root root 149 Oct 15 20:00 a.txt
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt
[root@image_boundary bb]# rm a.txt
rm: remove regular file `a.txt'? n
[root@image_boundary bb]# ll
total 12
-rw-r--r--. 1 root root 149 Oct 15 20:00 a.txt
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt
[root@image_boundary bb]# rm a.txt 
rm: remove regular file `a.txt'? y
[root@image_boundary bb]# ll
total 8
-rw-r--r--. 1 root root  95 Oct 15 20:02 b.txt
-rw-r--r--. 1 root root 138 Oct 15 20:02 c.txt

"rm -rf bb会删除bb目录下所有的目录和文件,问都不问,毫不留情。一定不要轻易使用该命令"
[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 20:53 bb
drwxr-xr-x. 2 root root 4096 Oct 15 20:30 cc
[root@image_boundary Desktop]# rm -rf bb
[root@image_boundary Desktop]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 20:30 cc

6)cp:复制文件或目录

  • 注意:某个文件或目录被复制后,原始文件或目录依然存在。
  • cp 源文件(目录) 目标文件(目录)
  • -i:提示。
  • -r/-R参数:当【复制目录】的时候,必须用到这个参数。
  • -r/-R:递归复制目录。
  • -f参数:在搭建集群时,修改时区的时候用到
  • -f:覆盖已存在的目标文件,而不给出提示。

① 同一文件,复制到同一目录下,需要改名;否则,会报错。

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:00 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:00 bb
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# ll
total 4
-rw-r--r--. 1 root root 21 Oct 15 21:00 sum.txt
-rw-r--r--. 1 root root  0 Oct 15 20:59 sum.txt~
"把当前目录下的sum.txt文件,复制到当前目录下。"
"假如不修改文件名,会报错。"
[root@image_boundary aa]# cp sum.txt sum.txt  
cp: `sum.txt' and `sum.txt' are the same file
"修改文件名后,才不会报错。"
[root@image_boundary aa]# cp sum.txt sum1.txt
[root@image_boundary aa]# ll
total 8
-rw-r--r--. 1 root root 21 Oct 15 21:04 sum1.txt
-rw-r--r--. 1 root root 21 Oct 15 21:00 sum.txt
-rw-r--r--. 1 root root  0 Oct 15 20:59 sum.txt~

② 同一文件,复制到不同目录下,不需要改名;

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:04 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:00 bb
"将aa目录下的sum.txt文件,复制到bb目录下,不需要修改名称。"
[root@image_boundary Desktop]# cp aa/sum.txt bb/
[root@image_boundary Desktop]# cd bb
[root@image_boundary bb]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:01 bbb
-rw-r--r--. 1 root root   21 Oct 15 21:07 sum.txt

③ 递归复制bb目录中的东西(既包括文件,也包括目录),到aa目录中去。

"由于复制目录,因此必须使用参数【-r/-R】"
[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:52 aa
drwxr-xr-x. 3 root root 4096 Oct 15 21:07 bb
"将bb目录复制到aa目录下。"
"注意:复制目录的时候,必须要使用参数-r或者-R"
[root@image_boundary Desktop]# cp -r bb aa/
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# ll
total 4
drwxr-xr-x. 3 root root 4096 Oct 15 21:53 bb
-rw-r--r--. 1 root root    0 Oct 15 20:59 sum.txt~

7)mv:移动某个文件或目录

  • -i:提示。
  • -f:强制移动。
  • -u:新覆盖旧,不存在时移动。

① 将同一个文件,移动到同级目录下,必须修改文件名,效果相当于重命名。

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 21:55 aa
drwxr-xr-x. 2 root root 4096 Oct 15 21:54 bb
[root@image_boundary Desktop]# mv bb/sum.txt bb/sum1.txt

② 将同一个文件,移动到不同目录下,如果没有指定新文件名,效果相当于移动。

  • 此操作,仅相当于把sum.txt移动到aa目录下。
[root@image_boundary Desktop]# mv bb/sum.txt aa/

③ 将同一个文件,移动到不同目录下,可以不用修改文件名。

  • 如果指定了新文件名,效果相当于移动+重命名。
[root@image_boundary Desktop]# mv bb/sum.txt aa/sum.txt
"把bb目录下的sum.txt文件,移动到aa目录下,并重命名为sum1.txt。"
[root@image_boundary Desktop]# mv bb/sum.txt aa/sum1.txt

8)head/tail:展示开头或者结尾的若干行,默认10行

  • -n :指定行数;
  • head :查看文件中所有内容;
  • tail :查看文件中所有内容;
  • head -n:查看前n行数据;
  • tail -n:查看后n行数据;
  • “在计划任务那里会用下面这个参数。”
  • “在hadoop集群里面,查看log日志文件的时候,会用到。”
  • tail -f:查看新追加内容;

操作如下:

[root@image_boundary Desktop]# ll
total 8
drwxr-xr-x. 2 root root 4096 Oct 15 22:35 aa
drwxr-xr-x. 2 root root 4096 Oct 15 22:30 bb
[root@image_boundary Desktop]# head -3 aa/sum.txt
aaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbb
ccccccccccccccccc
[root@image_boundary Desktop]# tail -3 aa/sum.txt
mmmmmmmmmmmmmmmmm
nnnnnnnnnnnnnnnnn
ooooooooooooooooo

9)cat/tac:查看文件内容

  • -n:显示文件行号。(这是cat常用的)
  • cat :查看文件中所有内容,正着查看。
  • tac :查看文件中所有内容,倒着查看。

操作如下:

[root@image_boundary Desktop]# cat -n aa/sum.txt
     1 1
     2 2
     3 3
     4 4

cat配合EOF命令,有一个比较厉害的操作:
在这里插入图片描述

10)more:分屏显示,每次显示一屏

  • q键:退出more命令。
  • Space键:显示文本的下一屏内容。
  • Enter键:只显示文本的下一行内容。
  • h键:显示帮助屏,该屏上有相关的帮助信息。
  • b键:显示上一屏内容。

11)less:分屏上下翻页浏览文件内容

  • q键:退出less命令。
  • G:跳到文件末尾
  • gg:跳到文件首行
  • 按e键:向上滚动一行
  • 按y键:向下滚动一行

操作如下:

less -N aa/sum.txt  此参数会显示文件内容的行数

结果如下:
在这里插入图片描述

12)echo:输出字符串或者变量

-e:处理特殊的符号。eg:写了-e,就会把\n当成换行符,否则不会。
echo $PWD :输出变量的绝对路径   
">表示覆盖源文件中的内容"     
echo   aa  > a.txt     
">>表示追加到源文件末尾"
echo   aa >> a.txt   

操作如下:

[root@image_boundary Desktop]# echo "aa\nbb"
aa\nbb
[root@image_boundary Desktop]# clear
"-e参数,表示让系统能够识别特殊字符"
[root@image_boundary Desktop]# echo -e "aa\nbb"
aa
bb
[root@image_boundary Desktop]# echo -e "aa\tbb"
aa bb
==================================================
"echo $PWD表示输出当前文件夹的绝对路经,很有用。"
[root@image_boundary Desktop]# echo $PWD
/root/Desktop
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# echo $PWD
/root/Desktop/aa
[root@image_boundary aa]# cd ../bb
[root@image_boundary bb]# echo $PWD
/root/Desktop/bb
=================================================
[root@image_boundary Desktop]# echo -e '666\n888' > ./aa/aa.txt
[root@image_boundary Desktop]# echo -e '中国人' >> ./aa/aa.txt

13)ln:创建硬链接和软链接

  • 硬链接类似于【复制】;
  • 软连接类似于【创建快捷方式】(常用);
  • 为f1 创建硬链接 f2 :ln f1 f2
  • 为f1 创建软链接(也叫符号链接) f3 :ln -s f1 f3

操作如下:

[root@image_boundary Desktop]# ll
total 4
drwxr-xr-x. 2 root root 4096 Oct 15 23:28 aa
"创建一个文件test.txt"
[root@image_boundary Desktop]# cd aa
[root@image_boundary aa]# touch test.txt
"创建一个硬链接,相当于是复制了该文件"
[root@image_boundary aa]# ln test.txt test
"创建一个软连接,相当于给该文件创建了一个快捷方式"
[root@image_boundary aa]# ln -s test.txt s_test
[root@image_boundary aa]# echo "I am a student" >> test.txt
[root@image_boundary aa]# cat s_test
I am a student
[root@image_boundary aa]# cat  test
I am a student
"删除源文件test.txt后"
[root@image_boundary aa]# rm -rf test.txt
"软连接文件(快捷方式)会消失"
[root@image_boundary aa]# cat s_test
cat: s_test: No such file or directory
"但是这个硬链接(复制)不会消失"
[root@image_boundary aa]# cat test
I am a student

14)alias:查看某些命令的别名

  • “定义别名的意义在于,可以用别名,代替某些组合命令,减少敲代码;”
  • 查看别名:alias
  • 定义别名:alias la= ‘ll -a’
  • 取消别名:unalias la

操作如下:

[root@image_boundary Desktop]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

10、其他常用命令

clear 或 ctrl+l :清屏
history :显示历史代码命令
su :切换用户。$普通用户;#管理员root用户
hostname :显示主机名
sudo :以root用户权限执行一次命令(目前不太明白这个)
exit :退出当前登录状态。当前是普通用户切换到root用户;当前是root用户就切换到普通用户。
who :显示目前有哪些用户登入系统
| :管道符,表示把前面命令内容的输出,当做后面命令的输入。

11、案例讲解

在这里插入图片描述
操作如下:

"首先,创建这3个文件夹;"
[root@image_boundary Desktop]# mkdir myFile   
[root@image_boundary Desktop]# mkdir myPic
[root@image_boundary Desktop]# mkdir backup
[root@image_boundary Desktop]# ll
total 16
drwxr-xr-x. 2 root root 4096 Oct 15 23:36 aa
drwxr-xr-x. 2 root root 4096 Oct 15 23:57 backup
drwxr-xr-x. 2 root root 4096 Oct 15 23:56 myFile
drwxr-xr-x. 2 root root 4096 Oct 15 23:56 myPic
"分别在myFile、backup分别创建一个a.txt;"
[root@image_boundary Desktop]# cd myFile
[root@image_boundary myFile]# touch a.txt
[root@image_boundary myFile]# cd ..
[root@image_boundary Desktop]# cd backup
[root@image_boundary backup]# mkdir a.txt
"删除backup这个文件夹及其其中的内容;"
[root@image_boundary Desktop]# rm -rf backup
"将同一个文件夹移动到同级目录下,相当于修改名字;"
[root@image_boundary Desktop]# mv ./myFile ./File
"将File目录下的a.txt文件,移动到myPic目录下;"
[root@image_boundary Desktop]# cp ./File/a.txt ./myPic/
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。