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

网友投稿 617 2022-05-28

目录

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操作系统的产生过程

注意:linux之所以叫做linux,这个名字是由linus和unix组合而来的。

3、linux优点

1)分时多用户和多任务的操作系统;

2)多数网络协议支持、方便的远程管理

3)强大的内存管理和文件系统管理;

4)大量可用的软件和免费的软件;

5)优良的稳定性和安全性;

6)良好的可移植性和灵活性;

7)可供选择的厂商多;

4、linux体系结构

5、linux内核及发行版本

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

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

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

发行版本:

6、文件系统和目录结构

Windows : NTFS(FAT16 FAT32) Linux: centos5 : ext3 文件系统 centos6 : ext4 文件系统 centos7 : xfs 文件系统

① linux中所有的内容,都是文件。

② 目录或者文件的名称长度不超过255个字符,文件或目录是由以下字符构成:

大写字母(A-Z)、小写字母(a-z)、数字(0-9)

下划线(_) 、句点(.) 、逗号(,)

注意:文件和目录是区分大小写的。

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

④ 目录结构

树结构如下:

注意:/(根目录)底下的二级目录不要删除、也不要添加目录。

使用建议:

7、终端和常用命令

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

为大家奉上linux命令大全宝贝网站:http://man.linuxde.net/

8、磁盘命令

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

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

-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

-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 .

-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、文件命令

-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

[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

-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

-p:递归删除空目录

操作如下:

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

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

注意:某个文件或目录被复制后,原始文件或目录依然存在。

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~

-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

-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

-n:显示文件行号。(这是cat常用的)

cat :查看文件中所有内容,正着查看。

tac :查看文件中所有内容,倒着查看。

操作如下:

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

cat配合EOF命令,有一个比较厉害的操作:

q键:退出more命令。

Space键:显示文本的下一屏内容。

Enter键:只显示文本的下一行内容。

h键:显示帮助屏,该屏上有相关的帮助信息。

b键:显示上一屏内容。

q键:退出less命令。

G:跳到文件末尾

gg:跳到文件首行

按e键:向上滚动一行

按y键:向下滚动一行

操作如下:

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

结果如下:

-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

硬链接类似于【复制】;

软连接类似于【创建快捷方式】(常用);

为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

“定义别名的意义在于,可以用别名,代替某些组合命令,减少敲代码;”

查看别名:alias

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

定义别名: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/

Linux Unix

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:企业云盘与OBS对象存储服务对比
下一篇:linux 查看磁盘空间大小
相关文章