资讯详情

【期末复习、面试】Linux

Linux学习-目录

  • 一、课程简介(前言)
  • 二、 字符操作相关命令
    • 2.0 掌握重点
    • 2.1 运行级别 runlevel:print previous and current SysV runlevel.
    • 2.2 相关环境变量:echo;env;printenv;set cat /etc/environment
      • 2.2.1 显示环境变量
      • 2.2.2 uname:可显示计算机和操作系统的相关信息
    • 2.3 文件相关命令
      • 2.3.1 ls:list directory contents
      • 2.3.2 cd:change the working directory
      • 2.3.3 pwd:print name of current/working directory
      • 2.3.4 rm:remove files or directories
      • 2.3.5 mkdir:make directories
      • 2.3.6 rmdir:remove empty directories
      • 2.3.7 cp:copy files and directories
      • 2.3.8 mv:move (rename) files
      • 2.3.9 lpr/lpq/lprm
      • 2.3.10 echo:display a line of text
      • 2.3.11 cat:concatenate files and print on the standard output
      • 2.3.12 head:output the first part of files
      • 2.3.13 tail:output the last part of files
      • 2.3.14 more:file perusal filter for crt viewing
      • 2.3.15 less:opposite of more
      • 2.3.16 locate:find files by name
      • 2.3.17 grep:print lines that match patterns
    • 2.2 man;命令历史和Tab自动补全
      • 2.2.1 man:an interface to the system reference manuals
      • 2.2.2 命令历史history:GNU History Library
      • 2.2.3 Tab自动补充(命令)&参数)
      • 2.2.4 使用多个命令
    • 2.3 管道:pipelines
      • 2.3.1什么是管道
      • 2.3.2 管道的好处
    • 2.4 重定向
    • 2.5 修改权限chmod:change files mode bits
    • 2.6 字符编辑器vi:Vi IMproved, a programmer's text editor
      • 2.6.1 vi三种模式的转换
      • 2.6.2 光标移动命令
      • 2.6.3 屏幕翻滚类命令
      • 2.6.4 删除相关命令
      • 2.6.5 复制相关命令
      • 2.6.6 搜索和替换命令
      • 2.6.7 Last Line Mode命令
    • 2.7 补充:通配符和简单正则
  • 三、 Linux文件系统
    • 3.1 Linux和DOS文件系统的差异
    • 3.2 Linux文件类型有哪些?Linux文件属性中各字段的含义
    • 3.3 Linux根目录下通常有哪些目录?主目录的含义和作用
    • 3.4 相关命令
      • 3.4.1 file:determine file type
      • 3.4.2 dd:convert and copy a file文件的转换和复制
      • 3.4.3 fdisk:manipulate disk partition table操作磁盘分区表
      • 3.4.4 sfdisk:display or manipulate a disk partition table显示或操作磁盘分区表
      • 3.4.5 man:an interface to the system reference manuals
      • 3.4.6 wc:print newline, word, and byte counts for each file打印每个文件的换行符、单词和字节计数
      • 3.4.7 grep:print lines matching a pattern(整行显示匹配关键字)
      • 3.4.8 cmp:compare two files byte by byte
      • 3.4.9 diff:compare files line by line
      • 3.4.10 sdiff:side-by-side merge of file differences并行合并文件差异
      • 3.4.11 sort:sort lines of text files 按行去排序
      • 3.4.12 uniq:report or omit repeated lines报告/删除重复行
      • 3.4.13 sed:stream editor for filtering and transforming text流编辑器用于过滤和转换文本
      • 3.4.14 rev:reverse lines characterwise反转线字符
      • 3.4.15 tac:concatenate and print files in reverse反向连接和打印文件
      • 3.4.16 nl:number lines of files加行文件号
      • 3.4.17 stat:display file or file system status显示文件或文件系统状态
      • 3.4.18 ftp基本命令
      • 3.4.19 其他命令
    • 3.4 其他内容
  • 四、流程管理
    • 4.1 进程与程序
    • 4.2 父子进程间的关系
    • 4.3 进程状态
    • 4.4 前后台进程
    • 4.5 proc目录
    • 4.6 相关命令
      • 4.6.1 ps
      • 4.6.2 pstree
      • 4.6.3 kill
      • 4.6.4 top
      • 4.6.5 at
      • 4.6.6 crontab
      • 4.6.7 &后台运行
      • 4.6.8 jobs
      • 4.6.9 bg
      • 4.6.10 fg
      • 4.6.11 proc/$pid/cmd
      • 4.6.12 proc/$pid/exe
      • 4.6.13 proc/$pid/cmdline
  • 五、常用命令介绍
    • 5.1 pwd
    • 5.2 cd
    • 5.3 ls
    • 5.4 find
    • 5.5 mkdir
    • 5.6 rmdir
    • 5.7 mv
    • 5.8 cp
    • 5.9 rm
    • 5.10 chmod
    • 5.11 chown
    • 5.12 tar
    • 5.13 gzip
    • 5.14 bzip2
    • 5.15 file
    • 5.16 df
    • 5.17 more
    • 5.18 less
    • 5.19 cat
    • 5.20 su
    • 5.21 w
    • 5.22 who
    • 5.23 finger
    • 5.24 passwd
    • 5.25 whereis
    • 5.26 man
    • 5.27 info
    • 5.28 tr
    • 5.29 fmt
    • 5.30 awk
  • 六、C Prog
    • 6.1 编辑、缩排
    • 6.2 编译、运行
      • 6.2.1 gcc四个阶段
    • 6.3 库文件
    • 6.4 路径问题
  • 七、Shell Prog
  • 八、期末复习

一、课程简介(前言)

  本博客主要了记录Linux学习过程中的一些重难点(包括常见命令、SHELL编程等内容),适合期末复习以及研究生面试等浏览。   操作系统课程是计算机及相关专业的一门专业基础课,而UNIX/Linux操作系统以其稳定、开源等特性是操作系统的典型实现。通过“Unix/Linux体系及编程”课程的学习,使学生能了解,能够作为系统管理员进行日常的配置管理,

二、 字符操作类相关命令

2.0 掌握重点

见2.1部分

见2.3部分

见2.7部分

见2.2.4部分

见2.4部分

见2.5部分

见2.6.1部分

见2.6部分

2.1 运行级别 runlevel:print previous and current SysV runlevel.

:显示上次的运行级别和当前的运行级别,“N”表示没有上次的运行级别。

man runlevel

2.2 相关环境变量:echo;env;printenv;set cat /etc/environment

2.2.1 显示环境变量

env:run a program in a modified environment

echo:用于显示某个/某几个环境变量

cat /etc/environment

2.2.2 uname:可显示电脑以及操作系统的相关信息

uname 及 uname -a:a-all

Linux:操作系统(print the kernel name)名称(uname -s)s-sysname zoushilong:显示网络节点上的主机名(print the network node hostname)(uname -n)n-nodename 5.4.0-90-generic:显示内核发行版本号、(print the kernel release)(uname -r)r-release #101-Ubuntu SMP Fri Oct 15 20:00:55 UTC 2021:系统版本及时间(内核版本)(print the kernel version)(uname -v)v-version x86_64:显示主机的硬件架构名称(print the machine hardware name)(uname -m)m-machine x86_64:打印处理器类型(print the processor type or"unknown")(uname -p)p-processor x86_64:打印硬件平台(print the hardware platform or"unknown")(uname -i) GNU/Linux:打印操作系统名称(print the operating system)(uname -o)

2.3 文件相关命令

2.3.1 ls:list directory contents

ls:list directory contents

ls -l

:为文件的类型及权限。

  • 第10位为d,表示目录;为-,表示普通文件;为l,表示链接文件
  • 第7~9位,表示文件所有者owner的权限
  • 第4~6位,表示同组用户group的权限
  • 第1~3位,表示其他用户others的权限

:表示链接数,表示有多少个文件链接到inode号码

  • 对于目录,链接数至少为2
  • 对于普通文件,链接数大于/等于1

:所有者owner

:所属群组

:文件大小

  • 对于,默认为4K,即4096
  • 对于,大小为ASCII码的个数

:文件名

ls -a

ls -l 1.txt 等同于 ll 1.txt

2.3.2 cd:change the working directory

2.3.3 pwd:print name of current/working directory

2.3.4 rm:remove files or directories

rm -i 1.txt:提示是否进行删除

2.3.5 mkdir:make directories

嵌套创建:mkdir -p father/son

2.3.6 rmdir:remove empty directories

嵌套删除:rmdir -p father/son

2.3.7 cp:copy files and directories

DESCRIPTION:Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

cp 1.txt doc/2.txt:复制同时取别名 cp 1.txt doc/:复制文件到doc目录下 cp ../1.txt ./:复制上层目录下的1.txt文件到当前目录下

2.3.8 mv:move (rename) files

DESCRIPTION:Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.

mv 1.txt 2.txt:文件改名,源文件1.txt不存在,同cp的不同

2.3.9 lpr/lpq/lprm

lpr:off line print 【DESCRIPTION:lpr uses a spooling daemon to print the named files when facilities become available.】 lpq:spool queue examination program 【DESCRIPTION:lpq examines the spooling area used by lpd(8) for printing files on the line printer, and reports the status of the specified jobs or all jobs associated with a user.】 lprm:remove jobs from the line printer spooling queue 【DESCRIPTION:lprm will remove a job, or jobs, from a printer’s spool queue. 】

2.3.10 echo:display a line of text

DESCRIPTION:Echo the STRING(s) to standard output.

2.3.11 cat:concatenate files and print on the standard output

连接文件并在标准输出上打印

cat md.c:查看文件内容

cat -n md.c:显示行号

2.3.12 head:output the first part of files

DESCRIPTION:Print the first 10 lines of each FILE to standard output.

2.3.13 tail:output the last part of files

DESCRIPTION:Print the last 10 lines of each FILE to standard output.

2.3.14 more:file perusal filter for crt viewing

【DESCRIPTION:more is a filter for paging through text one screenful at a time.】More是一个过滤器,用于一次一个屏幕地对文本进行分页。回车向下翻1行,空格向下翻1页,q退出

cat -n md.c | more:常用命令

2.3.15 less:opposite of more

2.3.16 locate:find files by name

【DESCRIPTION:locate reads one or more databases prepared by updatedb(8) and writes file names matching at least one of the PATTERNs to standard output, one per line.】 locate读取updatedb(8)准备的一个或多个数据库,并将匹配至少一个pattern的文件名写入标准输出,每行一个。

locate hello

2.3.17 grep:print lines that match patterns

grep hello 1.txt:显示包含关键字的一整行。

grep -c hello 1.txt:计算符合样式的列数

输出结果为:1、5、6、7四行内容,且

2.2 man;命令历史和Tab自动补全

2.2.1 man:an interface to the system reference manuals

系统参考手册的界面 man ls

man 3 printf

man man:下表显示了手册的章节编号,后跟它们所包含的页面类型。

2.2.2 命令历史history:GNU History Library

【DESCRIPTION:Many programs read input from the user a line at a time. The GNU History library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones.】 许多程序从用户处一次读取一行输入。GNU History库能够跟踪这些行,将任意数据与每一行关联起来,并利用来自前一行的信息来组成新的行。 history

!2015:运行第2015行命令

!d:运行最近以d开头的命令

2.2.3 Tab自动补全(命令&参数)

:若能匹配,便会直接补全 :匹配以相同字母打头的相关文件,并输出相关文件

2.2.4 使用多重命令

date;tom;uname:用;不管结果真假,均执行。即执行所有命令,包含错误

date&&tom&&uname:第一个操作是false就停止

date||tom||uname:第一个操作是true就停止

2.3 管道:pipelines

2.3.1什么是管道

  管道是进程间通信的一种实现方式,一个命令的输出可以通过管道做为另一个命令的输入。

2.3.2 管道的好处

  ①将逻辑上简单的操作串起来,实现巧妙的工作   ②速度快,管道是内存操作,不需要I/O读写,不涉及磁盘操作。即提高速度的同时,节省了大量的I/O读写。

echo xyy myy lyy

echo xyy myy lyy | tr " " "\n"

echo xyy myy lyy | tr " " "\n" | sort

echo xyy myy lyy | tr " " "\n" | sort -r

2.4 重定向

ls > 2.txt

ls tom 2> ls.err

cat < ls.err

ls xyymyy 2>> ls.err

cat -n unistd.h | grep STD

2.5 修改权限chmod:change files mode bits

chmod

chmod u+x 1.txt:给1.txt文件user用户添加的x权限,即执行权限。 chmod a+x,o-w 1.txt:给1.txt文件所有用户添加可执行权限,others用户删除写权限 chmod u=rwx,g=rw,o=x 1.txt:直接给1.txt文件赋予权限 chmod octal 1.txt:八进制修改文件权限,例如chmod 777 1.txt,默认权限为:664 chmod -R 755 doc/:统一递归修改文件权限

2.6 字符编辑器vi:Vi IMproved, a programmer’s text editor

  • 字符模式配置及系统维护工作
  • vi
  • :q回车

2.6.1 vi的三种模式的转换

2.6.2 光标移动类命令

2.6.3 屏幕翻滚类命令

2.6.4 删除相关命令

2.6.5 复制相关命令

2.6.6 搜素及替换命令

2.6.7 Last Line Mode命令

set nu:打开行号 set nonu:关闭行号

:1,3 co 5:将1-3行的内容拷贝到第5行

:1,3 m 5:将第1-3行的内容移动到第5行

:r 2.txt:在光标位置加载2.txt新文件

:e:在编辑当前文件的情况下不退出,继续编辑新的文件 :Sex:水平分割窗口,下半部分文件的内容,上半部分目录树。

   第二行:当前文件所在路径    第三行:按照文件名排序    …表示上一层目录 ,按回车进入    .当前目录,按回车进入    按q退出,退出分屏

Vex:垂直分割,Ctrl+W:在两个窗口间进行切换

:set spell:检查文档中单词错误

!cal:在不退出vi的情况下,运行一个命令

r!cal:在不退出vi的前提下,运行一个SHELL命令,将命令的运行结果贴在当前文件中

   将一个命令的运行结果自动贴到文档中 vimtutor:vi帮助文档(了解) vimdiff:区别版本的信息,两个文件的信息(了解)

2.7 补充:通配符和简单正则

三、 Linux文件系统

3.1 Linux和DOS文件系统的区别

3.2 Linux有哪些文件类型;Linux文件属性中各字段的含义

查看文件类型:ls -l 文件名

file命令 file 1.txt

file md.c

file stat.h

file first.s

Linux源代码包括.c文件、.h文件以及1/3左右的汇编文件(.s)。

链接文件:file doc/

l:link file stdin

标准的输入设备:0、标准的输出设备(stdout):1、标准的错误输出(stderr):2

块设备特殊文件(/dev目录下)   sda:第一个设备   第27个:sdaa   sda1: 第一个设备的第一个分区

file sda

字符设备特殊文件:

  文件在内核中通过数字进行管理,对于设备特殊文件,sda设备(8,0),主设备号8,次设备号0,owner属于root,group属于磁盘disk。内核中通过major(读取主设备号)、minor(读取次设备号)函数进行管理。tty主设备号为5,次设备号为0。

file tty

套接字文件:网络编程中一种特殊文件,内存可以直接从网卡读或写文件,RDMA(去对方网卡中读)

3.3 Linux根目录下一般有哪些目录?主要目录的含义与作用

root:管理员的 sys:系统管理

boot:系统启动 lost+found:失物招领

3.4 相关命令

3.4.1 file:determine file type

见前面内容

3.4.2 dd:convert and copy a file转换和复制文件

man dd

bs:block sizes,一次读取多大的bs conunt:数量,转换多少块 if:input file of:output file

dd if=/dev/hdx of=/dev/hdy:整盘备份,设备是文件。配置大型服务器,32个结点,配好一个结点,结合脚本 of=/path/to/image:在硬盘的第一个扇区中521字节,MBR-主引导记录 if=/dev/urandom:产生随机数,不断向目标文件写随机数,表明销毁硬盘中数据,不同于删除,不易恢复

if=/var/test of=/dev/null:纯读速度,null可以无限接收文件,并没有真正去写,只是读

if=/dev/zero of=/var/test:并没有真正读,真正地写

3.4.3 fdisk:manipulate disk partition table操作磁盘分区表

每个分区也是通过数字进行管理 sudo fdisk -l /dev/sda2

Disk:disk文件、大小20GiB、具体大小:2…、sectors:扇区 真正一次读取一个扇区的大小:512 扇区大小(虚地址/实地址)大小:512,方便换算 I/O大小:一次读取多少最优(最小/最优)

sudo fdisk -l:打印所有分区的信息

3.4.4 sfdisk:display or manipulate a disk partition table显示或操作磁盘分区表

sudo sfdisk -g:显示几何的信息

硬盘最重要的三个几何参数:heads:磁头、sectors:扇区、track:整个扇区磁道、cylinders:柱面

sudo sfdisk -T:现在的操作系统支持哪些文件系统

Name:文件系统的名字、16进制

3.4.5 man:an interface to the system reference manuals

man查看帮助文档

3.4.6 wc:print newline, word, and byte counts for each file打印每个文件的换行符、字和字节计数

统计文件中有行,有个words,有*个characters wc -l 1.txt

wc -w 1.txt wc -c 1.txt

ls | wc -l:统计当前目录下面有多少的文件

ls /dev/tty* | wc -l:筛选并统计dev目录下tty文件有多少个

3.4.7 grep:print lines matching a pattern(把匹配关键字的一整行显示出来)

在某个文件中搜索某个关键字

grep hello 1.txt:显示文件中包含hello的行

grep -n hello 1.txt:显示文件中包含hello的行的行号

grep -n -1 hello 1.txt:显示文件中包含hello的行的上一(-2)行和下一(-2)行、上下文(n-line number)

grep -c hello 1.txt:显示文件中包含hello的行的次数,一行两个hello算3次(c-count)

grep -v hello 1.txt:显示文件中不匹配hello的行的有哪些(v-invert match)

grep -i hello 1.txt:显示文件中匹配hello的行的有哪些(忽略掉大小写的区别)

grep -n -i hello 1.txt 99.txt:搜索多个文件中包含hello的行

cat /etc/passwd | grep stu:查看文件中有多少stu前缀的用户

3.4.8 cmp:compare two files byte by byte

cmp 1.txt 2.txt:只给出两个文件第一个不同的地方

cmp 1.txt 2.txt:修改一行内容后,执行cmp命令

第二行,第41个byte不一样

3.4.9 diff:compare files line by line

diff 1.txt 2.txt:逐行比较两个文件。2c2 修改一行

diff 1.txt 2.txt:逐行比较两个文件。 分隔符 — 表示上一个文件和下一个文件。2c2想使两个文件一样需要修改第二行、4a5想使两个文件一样要增加一行,在第四行的位置增加第五行,第五行内容为空行。

3.4.10 sdiff:side-by-side merge of file differences文件差异的并行合并

sdiff 1.txt 2.txt:左右两边分栏对比,较为常用的方式、简单代码对比、多个版本代码的比较

3.4.11 sort:sort lines of text files 按照行去排序

sort student_records:按第一列首字母的字母顺序排序

sort student_records > student_records.sorted:按第一列首字母的字母顺序排序,并保存在.sorted文件中

sort -r student_records:按第一列首字母的字母顺序逆序排序

sort -k 4 student_records:以第4列为关键字进行排序

sort -k 4 -k 3 student_records:以第四个关键字排序,再指定第二个关键字。若第4列关键字相同,按另一个关键字进行排序 sort sortnum.txt:以字符排序 sort -n sortnum.txt:以数值排序,而不是字符

sort -t ';' com.txt:以;为分隔符,第1列为关键字排序(student_records文件:用tab键分隔浪费空间、/etc/passwd用:分隔)类似的文件需要告诉 用什么分隔的 sort -t ';' -k2 com.txt:以;为分隔符,第2列为关键字排序

sort -t ';' -k2 -n com.txt:以;为分隔符,第2列为关键字按数值大小进行排序

sort -k 1.3 com.txt:以第一个字段,第三个字符为关键字进行排序

sort *.txt > txt.sorted.file:将文件内容统一合并然后进行排序

3.4.12 uniq:report or omit repeated lines报告/删除重复的行

adjacent sort student_records | uniq:先对文件进行排序然后进行去重处理

sort student_records | uniq -d:先对文件进行排序,然后查看谁重复了(对于连续重复的一组只输出一条)

sort -u student_records:等价命令。先排序然后去重(sort本身可以去重-u参数)

uniq student_records:必须先排序,然后去重

实验内容:统计上课信息 w

cat 201006041620 |grep unix|wc -l:查看文件内容,并输出含unix的行的个数(需要进行过滤)

cat 201006041620 |grep unix|sort|wc -l:查看文件内容,对含unix的行的第一个关键字进行排序,并输出行的个数 cat 201006041620 |grep unix|sort|uniq|wc -l:查看文件内容,对含unix的行的第一个关键字进行排序并去重,输出行的个数

cat 201006041620 |cut -c -9|grep unix|sort|uniq|wc -l:查看文件内容,先提取出前9个字符,对含unix的行的第一个关键字进行排序并去重,输出行的个数

cat 201006041620|cut -c -9:看文件内容、切前面9个

3.4.13 sed:stream editor for filtering and transforming text用于过滤和转换文本的流编辑器

sed '' 1.txt:输出整个文本

sed -n '' 1.txt:什么模式也没有指定、等于关闭输出、-n仅显示模式匹配的结果 sed -n '5p' 1.txt:只输出第5行

sed -n 'p' 1.txt:显示所有的行 sed -n '2~2p' 1.txt:打印偶数行,第2行开始,步长为2 sed -n '1,3!p' 1.txt:打印1~3行以外的行(!表示取反) sed '1,3d' 1.txt:删除1~3行 sed '1,3a &&&&&&&&&&' 1.txt:1~3行每一行后添加&&&&&&&&&&

sed '1,3y/abcde/ABCDE/' 1.txt:1~3行对应字母变大写 sed '1d' 1.txt 2.txt:对多个文件同时进行处理,删除第1行 sed -e 's/hello/hi/g' 1.txt:将文件中所有的hello替换为hi sed -e 's/popular/wellknown/g' sed.txt > sed.txt.new:将sed.txt文件中所有的popular替换为wellknown,重定向到sed.txt.new cat script.sed:将参数存起来

sed -f script.sed sed.txt > sed.txt.new:-f读取这个文件,读取这个文件中的参数对该文件处理(适用于自动生成的文件) sed -n '/hello/w hello.txt' 1.txt:在1.txt文件中包含hello的行写入hello.txt(w即write)

sed -n l 1.txt:字母l,显示文件用什么进行分隔的,是$(行尾)或者\t即tab键(常用该命令查看分隔符号进行提取)

sed -i:直接对原始文件进行操作

3.4.14 rev:reverse lines characterwise反转线字符

rev 1.txt:行内反序输出、行里面的内容反序

3.4.15 tac:concatenate and print files in reverse反向连接和打印文件

tac 1.txt:文件内的行反序输出、最后一行变第一行

3.4.16 nl:number lines of files给文件加行号

Write each FILE to standard output, with line numbers added. nl 1.txt:给文件加行号(默认空行不加编号)

cat相关命令 cat -b(覆盖掉-n):空行不加行号,同nl

nl -n rz 1.txt:按照某种格式rz(以0开始编号,左对齐)给文件加行号

3.4.17 stat:display file or file system status显示文件或文件系统状态

ll 1.txt:查看文件最后一次被修改的时间(Mtime),cp命令会改变文件的修改时间

ls -lc 1.txt:查看文件的状态时间(Ctime)、文件属性状态被修改的时候(chmod) ls -lu 1.txt:查看文件的(最后一次读的)访问时间(Atime)、cat、more会改变,stat、ls不会改变

stat 1.txt:查看文件的三个时间

Links:文件链接数 inode:ls -i 1.txt:索引号 Uid:userID、Gid:Group ID :最后一次访问的时间、:最后一次修改的时间、:修改属性的时间 chmod改变文件修改属性的时间

cat 1.txt > 1.txt.cat 不修改源文件1.txt的三个时间 vi 1.txt修改内容存盘退出,三个时间均修改了

touch -a:修改acesstime、changetime touch -m:修改modifytime、changetime touch:修改三个时间 touch -a 1.txt

touch 1.txt:修改三个时间 touch filename:文件存在则修改其时间,否则创建该文件。

3.4.18 ftp基本命令

3.4.19 其他命令

du -h:查看目录的容量

df -i:文件系统的磁盘使用情况统计

cat /proc/filesystems:查看Linux已载入到内存中的文件系统

ls -l /lib/modules/$(uname -r)/kernel/fs:查看本地Linux支持的文件系统 uname -r:操作系统发行编号,加$取出里面的值

3.4 其他内容

:存放文件系统 file system table

静态的文件系统信息,'blkid’打印通用的标识符号,本机ext4的文件系统

parted:创建磁盘分区 mkfs:格式化分区

major:主设备号、minor:次设备号、#blocks:分区的大小、name:设备的名字

:显示文件系统硬盘的使用率

文件名称、大小、使用了、还有多少空间可用、用的百分比、挂载点

static:静态的、variable:动态的、随着系统的运行内容在不断变化 shareable:共享的、unshareable:非共享的 每个用户的/etc都是自己的

四、进程管理

4.1 进程与程序

进程与程序之间的区别和联系:

4.2 父子进程间的关系

4.3 进程状态

4.4 前后台进程

4.5 proc目录

4.6 相关命令

4.6.1 ps

4.6.2 pstree

4.6.3 kill

4.6.4 top

4.6.5 at

4.6.6 crontab

4.6.7 &后台运行

4.6.8 jobs

4.6.9 bg

4.6.10 fg

4.6.11 proc/$pid/cmd

4.6.12 proc/$pid/exe

4.6.13 proc/$pid/cmdline

ps -l

ps aux

watch -n 3 date & ps -l kill 2827 ps -l kill -9 2827 ps -l

ps -l vi 0524.txt ps -l jobs -l

fg %1 jobs -l

五、常用命令介绍

5.1 pwd

5.2 cd

5.3 ls

5.4 find

5.5 mkdir

5.6 rmdir

5.7 mv

5.8 cp

5.9 rm

5.10 chmod

5.11 chown

5.12 tar

5.13 gzip

5.14 bzip2

5.15 file

5.16 df

5.17 more

5.18 less

5.19 cat

5.20 su

5.21 w

5.22 who

5.23 finger

5.24 passwd

5.25 whereis

5.26 man

5.27 info

5.28 tr

5.29 fmt

5.30 awk

六、C Prog

6.1 编辑、缩排

  • vi/vim、emacs、nano
  • cd:C program beautifier、bcpp:make C++ beautifier、indent

indent:changes the appearance of a C program by inserting or deleting whitespace. 类似的tex、latex

cat -n first.c | indent

cat first.c |indent

cat second.c | indent

cat first-err.c |indent:嵌套错误

cat second.c | indent -br/-bl -bl:Put braces on line after if -br:Put braces on line with if

6.2 编译、运行

gcc:GNU project C and C++ compiler gcc --version g++ --version

gcc first.c:默认编译生成的文件名为a.out

./a.out:.表示运行当前目录下的a.out文件

gcc second.c

gcc first.c -o helloworld:给生成的outfile文件起个名字helloworld 注意:-o+目标文件名字

C++编译器可以编译C程序,反过来不可以 gcc first.c and g++ first.c:编译C程序 g++ hello.cpp 可正常运行、gcc hello.cpp 报错

6.2.1 gcc四个阶段

gcc编译四个阶段 man gcc

gcc hello.c:一次编译,默认生成可执行文件a.out gcc hello.c -o b.out:一次编译,给生成的可执行文件取名b.out

gcc -E hello.c -o hello.i:编译的第一个阶段。调用预处理程序cpp:主要解决头文件。向其中插入"#include"语句所包含的内容,并由它负责展开在源文件中定义的宏,生成.i文件(gcc -E) gcc -S hello.i -o hello.s:编译的第二个阶段。检查语法错误cc将代码翻译成汇编语言.s文件,该阶段只进行翻译而不进行汇编,生成汇编代码。(gcc -S) gcc -c hello.s -o hello.o:编译的第三个阶段。调用as将编译阶段生成的.s文件转成.o文件:不进行链接操作。(gcc -c) gcc hello.o -o c.out:编译的最后一个阶段。调用链接程序ld:把生成的目标代码链接成一个可执行程序(gcc *.o)

gcc -S hello.c:在第二步停止,默认生成hello.s文件 cat hello.i:查看hello.i文件

cat hello.s:查看hello.s文件

.o文件(二进制文件),cat命令只可以看普通文本。用readelf来看 readelf -S hello.o:查看elf文件,可执行、可链接文件Executable and Linking Format man readelf:display information about ELF files

gcc -M hello.c:生成头文件关联的信息

gcc -v hello.c:显示编译版本、头文件目录等信息

gcc -xc hello.tom:-x language告诉gcc按照什么标准去编译源文件。后缀名是什么不重要,可以以c程序方式编译.tom文件。gcc hello.tom直接编译不可以

6.3 库文件

  • Windows:.lib:静态链接库.dll:动态链接库 Linux:.a:静态链接库.so:动态(共享)链接库 ls /lib/libc*:查看C语言的动态链接库和静态链接库 ls /lib/x86_64-linux-gnu/libc*

ls -l /lib64/:通过ld-linux-x86-64.so.2总加载器加载其它库函数(一般指向/lib) 为了方便单独建了一个64位的lib目录

链接文件。链接时通过该库函数把其他的库函数加载出来

ldd a.out:ldd可以查看该命令本身调用了哪些动态链接库 ldd /bin/ls:ls在启动后,加载器将所有的动态库加载后然后再将控制权移交给ls程序的入口。 man ldd:print shared object dependencies动态链接库

第一个必不可少的,第二个是C的动态链接库,第三个总的加载器。

readelf -l a.out:gcc包含了一个特殊的elf头:INTERP,其作用是指定加载器的路径。

6.4 路径问题

".h":先去下查找,若当前目录没有,再去系统目录下查找/usr/include/ <*.h>:先去下找,内核态源代码一般存放在该文件下

find ./ -name dvss.c:查找dvss.c文件

cat dvss.c:查看该文件的内容

gcc -static dvss.c:生成a.out,静态链接库的方式编译 gcc dvss.c -o b.out:默认方式,动态共享链接库的方式 ll *.out:查看是否生成了两个文件 size *.out:静态生成的文件大,动态生成的文件小。 size:查看每个可执行程序的大小

time:衡量程序运行的时间 time ./a.out tom and jerry xyy myy lyy:先运行后面的程序,再显示运行该程序的时间

real:真正运行的时间 user:用户态 sys:内核态(不一定所有的都需要) 不是所有的real==user+sys,一般real>user+sys

程序的作用是输出所有的argv

time ./b.out tom and jerry xyy myy lyy

cat -n power.c

包含<math.h>头文件 gcc power.c:编译文件,找不到power函数。编译器设计问题,想gcc,在编译时添加参数-lm l-link,m-math gcc power.c -lm:不是所有的代码都需要链接,用到的加一个参数,提高速度。用到了数学库函数,需要显示地告诉编译器去找。 ll a.out

ldd a.out:a.out用到了数学库函数,而hello.c没有第二行的。第四行总的链接器

man power

ldd power:查看相应的依赖

  • cat makefile-power:把一些编译的规则放在一个文件中,关键是如何写规则。

前两行注释。 规则(power目标:power.c来源)、动作cc power.c -o power -lm用到了数学库函数(用TAB键开始)

make -f makefile-power:make编译规则文件,-f读取后面文件的规则 power是最新的,不用再编译,之前编译过,没有修改。 gcc power.c -o power -lm:不管时间戳,进行编译 make的一个优势:判断源代码与目标代码的时间戳。

touch power.c:若文件不存在,创建新的文件;若文件存在,将该文件touch到最新的时间 make -f makefile-power:太麻烦了

一般来说,Make会在当前目录自动查找三个文件,先找GNUMakefile,再找makefile,再找Makefile。均没有就报错,若存在其中一个直接make就可以

将上面的power.c文件拆开。 cat -n input.h cat -n computer.h cat -n main.h

cat -n input.c cat -n computer.c cat -n main.c

cat makefile make rm pow:只做了第一条动作 make clean:执行所有的动作 生成.o文件

简化动作:gcc知道.c->.o需要执行的操作 make clean

touch input.c:重新编译input.c,说明input.o修改了,再去生成新的main.o

如何简化规则?

$<:对每个依赖文件,对所有的.c都生成对应的.o

gcc -M hello.c:生成头文件关联的信息

gcc -MM hello.c:不包含头文件的依赖关系

gcc -MM main.c > main.makefile:将依赖关系保存起来 gcc -MM main.c -MF main.makefile:同上 gcc -MM main.c -MT hello.o:修改目标代码 改为hello.o

一个C文件在内存中布局,如何存放的: 正文段(text)、初始化的数据段(声明时赋值、初始化)、未初始化的数据段(声明一个数组用的时候进行赋值)、堆(动态内存分配)、栈(自动变量、函数调用、上下文切换信息)

低地址部分:正文段、初始化的数据段(两部分直接从程序中读入由exec读入),未初始化的数据段(数组、初始化为0由exec做),命令行参数。 malloc函数:用来分配内存的,只能在stack、heap之间的区域。是一个通用的函数,不能直接分配内存,调用了sbrk。stack break只能在stack、heap之间。 man malloc:Library calls

man brk:System calls

size /bin/ls size /bin/

gcc cat.c gcc -Wall cat.c

gcc -w cat.c

gcc -Wall -Wno-unused cat.c:屏蔽掉Wunused类错误 gcc -Wall cat.c

gcc -Wall -Wno-implicit cat.c:关闭掉implicit类错误

gcc -Wall -Werror cat.c:警告当做错误处理

cat -n try-gcc-op.c:优化编译

gcc try-gcc-op.c:无优化、默认 -O0 gcc -O1 try-gcc-op.c -o b.out:主要进行跳转和延迟退栈两种优化 gcc -O2 try-gcc-op.c -o c.out:除了完成-O1的优化之外,还进行一些额外的调整工作,如指令调整等。 gcc -O3 try-gcc-op.c -o d.out:优化级别最高,打开所有-O2的优化选项并包括循环展开和其他一些与处理特性相关的优化工作

time ./a.out:运行当前可执行文件,并打印运行时间 time ./b.out

time ./c.out

time ./d.out

cat string-debug.c gcc string-debug.c

gcc string-debug.c -g -o b.out ll *.out gdb

file b.out:装入被调试的可执行文件 l:列出源代码 r:运行

b string-debug.c:7 r set variable string=(char *)malloc(100) n

q

简单的例子 cat myls.c gcc myls.c ./a.out doc/

cat errno.c gcc errno.c ./a.out

cat -n helofile.c

gcc holefile.c ll file.hole ./a.out ll file.hole cat file.hole od -c file.hole

七、Shell Prog

cat hello.sh ll hello.sh chmod u-x hello.

标签: 连接器q18j4a

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台