2024.10.29- Linux(CentOS7)笔记(1)

一、Linux文件系统的介绍

Linux的文件系统与Unix的文件系统类似,也是一个树形结构。 最基本的是根目录:/ 。相当于windows文件系统的盘符。

复制代码
/: 根路径,  根路径下有以下文件夹
​
/bin -> usr/bin   :  存储的是用户级别的指令文件  
/boot             :  用于存储操作系统的启动文件
/dev              :  用来挂载其他设备的,
/etc              :  系统配置文件
/home             :  除了超级管理员,其他用户的家的位置。
                    比如:  lucy用户的家的位置 /home/lucy/
                           michael用户的家的位置/home/michael/
/lib -> usr/lib      : 系统资源库
/lib64 -> usr/lib64  : 系统资源库
/media
/mnt
/opt
/proc
/root             :   超级管理员root的家。
/run
/sbin -> usr/sbin :  超级命令所在的位置
/srv
/sys              :  一些系统文件的位置
/tmp
/usr
/var

二、Linux的基础命令

2.1 命令格式的说明

复制代码
1. 进入命令行界面的提示符解析
   [root@localhost ~]#
   root位置:  登录用户名
   @: 连接符号
   localhost位置:  本机的主机名 
   ~位置:   当前的所在位置
   #位置:  表示是超级管理员还是普通用户
           超级管理员则使用#
           普通用户则使用$
    
   eg: 如果登录名为scott,主机名为linux01, 当前位置为/home/soft/tencent/wechat/
       那么提示符的样子:
       [scott@linux01 wechat]$ 
2. 命令格式的说明(格式标明时的中括号一般情况下都表示可有可无)
     
    格式如下:      
     [scott@linux01 wechat]$   command [-option][...] [参数]
    eg: ls  -la  /usr
​
    说明: 
        - 大部分命令遵从该格式
        - 多个选项时,可以一起写   
            eg: ls –l –a  ls –la
        - 简化选项与完整选项(注:并非所有选项都可使用完整选项)   
            eg: ls –all   ls –a

2.2 三个最常用的指令

复制代码
1. pwd :  print current work directory三个单词的简写
          作用就是以绝对路径的形式显示当前的位置所在
          eg:
          [root@localhost network-scripts]# pwd
          /etc/sysconfig/network-scripts
          [root@localhost ~]# pwd
          /root
2. ls :   list directory contents的简写
          作用,就是列出指定目录下的内容(文件,子目录等)
          eg:  ls   默认列出当前工作空间里的内容
          
          常用的选项:  
            -l :  列出每个子文件的属性详情     
            -a :  显示所有的内容,包括隐藏的
            -S :  以大小进行降序排序显示, 尽量与-h,-l一起使用
            -h:   以方便人类可读的显示效果显示大小的单位,比如k,MB,G
         eg:  ls -l 显示属性详情,  可以简化写: ll
         eg:  ls -a 显示所有包括隐藏的内容
         eg:  ls -l -a 显示所有包括隐藏的内容的属性详情   ,简化写:ll -a  或者ls -la
         eg:  ls -lhS  显示当前目录下的所有内容,并降序排序
         eg:  ls -lhS /etc  显示指定目录/etc下的所有内容,并降序排序
         
         
3. cd :  change directory的简写,  切换工作空间。
         注意:是一个特殊指令,特殊在是一个shell内置指令。
         
         cd  [target directory]
         eg:  cd       回家
         eg:   cd ~     回家
         eg:   cd -    回到上一次的位置
         eg:   cd /etc   切换到/etc下
         eg:   cd ..   回到上一级目录(父目录)
               cd .  表示不动

扩展1:属性详情

复制代码
【-rw-------】 【1】   【root】 【root】 【1259】 【11月 29 19:03】 【anaconda-ks.cfg】
文件类型+权限  硬链接数  owner    group   文件大小  最后一次访问时间     文件名

扩展2:绝对和相对路径

复制代码
在计算机中,路径有两种写法,分别是绝对路径和相对路径
​
-- 绝对路径:  从根开始书写的路径。  Linux的根的写法,就是一个斜杠  /  
             eg:  /home/scott/app/tencent/qq/bin/qq.sh
-- 相对路径:  从当前目录下开始书写的路径。
          
          .   :   当前目录的表示方式    注意: 书写相对路径时,当前目录可以省略不写
          ..  :   上一级目录的表示方式
          
          eg:  假如工作空间位于:/home/scott/app/tencent/qq/bin/目录下
               
               问题1:  切换到app目录下
                       绝对路径的写法: cd /home/scott/app/
                       相对路径的写法: cd ./../../../
                                      cd ../../../
                                      
               问题2:  切换到/home/michael/app/目录下
                       绝对路径的写法: cd /home/michael/app
                       相对路径的写法: cd ../../../../../michael/app/
​
​
         小贴士:  如果不涉及移动操作,那么绝对路径书写起来简单。
                  如果涉及到项目移动,项目部署,则应该使用相对路径

2.3 帮助指令

man指令:

复制代码
作用:查看指定命令的帮助文档
语法: man  指令
eg:   man ls
      man pwd
      man cd

help指令

复制代码
作用:查看指定命令的主题信息
语法:  help  指令

注意: 不是所有的指令都有主题信息

info指令

复制代码
作用:用来查看指令命令的详细信息
语法: info 指令

2.4 文件处理指令

2.4.1 touch

复制代码
作用:用于创建一个空文件
语法: touch  filename.....
eg:  
[root@localhost etc]# touch file1 file2 file3
[root@localhost etc]# pwd
/etc
[root@localhost etc]# touch ~/file4
[root@localhost etc]# touch ~/{file5,file6}
[root@localhost etc]# ls ~
anaconda-ks.cfg  file1  file2  file3  file4  file5  file6

2.4.2 mkdir

复制代码
作用:用于创一个目录
语法:mkdir [-p]  dirname.....
​
[root@localhost ~]# mkdir dir1
[root@localhost ~]# mkdir ./dir2 ./dir3
[root@localhost ~]# cd /etc
[root@localhost etc]# mkdir ~/{dir4,dir5}
[root@localhost etc]# mkdir ~/dir6/dir66       #会报错,因为dir6不存在,所以不能创建dir66
[root@localhost etc]# mkdir -p ~/dir6/dir66    #表示多层级创建目录
[root@localhost etc]# ls -l ~
总用量 4
-rw-------. 1 root root 1259 11月 29 19:03 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 11月 29 22:32 dir1
drwxr-xr-x. 2 root root    6 11月 29 22:33 dir2
drwxr-xr-x. 2 root root    6 11月 29 22:33 dir3
drwxr-xr-x. 2 root root    6 11月 29 22:33 dir4
drwxr-xr-x. 2 root root    6 11月 29 22:33 dir5
drwxr-xr-x. 3 root root   19 11月 29 22:34 dir6
-rw-r--r--. 1 root root    0 11月 29 22:28 file1
-rw-r--r--. 1 root root    0 11月 29 22:28 file2
-rw-r--r--. 1 root root    0 11月 29 22:28 file3
-rw-r--r--. 1 root root    0 11月 29 22:29 file4
-rw-r--r--. 1 root root    0 11月 29 22:30 file5
-rw-r--r--. 1 root root    0 11月 29 22:30 file6
[root@localhost etc]# ls ~/dir6
dir66

2.4.3 rm

复制代码
作用:删除文件或者是目录
语法:rm [-rf]  filename .....
eg
     
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file1  file2  file3  file4  file5  file6
[root@localhost ~]# rm file1
rm:是否删除普通空文件 "file1"?y
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file2  file3  file4  file5  file6
[root@localhost ~]# rm dir1
rm: 无法删除"dir1": 是一个目录
[root@localhost ~]# rm -f file2
[root@localhost ~]# ls
anaconda-ks.cfg  dir1  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]# rm -r dir1
rm:是否删除目录 "dir1"?y
[root@localhost ~]# ls
anaconda-ks.cfg  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]#  
​
​
总结:  默认情况下,是有询问的删除文件,输入y表示删除,输入n表示不删除
       如果想要强制删除文件,添加-f, 但是要慎用。
       
       如果想要删除目录,必须添加-r,表示递归删除。

2.4.4 mv

复制代码
作用:移动文件或者目录,有更名作用
语法: mv [OPTION]... SOURCE... DIRECTORY
​
eg: 
[root@localhost ~]# ls
anaconda-ks.cfg  dir2  dir3  dir4  dir5  dir6  file3  file4  file5  file6
[root@localhost ~]# mv file3 file4 dir2 dir3    # 将file3 file4 dir2 移动到dir3里
[root@localhost ~]# ls
anaconda-ks.cfg  dir3  dir4  dir5  dir6  file5  file6
[root@localhost ~]# ls dir3
dir2  file3  file4
[root@localhost ~]# mv file6 dir3/file7     #将file6移动到dir3里并更名为file7
[root@localhost ~]# ls dir3
dir2  file3  file4  file7
[root@localhost ~]#

2.4.5 cp

作用:拷贝文件或者是目录

语法:cp [-r] source....directory

eg:

root@localhost \~\]# rm -rf ./\* 删除当前目录下的所有非隐藏文件 \[root@localhost \~\]# touch file1 file2 file3 \[root@localhost \~\]# mkdir dir1 dir2 dir3 \[root@localhost \~\]# ll 总用量 0 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir1 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3 -rw-r--r--. 1 root root 0 11月 29 23:04 file1 -rw-r--r--. 1 root root 0 11月 29 23:04 file2 -rw-r--r--. 1 root root 0 11月 29 23:04 file3 \[root@localhost \~\]# cp file1 dir1 # 拷贝file1到dir1里 \[root@localhost \~\]# ll 总用量 0 drwxr-xr-x. 2 root root 19 11月 29 23:04 dir1 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3 -rw-r--r--. 1 root root 0 11月 29 23:04 file1 #源文件依然存在 -rw-r--r--. 1 root root 0 11月 29 23:04 file2 -rw-r--r--. 1 root root 0 11月 29 23:04 file3 \[root@localhost \~\]# ls dir1 # 查看dir1里的内容 file1 \[root@localhost \~\]# echo "helloworld" \> file2 #写一些字符串到file2文件里 \[root@localhost \~\]# ll 总用量 4 drwxr-xr-x. 2 root root 19 11月 29 23:04 dir1 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3 -rw-r--r--. 1 root root 0 11月 29 23:04 file1 -rw-r--r--. 1 root root 11 11月 29 23:05 file2 -rw-r--r--. 1 root root 0 11月 29 23:04 file3 \[root@localhost \~\]# cp file2 dir1/file22 # 复制file2到dir1里同时更名为file22 \[root@localhost \~\]# ll dir1 总用量 4 -rw-r--r--. 1 root root 0 11月 29 23:04 file1 -rw-r--r--. 1 root root 11 11月 29 23:05 file22 \[root@localhost \~\]# cp file3 file4 #拷贝并更名 \[root@localhost \~\]# ll 总用量 4 drwxr-xr-x. 2 root root 33 11月 29 23:05 dir1 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir2 drwxr-xr-x. 2 root root 6 11月 29 23:04 dir3 -rw-r--r--. 1 root root 0 11月 29 23:04 file1 -rw-r--r--. 1 root root 11 11月 29 23:05 file2 -rw-r--r--. 1 root root 0 11月 29 23:04 file3 -rw-r--r--. 1 root root 0 11月 29 23:07 file4 \[root@localhost \~\]# cp dir2 dir3 # 拷贝目录时, 不带参数-r,会忽略目录 cp: 略过目录"dir2" \[root@localhost \~\]# ll dir3 总用量 0 \[root@localhost \~\]# cp dir1 dir3 cp: 略过目录"dir1" \[root@localhost \~\]# ll dir3 总用量 0 \[root@localhost \~\]# cp -r dir1 dir3 # 如果想要拷贝目录,应该带上-r参数,表示递归拷贝 \[root@localhost \~\]# ll dir3 总用量 0 drwxr-xr-x. 2 root root 33 11月 29 23:08 dir1 #### 2.4.6 ln ``` 作用:用于创建链接文件 语法: ln [-s] filename newfilename 解析:   linux的链接文件分为两类,   一类是软链接文件: 软链接文件相当于windows的快捷方式                     文件和目录都可以有软链接                     创建语法: ln -s filename newfilename                       一类是硬链接文件: 文件可以有硬链接,目录不能有硬链接   创建语法: ln filename newfilename           总结: 软链接,新开辟了一个文件块和inode         硬链接,就是源文件名的别名 ``` #### 2.4.7 echo ``` 作用: 用于展示一行文件信息 语法:  echo 字符串|环境变量名 ​ [root@localhost ~]# echo you are best     # 打印一串字符,到控制台 you are best [root@localhost ~]# echo "you are best" you are best [root@localhost ~]# echo $HOME   #打印HOME变量的值 /root [root@localhost ~]# echo $PATH   #打印PATH变量的值 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@localhost ~]# echo $HOSTNAME #打印HOSTNAME变量的值 localhost.localdomain ``` 扩展:重定向符号 ``` > : 重定向到某一个位置,会覆盖原有的内容 >>: 重定向到某一个位置,追加到原有内容的后面 ​ ​ [root@localhost ~]# echo "hello world" hello world ​ # 将一串字符输入到content.txt文件里, 注意,如果该文件不存在,会直接创建。 [root@localhost ~]# echo "hello world" > content.txt   [root@localhost ~]# ll 总用量 16 -rw-r--r--. 1 root root 12 11月 29 23:43 content.txt [root@localhost ~]# echo "hello" > content.txt   #覆盖 [root@localhost ~]# ll 总用量 16 -rw-r--r--. 1 root root  6 11月 29 23:43 content.txt     #变成6个字节 [root@localhost ~]# echo "world" >> content.txt #追加 [root@localhost ~]# ll 总用量 16 -rw-r--r--. 1 root root 12 11月 29 23:44 content.txt     #变成12个字节 ``` ### 2.5 文件查看指令 #### 2.5.1 cat ``` 作用: 查看整个文件的内容 语法: cat [-An] filename 解析: -A 显示隐藏的字符     -n 显示行号 ``` #### 2.5.2 more/less ``` 作用: 用于分页查看文件内容 语法: more filename 解析: 默认查看第一页内容       空格键/f键   查看下一页       enter键: 一行一行的滚动       b :往回翻页       q|Q: 退出             less和more用法一样 ``` #### 2.5.3 head ``` 作用: 查看文件的头部信息,默认查看10行 语法: head [-number] filename 解析: 如果想要查看指定行数,添加-数字 ``` #### 2.5.4 tail ``` 作用:查看文件的末尾信息,默认查看10行 语法: tail [-number] filename 解析: 如果想要查看指定行数,添加-数字 ``` ### 2.6 文件查找指令 find ``` 作用:是可以根据指定类型参数,来查找文件系统中的文件或者是目录的 语法: find 搜索位置 条件 eg: [root@localhost ~]# find /etc -name 'init*'   按照名字查找init开头的文件或目录 /etc/inittab /etc/sysconfig/init /etc/sysconfig/network-scripts/init.ipv6-global /etc/init.d /etc/rc.d/init.d /etc/selinux/targeted/active/modules/100/init /etc/selinux/targeted/contexts/initrc_context [root@localhost ~]# find /etc -name 'init'   按照名字为init的文件或目录 /etc/sysconfig/init /etc/selinux/targeted/active/modules/100/init [root@localhost ~]# find /etc -name 'in??'   按照名字查找in开头并且长度为4的文件或目录 /etc/sysconfig/init /etc/selinux/targeted/active/modules/100/init [root@localhost ~]# find /etc -name '?i*'   按照名字查找第二个字符是i的文件或目录 ​ ​ find /etc -type d   查看/etc下的所有目录,包括子目录               d:目录   l:软链接, f:普通文件 find /etc -size -1024 find /etc -size +2K  -size -3k       单位: k,M,G等                             注意: 默认单位为一个block   一个block相当于512byte               如果想要查询小于100MB的文件, 100*1024KB  100*1024*2block               所以:find /etc -size -204800                     find /etc -size +1024   查看大于512B的文件                       ``` grep ``` 作用:用于过滤查询文件内容 语法:grep [-cinv] '搜寻字符串' filename ​ -c :输出匹配行的个数(是以行为单位,不是以出现次数为单位) -i :忽略大小写,大小写视为相同 -n :显示匹配行及行号 -v :反向选择,显示不包含匹配文本的所有行。 ​ eg: [root@localhost ~]# grep -i HOST ./profile   忽略大小写的查找host所在的行信息 [root@localhost ~]# grep -ci HOST ./profile   忽略大小写的查找host所在的行的数量 [root@localhost ~]# grep -in HOST ./profile   忽略大小写的查找host所在的行信息以及行号 [root@localhost ~]# grep -v HOST ./profile 查找除了HOST所在的行的其他行的信息 ``` 扩展:管道 ``` 管道: | 作用: 将前一个命令的结果通过管道交给后一个命令,继续操作 ​ [root@localhost ~]# grep -i HOST ./profile HOSTNAME=`/usr/bin/hostname 2>/dev/null` export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL ​ # 前一个查询的结果交给下一个grep继续过滤 [root@localhost ~]# grep -i HOST ./profile | grep USER export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL ``` which ``` 作用:用来在path变量的值中查找命令的位置 语法: which 命令 ``` whereis ``` 作用:用于查看命令的位置和帮助文件的位置 语法: whereis 命令 ``` ### 2.7 文件(解)压缩指令 **gzip/gunzip** ``` 作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件 语法: gzip filename..... 特点:   - 只能压缩文件, - 并且源文件会消失       - 压缩文件名的后缀.gz gzip -d 表示解压缩,相当于gunzip的作用 ``` **bzip2/bunzip2(需要安装)** ``` 作用:压缩指令,将每一个文件进行压缩。一个文件对应一个压缩文件 语法: bzip2 filename..... 特点:   - 只能压缩文件, - 默认情况下,源文件会消失, 除非带上-k参数,会保留源文件       - 压缩文件名的后缀.bz2 bzip2 -d 表示解压缩,相当于bunzip2的作用 ``` **zip** ``` 作用:压缩指令,可以将多个文件或者目录压缩到一个压缩文件中。 语法: zip -r compressfilename.zip file1 file2.... dir1 dir2.... 特点: - 保留源文件 - 需要自定义压缩文件名 - 如果有目录要压缩,必须添加-r参数 小贴士:zip是为了兼容windows的压缩工具,而提供的 zip和unzip都需要安装 ``` **tar** ``` 作用:打包指令,用于将多个文件打成一个包,也就是一个包文件 语法: tar -[cxvf] tarfilename.tar file............ ​ 解析: -c :表示打包,即create       -f :用于指定新文件名,必须和新文件名挨着       -x :拆包, 不能和-c共存       -v :显示压缩过程             eg: tar -cvf michael.tar file1 file2 dir1 dir2           tar -xvf michael.tar     拆包 小贴士: - 打包后的文件大小,是打包前的总和。 - 所以,如果想要压缩包文件,tar指令一般会和压缩(解压缩)指令一起使用。 常用组合:   zcvf 中的z 表示gzip           jcvf 中的j 表示bzip2 打包并压缩:tar -[zcvf] arfilename.tar file............   tar -[jcvf] arfilename.tar file............         解压缩并拆包:tar -[zxvf] arfilename.tar         tar -[jxvf] arfilename.tar ``` ### 2.8 时间指令date ``` 作用:查看或者设置时间 reg:   date     查看系统当前时间   以自定义的方式显示系统时间: date +'%Y-%m-%d %H:%M:%S'   ​ 注意:+与字符串之间不能有空格,与date之间要有空格 设置时间 eg: date -s "2015-5-8 19:48:00" 同步到bios,重启之后才能继续生效 eg: hwclock -w ​ 作用:查看时间或者修改时间 ]$ date "+%Y-%m-%d %H:%M:%S" 更改输出样式 ]$ date -s "2019-08-05 11:25:00" 设置时间 ]$ date -d "10 days ago" "+%Y-%m-%d %H:%M:%S" 获取10天前的时间 ]$ dt=`date -d "10 days ago" "+%Y-%m-%d %H:%M:%S"` ]$ echo $dt ``` ### 2.9 系统关机指令 ``` 1. 重启指令   reboot   init 6 2. 关机指令: shutdown -h now   立即关机 shutdown -h 20:30 定时关机   poweroff   half   init 0 ``` ### 2.10 linux的快捷键和basename以及dirname > ``` > ctrl+c   终止前台程序 > ctrl+z   将前台程序挂起,   fg指令是用于将挂起程序调度到前台运行 > ctrl+l   清屏,相当于clear > ​ > ctrl + a   回到命令行的最前端 > ctrl + e 回到命令行的最后面 > ctrl + w 删除光标前的一个单词 > ctrl + k 删除光标后的所有单词 > ​ > basename   /root/profile     用于显示整个路径中的最后一个名字   结果:profile > dirname   /etc/dir1/profile   显示最后一个名字之前的整个路径   结果:/etc/dir1 > ​ > ​ > tab键: 自动补全键 >       按1次的效果: 补全到相同的字符的最后一位。 >       按2次的效果: 显示所有符合该字符串开头的文件或者文件夹,或者是命令 > ``` ### 2.11 磁盘相关命令 #### 2.11.1 du ``` 作用:用于查看文件或目录的大小(磁盘使用空间) 语法:du [-ahs] [文件名|目录] 解析: -a 显示子文件的大小 -h 以易读的方式显示 KB,MB,GB等 -s summarize 统计总占有量 ​ 说明: -s和-a不能同时使用 ``` #### 2.11.2 df ``` 作用:用于查看Linux文件系统的状态信息,显示各个分区的容量、已使用量、未使用量及挂载点等信息以及剩余空间 ​ 语法:df \[-hkam] [挂载点] ​ -h(human-readable)根据磁盘空间和使用情况 以易读的方式显示 KB,MB,GB等 -k   以KB 为单位显示各分区的信息,默认 -m 以MB为单位显示信息 -a   显示所有分区包括大小为0 的分区 ``` #### 2.11.3 free ``` 作用:显示系统内存的使用情况,包括物理内存、交换内存(swap)和内核缓冲区内存。   相当于windows的任务管理器里的性能查看 语法: free [-kmg] ​ 选项: ​ -k:   以KB为单位显示,默认就是以KB为单位显示 -m: 以MB为单位显示 -g:   以GB为单位显示 ​ 清理缓存命令: echo 1 > /proc/sys/vm/drop_caches ```

相关推荐
码农101号11 分钟前
Linux中shell编程表达式和数组讲解
linux·运维·服务器
powerfulzyh23 分钟前
非Root用户启动SSH服务经验小结
运维·ssh
云道轩32 分钟前
升级centos 7.9内核到 5.4.x
linux·运维·centos
是小满满满满吗35 分钟前
传输层:udp与tcp协议
linux·服务器·网络
爱学习的小道长1 小时前
Ubuntu Cursor升级成v1.0
linux·运维·ubuntu
EelBarb1 小时前
seafile:ubuntu搭建社区版seafile12.0
linux·运维·ubuntu
Xam_d_LM1 小时前
【Latex】Windows/Ubuntu 绘制 eps 矢量图通用方法(drawio),支持插入 Latex 数学公式
linux·ubuntu·科研·矢量图·drawio
402 Payment Required1 小时前
serv00 ssh登录保活脚本-邮件通知版
运维·chrome·ssh
小柏ぁ1 小时前
calico/node is not ready: BIRD is not ready: BGP not established with xxx
运维·docker·kubernetes
moxiaoran57532 小时前
uni-app学习笔记三十--request网络请求传参
笔记·学习·uni-app